blob: e6e9e7bd53ba1155bf66642cf881c909c6b9ad0d [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'od1154eb2011-09-18 17:34:37 -040018#include "config.h"
Theodore Ts'oc762c8e1997-06-17 03:52:12 +000019#ifdef HAVE_GETOPT_H
20#include <getopt.h>
Theodore Ts'o373b8332000-04-03 16:22:35 +000021#else
22extern char *optarg;
23extern int optind;
Theodore Ts'oc762c8e1997-06-17 03:52:12 +000024#endif
Theodore Ts'ofe073572005-12-29 15:39:40 -050025#include <unistd.h>
Theodore Ts'of38cf3c2008-09-01 11:17:29 -040026#ifdef HAVE_STDLIB_H
27#include <stdlib.h>
28#endif
Theodore Ts'ofe073572005-12-29 15:39:40 -050029#include <sys/types.h>
Theodore Ts'o116db1b2002-04-01 01:28:30 -050030#include <sys/stat.h>
Theodore Ts'ofe073572005-12-29 15:39:40 -050031#include <fcntl.h>
Theodore Ts'oc762c8e1997-06-17 03:52:12 +000032
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -050033#include "e2p/e2p.h"
34
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +000035#include "resize2fs.h"
36
Theodore Ts'o0cee8a52000-04-06 21:38:34 +000037#include "../version.h"
Theodore Ts'o2e561e01998-04-08 06:18:37 +000038
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -050039char *program_name, *device_name, *io_options;
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +000040
Theodore Ts'odfcdc322001-01-11 15:38:00 +000041static void usage (char *prog)
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +000042{
Josef Bacik199ddaa2008-03-13 13:46:10 -040043 fprintf (stderr, _("Usage: %s [-d debug_flags] [-f] [-F] [-M] [-P] "
44 "[-p] device [new_size]\n\n"), prog);
Theodore Ts'o2e561e01998-04-08 06:18:37 +000045
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +000046 exit (1);
47}
48
Theodore Ts'o3b627e81998-02-24 20:24:49 +000049static errcode_t resize_progress_func(ext2_resize_t rfs, int pass,
50 unsigned long cur, unsigned long max)
Theodore Ts'o63b44fb1998-02-13 22:58:18 +000051{
52 ext2_sim_progmeter progress;
53 const char *label;
54 errcode_t retval;
55
56 progress = (ext2_sim_progmeter) rfs->prog_data;
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +000057 if (max == 0)
Theodore Ts'o3b627e81998-02-24 20:24:49 +000058 return 0;
Theodore Ts'o63b44fb1998-02-13 22:58:18 +000059 if (cur == 0) {
60 if (progress)
61 ext2fs_progress_close(progress);
62 progress = 0;
63 switch (pass) {
Theodore Ts'oa8519a21998-02-16 22:16:20 +000064 case E2_RSZ_EXTEND_ITABLE_PASS:
Theodore Ts'oa13575f2000-06-12 22:06:16 +000065 label = _("Extending the inode table");
Theodore Ts'o63b44fb1998-02-13 22:58:18 +000066 break;
67 case E2_RSZ_BLOCK_RELOC_PASS:
Theodore Ts'oa13575f2000-06-12 22:06:16 +000068 label = _("Relocating blocks");
Theodore Ts'o63b44fb1998-02-13 22:58:18 +000069 break;
Theodore Ts'oa8519a21998-02-16 22:16:20 +000070 case E2_RSZ_INODE_SCAN_PASS:
Theodore Ts'oa13575f2000-06-12 22:06:16 +000071 label = _("Scanning inode table");
Theodore Ts'o63b44fb1998-02-13 22:58:18 +000072 break;
73 case E2_RSZ_INODE_REF_UPD_PASS:
Theodore Ts'oa13575f2000-06-12 22:06:16 +000074 label = _("Updating inode references");
Theodore Ts'o63b44fb1998-02-13 22:58:18 +000075 break;
76 case E2_RSZ_MOVE_ITABLE_PASS:
Theodore Ts'oa13575f2000-06-12 22:06:16 +000077 label = _("Moving inode table");
Theodore Ts'o63b44fb1998-02-13 22:58:18 +000078 break;
Theodore Ts'oa8519a21998-02-16 22:16:20 +000079 default:
Theodore Ts'oa13575f2000-06-12 22:06:16 +000080 label = _("Unknown pass?!?");
Theodore Ts'oa8519a21998-02-16 22:16:20 +000081 break;
Theodore Ts'o63b44fb1998-02-13 22:58:18 +000082 }
Theodore Ts'oa13575f2000-06-12 22:06:16 +000083 printf(_("Begin pass %d (max = %lu)\n"), pass, max);
Theodore Ts'o63b44fb1998-02-13 22:58:18 +000084 retval = ext2fs_progress_init(&progress, label, 30,
85 40, max, 0);
86 if (retval)
87 progress = 0;
88 rfs->prog_data = (void *) progress;
89 }
90 if (progress)
91 ext2fs_progress_update(progress, cur);
92 if (cur >= max) {
93 if (progress)
94 ext2fs_progress_close(progress);
95 progress = 0;
96 rfs->prog_data = 0;
97 }
Theodore Ts'o3b627e81998-02-24 20:24:49 +000098 return 0;
Theodore Ts'o63b44fb1998-02-13 22:58:18 +000099}
100
Theodore Ts'o46c54902006-05-14 15:33:57 -0400101static void determine_fs_stride(ext2_filsys fs)
102{
103 unsigned int group;
104 unsigned long long sum;
105 unsigned int has_sb, prev_has_sb, num;
106 int i_stride, b_stride;
107
Theodore Ts'o96c6a3a2007-05-18 22:06:53 -0400108 if (fs->stride)
109 return;
Theodore Ts'o46c54902006-05-14 15:33:57 -0400110 num = 0; sum = 0;
111 for (group = 0; group < fs->group_desc_count; group++) {
112 has_sb = ext2fs_bg_has_super(fs, group);
113 if (group == 0 || has_sb != prev_has_sb)
114 goto next;
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400115 b_stride = ext2fs_block_bitmap_loc(fs, group) -
116 ext2fs_block_bitmap_loc(fs, group - 1) -
Theodore Ts'o46c54902006-05-14 15:33:57 -0400117 fs->super->s_blocks_per_group;
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400118 i_stride = ext2fs_inode_bitmap_loc(fs, group) -
119 ext2fs_inode_bitmap_loc(fs, group - 1) -
Theodore Ts'o46c54902006-05-14 15:33:57 -0400120 fs->super->s_blocks_per_group;
121 if (b_stride != i_stride ||
122 b_stride < 0)
123 goto next;
124
125 /* printf("group %d has stride %d\n", group, b_stride); */
126 sum += b_stride;
127 num++;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400128
Theodore Ts'o46c54902006-05-14 15:33:57 -0400129 next:
130 prev_has_sb = has_sb;
131 }
132
133 if (fs->group_desc_count > 12 && num < 3)
134 sum = 0;
135
136 if (num)
137 fs->stride = sum / num;
138 else
139 fs->stride = 0;
140
Theodore Ts'o96c6a3a2007-05-18 22:06:53 -0400141 fs->super->s_raid_stride = fs->stride;
142 ext2fs_mark_super_dirty(fs);
143
Theodore Ts'o46c54902006-05-14 15:33:57 -0400144#if 0
145 if (fs->stride)
146 printf("Using RAID stride of %d\n", fs->stride);
147#endif
148}
149
Theodore Ts'o0a617312000-02-02 19:14:36 +0000150int main (int argc, char ** argv)
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000151{
152 errcode_t retval;
153 ext2_filsys fs;
154 int c;
Theodore Ts'o05e112a1997-06-14 07:28:44 +0000155 int flags = 0;
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000156 int flush = 0;
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000157 int force = 0;
Theodore Ts'o5d2ef122006-03-18 20:03:47 -0500158 int io_flags = 0;
Josef Bacik199ddaa2008-03-13 13:46:10 -0400159 int force_min_size = 0;
160 int print_min_size = 0;
Theodore Ts'ofe073572005-12-29 15:39:40 -0500161 int fd, ret;
Valerie Aurora Henson8728d502010-06-13 18:00:00 -0400162 blk64_t new_size = 0;
163 blk64_t max_size = 0;
164 blk64_t min_size = 0;
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000165 io_manager io_ptr;
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -0500166 char *new_size_str = 0;
Theodore Ts'o46c54902006-05-14 15:33:57 -0400167 int use_stride = -1;
Theodore Ts'ofe073572005-12-29 15:39:40 -0500168#ifdef HAVE_FSTAT64
169 struct stat64 st_buf;
170#else
Theodore Ts'o116db1b2002-04-01 01:28:30 -0500171 struct stat st_buf;
Theodore Ts'ofe073572005-12-29 15:39:40 -0500172#endif
173 __s64 new_file_size;
Theodore Ts'o54434922003-12-07 01:28:50 -0500174 unsigned int sys_page_size = 4096;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -0400175 long sysval;
Theodore Ts'obf692352006-03-17 19:45:45 -0500176 int len, mount_flags;
177 char *mtpt;
Theodore Ts'oddc32a02003-05-03 18:45:55 -0400178
179#ifdef ENABLE_NLS
180 setlocale(LC_MESSAGES, "");
181 setlocale(LC_CTYPE, "");
182 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
183 textdomain(NLS_CAT_NAME);
Theodore Ts'o9d4507c2011-10-05 01:00:30 -0400184 set_com_err_gettext(gettext);
Theodore Ts'oddc32a02003-05-03 18:45:55 -0400185#endif
186
Theodore Ts'oa6d83022006-12-26 03:38:07 -0500187 add_error_table(&et_ext2_error_table);
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000188
Theodore Ts'of35fd3d2005-05-09 16:22:17 -0400189 fprintf (stderr, "resize2fs %s (%s)\n",
Theodore Ts'oba0af751998-03-09 17:41:53 +0000190 E2FSPROGS_VERSION, E2FSPROGS_DATE);
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000191 if (argc && *argv)
192 program_name = *argv;
Theodore Ts'o2e561e01998-04-08 06:18:37 +0000193
Josef Bacik199ddaa2008-03-13 13:46:10 -0400194 while ((c = getopt (argc, argv, "d:fFhMPpS:")) != EOF) {
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000195 switch (c) {
196 case 'h':
197 usage(program_name);
198 break;
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000199 case 'f':
200 force = 1;
201 break;
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000202 case 'F':
203 flush = 1;
204 break;
Josef Bacik199ddaa2008-03-13 13:46:10 -0400205 case 'M':
206 force_min_size = 1;
207 break;
208 case 'P':
209 print_min_size = 1;
210 break;
Theodore Ts'o05e112a1997-06-14 07:28:44 +0000211 case 'd':
212 flags |= atoi(optarg);
213 break;
214 case 'p':
215 flags |= RESIZE_PERCENT_COMPLETE;
216 break;
Theodore Ts'o46c54902006-05-14 15:33:57 -0400217 case 'S':
218 use_stride = atoi(optarg);
219 break;
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000220 default:
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000221 usage(program_name);
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000222 }
223 }
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000224 if (optind == argc)
225 usage(program_name);
Theodore Ts'o2e561e01998-04-08 06:18:37 +0000226
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000227 device_name = argv[optind++];
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -0500228 if (optind < argc)
229 new_size_str = argv[optind++];
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000230 if (optind < argc)
231 usage(program_name);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400232
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500233 io_options = strchr(device_name, '?');
234 if (io_options)
235 *io_options++ = 0;
236
Theodore Ts'obf692352006-03-17 19:45:45 -0500237 /*
238 * Figure out whether or not the device is mounted, and if it is
239 * where it is mounted.
240 */
241 len=80;
242 while (1) {
243 mtpt = malloc(len);
244 if (!mtpt)
245 return ENOMEM;
246 mtpt[len-1] = 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400247 retval = ext2fs_check_mount_point(device_name, &mount_flags,
Theodore Ts'obf692352006-03-17 19:45:45 -0500248 mtpt, len);
249 if (retval) {
250 com_err("ext2fs_check_mount_point", retval,
251 _("while determining whether %s is mounted."),
252 device_name);
253 exit(1);
254 }
Theodore Ts'o3a4d9862009-07-02 13:54:22 -0400255 if (!(mount_flags & EXT2_MF_MOUNTED) || (mtpt[len-1] == 0))
Theodore Ts'obf692352006-03-17 19:45:45 -0500256 break;
257 free(mtpt);
258 len = 2 * len;
259 }
Theodore Ts'ofe073572005-12-29 15:39:40 -0500260
Lukas Czernerc859cb12011-09-15 23:44:48 -0400261 fd = ext2fs_open_file(device_name, O_RDWR);
Theodore Ts'ofe073572005-12-29 15:39:40 -0500262 if (fd < 0) {
263 com_err("open", errno, _("while opening %s"),
264 device_name);
265 exit(1);
266 }
267
268#ifdef HAVE_FSTAT64
269 ret = fstat64(fd, &st_buf);
270#else
271 ret = fstat(fd, &st_buf);
272#endif
273 if (ret < 0) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400274 com_err("open", errno,
Theodore Ts'ofe073572005-12-29 15:39:40 -0500275 _("while getting stat information for %s"),
276 device_name);
277 exit(1);
278 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400279
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000280 if (flush) {
Theodore Ts'o48e08e02001-01-11 16:11:11 +0000281 retval = ext2fs_sync_device(fd, 1);
282 if (retval) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400283 com_err(argv[0], retval,
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000284 _("while trying to flush %s"),
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000285 device_name);
286 exit(1);
287 }
Theodore Ts'ofe073572005-12-29 15:39:40 -0500288 }
289
290 if (!S_ISREG(st_buf.st_mode )) {
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000291 close(fd);
Theodore Ts'ofe073572005-12-29 15:39:40 -0500292 fd = -1;
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000293 }
294
Theodore Ts'of38cf3c2008-09-01 11:17:29 -0400295#ifdef CONFIG_TESTIO_DEBUG
296 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
Theodore Ts'o05e112a1997-06-14 07:28:44 +0000297 io_ptr = test_io_manager;
298 test_io_backing_manager = unix_io_manager;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400299 } else
Theodore Ts'of38cf3c2008-09-01 11:17:29 -0400300#endif
Theodore Ts'o05e112a1997-06-14 07:28:44 +0000301 io_ptr = unix_io_manager;
302
Theodore Ts'o5d2ef122006-03-18 20:03:47 -0500303 if (!(mount_flags & EXT2_MF_MOUNTED))
304 io_flags = EXT2_FLAG_RW | EXT2_FLAG_EXCLUSIVE;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400305 retval = ext2fs_open2(device_name, io_options, io_flags,
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500306 0, 0, io_ptr, &fs);
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000307 if (retval) {
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000308 com_err (program_name, retval, _("while trying to open %s"),
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000309 device_name);
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000310 printf (_("Couldn't find valid filesystem superblock.\n"));
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000311 exit (1);
312 }
Jose R. Santos236efed2007-10-21 21:03:52 -0500313
Theodore Ts'o101c84f1998-03-24 16:27:11 +0000314 /*
315 * Check for compatibility with the feature sets. We need to
316 * be more stringent than ext2fs_open().
317 */
Theodore Ts'o76dd5e52002-10-30 23:07:21 -0500318 if (fs->super->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) {
Theodore Ts'o101c84f1998-03-24 16:27:11 +0000319 com_err(program_name, EXT2_ET_UNSUPP_FEATURE,
320 "(%s)", device_name);
321 exit(1);
322 }
Theodore Ts'oa8d632a2008-06-17 01:53:53 -0400323
324 /*
325 * XXXX The combination of flex_bg and !resize_inode causes
326 * major problems for resize2fs, since when the group descriptors
327 * grow in size this can potentially require multiple inode
328 * tables to be moved aside to make room, and resize2fs chokes
329 * rather badly in this scenario. It's a rare combination,
330 * except when a filesystem is expanded more than a certain
331 * size, so for now, we'll just prohibit that combination.
332 * This is something we should fix eventually, though.
333 */
334 if ((fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) &&
335 !(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
336 com_err(program_name, 0, _("%s: The combination of flex_bg "
337 "and\n\t!resize_inode features "
338 "is not supported by resize2fs.\n"),
339 device_name);
340 exit(1);
341 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400342
Eric Sandeen0d04d882009-05-18 17:11:28 -0500343 min_size = calculate_minimum_resize_size(fs);
344
Josef Bacik199ddaa2008-03-13 13:46:10 -0400345 if (print_min_size) {
Eric Sandeen8587d392010-07-12 13:27:44 -0500346 if (!force && ((fs->super->s_state & EXT2_ERROR_FS) ||
Eric Sandeen5fa92bc2010-03-31 14:28:42 -0500347 ((fs->super->s_state & EXT2_VALID_FS) == 0))) {
348 fprintf(stderr,
349 _("Please run 'e2fsck -f %s' first.\n\n"),
350 device_name);
351 exit(1);
352 }
Valerie Aurora Henson8728d502010-06-13 18:00:00 -0400353 printf(_("Estimated minimum size of the filesystem: %llu\n"),
Eric Sandeen0d04d882009-05-18 17:11:28 -0500354 min_size);
Josef Bacik199ddaa2008-03-13 13:46:10 -0400355 exit(0);
356 }
357
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -0400358 /* Determine the system page size if possible */
359#ifdef HAVE_SYSCONF
360#if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
361#define _SC_PAGESIZE _SC_PAGE_SIZE
362#endif
363#ifdef _SC_PAGESIZE
364 sysval = sysconf(_SC_PAGESIZE);
365 if (sysval > 0)
366 sys_page_size = sysval;
367#endif /* _SC_PAGESIZE */
368#endif /* HAVE_SYSCONF */
369
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000370 /*
371 * Get the size of the containing partition, and use this for
Theodore Ts'ofe073572005-12-29 15:39:40 -0500372 * defaults and for making sure the new filesystem doesn't
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000373 * exceed the partition size.
374 */
Valerie Aurora Henson8728d502010-06-13 18:00:00 -0400375 retval = ext2fs_get_device_size2(device_name, fs->blocksize,
376 &max_size);
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000377 if (retval) {
378 com_err(program_name, retval,
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000379 _("while trying to determine filesystem size"));
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000380 exit(1);
381 }
Josef Bacik199ddaa2008-03-13 13:46:10 -0400382 if (force_min_size)
Peng Tao1d8af182009-09-04 15:22:39 +0800383 new_size = min_size;
Josef Bacik199ddaa2008-03-13 13:46:10 -0400384 else if (new_size_str) {
Valerie Aurora Henson8728d502010-06-13 18:00:00 -0400385 new_size = parse_num_blocks2(new_size_str,
386 fs->super->s_log_block_size);
Eric Sandeen279a0002008-10-10 17:38:15 -0500387 if (new_size == 0) {
388 com_err(program_name, 0,
389 _("Invalid new size: %s\n"), new_size_str);
390 exit(1);
391 }
Theodore Ts'o55f4cbd2005-01-05 03:01:06 -0500392 } else {
Eric Sandeen248eb832010-12-15 22:37:34 -0600393 /* Take down devices exactly 16T to 2^32-1 blocks */
394 if (max_size == (1ULL << 32))
395 max_size--;
396 else if (max_size > (1ULL << 32)) {
397 com_err(program_name, 0, _("New size too large to be "
398 "expressed in 32 bits\n"));
399 exit(1);
400 }
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000401 new_size = max_size;
Theodore Ts'oa7ccdff2003-07-08 18:03:48 -0400402 /* Round down to an even multiple of a pagesize */
403 if (sys_page_size > fs->blocksize)
404 new_size &= ~((sys_page_size / fs->blocksize)-1);
405 }
Theodore Ts'o46c54902006-05-14 15:33:57 -0400406
Eric Sandeen0d04d882009-05-18 17:11:28 -0500407 if (!force && new_size < min_size) {
408 com_err(program_name, 0,
Valerie Aurora Henson8728d502010-06-13 18:00:00 -0400409 _("New size smaller than minimum (%llu)\n"), min_size);
Eric Sandeen0d04d882009-05-18 17:11:28 -0500410 exit(1);
411 }
Theodore Ts'o46c54902006-05-14 15:33:57 -0400412 if (use_stride >= 0) {
Andreas Dilgerde8f3a72007-05-25 11:18:11 -0400413 if (use_stride >= (int) fs->super->s_blocks_per_group) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400414 com_err(program_name, 0,
Theodore Ts'o46c54902006-05-14 15:33:57 -0400415 _("Invalid stride length"));
416 exit(1);
417 }
Theodore Ts'o96c6a3a2007-05-18 22:06:53 -0400418 fs->stride = fs->super->s_raid_stride = use_stride;
419 ext2fs_mark_super_dirty(fs);
Theodore Ts'o46c54902006-05-14 15:33:57 -0400420 } else
421 determine_fs_stride(fs);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400422
Theodore Ts'o116db1b2002-04-01 01:28:30 -0500423 /*
424 * If we are resizing a plain file, and it's not big enough,
425 * automatically extend it in a sparse fashion by writing the
426 * last requested block.
427 */
Theodore Ts'ofe073572005-12-29 15:39:40 -0500428 new_file_size = ((__u64) new_size) * fs->blocksize;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400429 if ((__u64) new_file_size >
Theodore Ts'ofe073572005-12-29 15:39:40 -0500430 (((__u64) 1) << (sizeof(st_buf.st_size)*8 - 1)) - 1)
431 fd = -1;
432 if ((new_file_size > st_buf.st_size) &&
433 (fd > 0)) {
434 if ((ext2fs_llseek(fd, new_file_size-1, SEEK_SET) >= 0) &&
435 (write(fd, "0", 1) == 1))
Theodore Ts'o116db1b2002-04-01 01:28:30 -0500436 max_size = new_size;
Theodore Ts'o116db1b2002-04-01 01:28:30 -0500437 }
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000438 if (!force && (new_size > max_size)) {
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000439 fprintf(stderr, _("The containing partition (or device)"
Valerie Aurora Henson8728d502010-06-13 18:00:00 -0400440 " is only %llu (%dk) blocks.\nYou requested a new size"
441 " of %llu blocks.\n\n"), max_size,
Theodore Ts'o792a0882003-05-13 23:32:59 -0400442 fs->blocksize / 1024, new_size);
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000443 exit(1);
444 }
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -0400445 if (new_size == ext2fs_blocks_count(fs->super)) {
Valerie Aurora Henson8728d502010-06-13 18:00:00 -0400446 fprintf(stderr, _("The filesystem is already %llu blocks "
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000447 "long. Nothing to do!\n\n"), new_size);
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000448 exit(0);
449 }
Theodore Ts'obf692352006-03-17 19:45:45 -0500450 if (mount_flags & EXT2_MF_MOUNTED) {
451 retval = online_resize_fs(fs, mtpt, &new_size, flags);
452 } else {
453 if (!force && ((fs->super->s_lastcheck < fs->super->s_mtime) ||
454 (fs->super->s_state & EXT2_ERROR_FS) ||
455 ((fs->super->s_state & EXT2_VALID_FS) == 0))) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400456 fprintf(stderr,
Theodore Ts'obf692352006-03-17 19:45:45 -0500457 _("Please run 'e2fsck -f %s' first.\n\n"),
458 device_name);
459 exit(1);
460 }
Theodore Ts'o96cdb372009-07-10 13:14:00 -0400461 printf(_("Resizing the filesystem on "
Valerie Aurora Henson8728d502010-06-13 18:00:00 -0400462 "%s to %llu (%dk) blocks.\n"),
Theodore Ts'obf692352006-03-17 19:45:45 -0500463 device_name, new_size, fs->blocksize / 1024);
464 retval = resize_fs(fs, &new_size, flags,
465 ((flags & RESIZE_PERCENT_COMPLETE) ?
466 resize_progress_func : 0));
Theodore Ts'of4b2a6d1998-02-21 04:20:44 +0000467 }
Theodore Ts'o3a4d9862009-07-02 13:54:22 -0400468 free(mtpt);
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000469 if (retval) {
Theodore Ts'oa13575f2000-06-12 22:06:16 +0000470 com_err(program_name, retval, _("while trying to resize %s"),
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000471 device_name);
Theodore Ts'o96cdb372009-07-10 13:14:00 -0400472 fprintf(stderr,
473 _("Please run 'e2fsck -fy %s' to fix the filesystem\n"
474 "after the aborted resize operation.\n"),
475 device_name);
476 ext2fs_close(fs);
Theodore Ts'o16082112002-04-09 12:46:19 -0400477 exit(1);
Theodore Ts'o1e1da291997-06-09 14:51:29 +0000478 }
Valerie Aurora Henson8728d502010-06-13 18:00:00 -0400479 printf(_("The filesystem on %s is now %llu blocks long.\n\n"),
Theodore Ts'o7e71e4c1998-09-30 00:54:35 +0000480 device_name, new_size);
Theodore Ts'ofe073572005-12-29 15:39:40 -0500481
482 if ((st_buf.st_size > new_file_size) &&
483 (fd > 0)) {
Theodore Ts'o261cd392008-01-21 13:45:00 -0500484#ifdef HAVE_FTRUNCATE64
Theodore Ts'ofb725562009-04-22 15:10:36 -0400485 retval = ftruncate64(fd, new_file_size);
Theodore Ts'ofe073572005-12-29 15:39:40 -0500486#else
Theodore Ts'ofb725562009-04-22 15:10:36 -0400487 retval = 0;
Theodore Ts'o98dca6c2008-01-21 13:43:18 -0500488 /* Only truncate if new_file_size doesn't overflow off_t */
489 if (((off_t) new_file_size) == new_file_size)
Theodore Ts'ofb725562009-04-22 15:10:36 -0400490 retval = ftruncate(fd, (off_t) new_file_size);
Theodore Ts'ofe073572005-12-29 15:39:40 -0500491#endif
Theodore Ts'ofb725562009-04-22 15:10:36 -0400492 if (retval)
493 com_err(program_name, retval,
494 _("while trying to truncate %s"),
495 device_name);
Theodore Ts'ofe073572005-12-29 15:39:40 -0500496 }
497 if (fd > 0)
498 close(fd);
Theodore Ts'oa6d83022006-12-26 03:38:07 -0500499 remove_error_table(&et_ext2_error_table);
Theodore Ts'o0a617312000-02-02 19:14:36 +0000500 return (0);
Theodore Ts'o24b2c7a1997-06-07 20:42:58 +0000501}