Whamcloud - gitweb
resize2fs: don't require fsck to print min size
[tools/e2fsprogs.git] / resize / main.c
1 /*
2  * main.c --- ext2 resizer main program
3  *
4  * Copyright (C) 1997, 1998 by Theodore Ts'o and
5  *      PowerQuest, Inc.
6  *
7  * Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004 by Theodore Ts'o
8  *
9  * %Begin-Header%
10  * This file may be redistributed under the terms of the GNU Public
11  * License.
12  * %End-Header%
13  */
14
15 #define _LARGEFILE_SOURCE
16 #define _LARGEFILE64_SOURCE
17
18 #include "config.h"
19 #ifdef HAVE_GETOPT_H
20 #include <getopt.h>
21 #else
22 extern char *optarg;
23 extern int optind;
24 #endif
25 #include <unistd.h>
26 #ifdef HAVE_STDLIB_H
27 #include <stdlib.h>
28 #endif
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32
33 #include "e2p/e2p.h"
34
35 #include "resize2fs.h"
36
37 #include "../version.h"
38
39 char *program_name;
40 static char *device_name, *io_options;
41
42 static void usage (char *prog)
43 {
44         fprintf (stderr, _("Usage: %s [-d debug_flags] [-f] [-F] [-M] [-P] "
45                            "[-p] device [new_size]\n\n"), prog);
46
47         exit (1);
48 }
49
50 static errcode_t resize_progress_func(ext2_resize_t rfs, int pass,
51                                       unsigned long cur, unsigned long max)
52 {
53         ext2_sim_progmeter progress;
54         const char      *label;
55         errcode_t       retval;
56
57         progress = (ext2_sim_progmeter) rfs->prog_data;
58         if (max == 0)
59                 return 0;
60         if (cur == 0) {
61                 if (progress)
62                         ext2fs_progress_close(progress);
63                 progress = 0;
64                 switch (pass) {
65                 case E2_RSZ_EXTEND_ITABLE_PASS:
66                         label = _("Extending the inode table");
67                         break;
68                 case E2_RSZ_BLOCK_RELOC_PASS:
69                         label = _("Relocating blocks");
70                         break;
71                 case E2_RSZ_INODE_SCAN_PASS:
72                         label = _("Scanning inode table");
73                         break;
74                 case E2_RSZ_INODE_REF_UPD_PASS:
75                         label = _("Updating inode references");
76                         break;
77                 case E2_RSZ_MOVE_ITABLE_PASS:
78                         label = _("Moving inode table");
79                         break;
80                 default:
81                         label = _("Unknown pass?!?");
82                         break;
83                 }
84                 printf(_("Begin pass %d (max = %lu)\n"), pass, max);
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         }
99         return 0;
100 }
101
102 static void determine_fs_stride(ext2_filsys fs)
103 {
104         unsigned int    group;
105         unsigned long long sum;
106         unsigned int    has_sb, prev_has_sb = 0, num;
107         int             i_stride, b_stride;
108
109         if (fs->stride)
110                 return;
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;
116                 b_stride = ext2fs_block_bitmap_loc(fs, group) -
117                         ext2fs_block_bitmap_loc(fs, group - 1) -
118                         fs->super->s_blocks_per_group;
119                 i_stride = ext2fs_inode_bitmap_loc(fs, group) -
120                         ext2fs_inode_bitmap_loc(fs, group - 1) -
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++;
129
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
142         fs->super->s_raid_stride = fs->stride;
143         ext2fs_mark_super_dirty(fs);
144
145 #if 0
146         if (fs->stride)
147                 printf("Using RAID stride of %d\n", fs->stride);
148 #endif
149 }
150
151 static void bigalloc_check(ext2_filsys fs, int force)
152 {
153         if (!force && EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
154                                 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
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"));
159                 exit(1);
160         }
161 }
162
163 int main (int argc, char ** argv)
164 {
165         errcode_t       retval;
166         ext2_filsys     fs;
167         int             c;
168         int             flags = 0;
169         int             flush = 0;
170         int             force = 0;
171         int             io_flags = 0;
172         int             force_min_size = 0;
173         int             print_min_size = 0;
174         int             fd, ret;
175         blk64_t         new_size = 0;
176         blk64_t         max_size = 0;
177         blk64_t         min_size = 0;
178         io_manager      io_ptr;
179         char            *new_size_str = 0;
180         int             use_stride = -1;
181         ext2fs_struct_stat st_buf;
182         __s64           new_file_size;
183         unsigned int    sys_page_size = 4096;
184         unsigned int    blocksize;
185         long            sysval;
186         int             len, mount_flags;
187         char            *mtpt;
188
189 #ifdef ENABLE_NLS
190         setlocale(LC_MESSAGES, "");
191         setlocale(LC_CTYPE, "");
192         bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
193         textdomain(NLS_CAT_NAME);
194         set_com_err_gettext(gettext);
195 #endif
196
197         add_error_table(&et_ext2_error_table);
198
199         fprintf (stderr, "resize2fs %s (%s)\n",
200                  E2FSPROGS_VERSION, E2FSPROGS_DATE);
201         if (argc && *argv)
202                 program_name = *argv;
203
204         while ((c = getopt (argc, argv, "d:fFhMPpS:")) != EOF) {
205                 switch (c) {
206                 case 'h':
207                         usage(program_name);
208                         break;
209                 case 'f':
210                         force = 1;
211                         break;
212                 case 'F':
213                         flush = 1;
214                         break;
215                 case 'M':
216                         force_min_size = 1;
217                         break;
218                 case 'P':
219                         print_min_size = 1;
220                         break;
221                 case 'd':
222                         flags |= atoi(optarg);
223                         break;
224                 case 'p':
225                         flags |= RESIZE_PERCENT_COMPLETE;
226                         break;
227                 case 'S':
228                         use_stride = atoi(optarg);
229                         break;
230                 default:
231                         usage(program_name);
232                 }
233         }
234         if (optind == argc)
235                 usage(program_name);
236
237         device_name = argv[optind++];
238         if (optind < argc)
239                 new_size_str = argv[optind++];
240         if (optind < argc)
241                 usage(program_name);
242
243         io_options = strchr(device_name, '?');
244         if (io_options)
245                 *io_options++ = 0;
246
247         /*
248          * Figure out whether or not the device is mounted, and if it is
249          * where it is mounted.
250          */
251         len=80;
252         while (1) {
253                 mtpt = malloc(len);
254                 if (!mtpt)
255                         return ENOMEM;
256                 mtpt[len-1] = 0;
257                 retval = ext2fs_check_mount_point(device_name, &mount_flags,
258                                                   mtpt, len);
259                 if (retval) {
260                         com_err("ext2fs_check_mount_point", retval,
261                                 _("while determining whether %s is mounted."),
262                                 device_name);
263                         exit(1);
264                 }
265                 if (!(mount_flags & EXT2_MF_MOUNTED) || (mtpt[len-1] == 0))
266                         break;
267                 free(mtpt);
268                 len = 2 * len;
269         }
270
271         fd = ext2fs_open_file(device_name, O_RDWR, 0);
272         if (fd < 0) {
273                 com_err("open", errno, _("while opening %s"),
274                         device_name);
275                 exit(1);
276         }
277
278         ret = ext2fs_fstat(fd, &st_buf);
279         if (ret < 0) {
280                 com_err("open", errno,
281                         _("while getting stat information for %s"),
282                         device_name);
283                 exit(1);
284         }
285
286         if (flush) {
287                 retval = ext2fs_sync_device(fd, 1);
288                 if (retval) {
289                         com_err(argv[0], retval,
290                                 _("while trying to flush %s"),
291                                 device_name);
292                         exit(1);
293                 }
294         }
295
296         if (!S_ISREG(st_buf.st_mode )) {
297                 close(fd);
298                 fd = -1;
299         }
300
301 #ifdef CONFIG_TESTIO_DEBUG
302         if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
303                 io_ptr = test_io_manager;
304                 test_io_backing_manager = unix_io_manager;
305         } else
306 #endif
307                 io_ptr = unix_io_manager;
308
309         if (!(mount_flags & EXT2_MF_MOUNTED))
310                 io_flags = EXT2_FLAG_RW | EXT2_FLAG_EXCLUSIVE;
311
312         io_flags |= EXT2_FLAG_64BITS;
313
314         retval = ext2fs_open2(device_name, io_options, io_flags,
315                               0, 0, io_ptr, &fs);
316         if (retval) {
317                 com_err(program_name, retval, _("while trying to open %s"),
318                         device_name);
319                 printf("%s", _("Couldn't find valid filesystem superblock.\n"));
320                 exit (1);
321         }
322         fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
323
324         /*
325          * Before acting on an unmounted filesystem, make sure it's ok,
326          * unless the user is forcing it.
327          *
328          * We do ERROR and VALID checks even if we're only printing the
329          * minimimum size, because traversal of a badly damaged filesystem
330          * can cause issues as well.  We don't require it to be fscked after
331          * the last mount time in this case, though, as this is a bit less
332          * risky.
333          */
334         if (!force && !(mount_flags & EXT2_MF_MOUNTED)) {
335                 int checkit = 0;
336
337                 if (fs->super->s_state & EXT2_ERROR_FS)
338                         checkit = 1;
339
340                 if ((fs->super->s_state & EXT2_VALID_FS) == 0)
341                         checkit = 1;
342
343                 if ((fs->super->s_lastcheck < fs->super->s_mtime) &&
344                     !print_min_size)
345                         checkit = 1;
346
347                 if (checkit) {
348                         fprintf(stderr,
349                                 _("Please run 'e2fsck -f %s' first.\n\n"),
350                                 device_name);
351                         exit(1);
352                 }
353         }
354
355         /*
356          * Check for compatibility with the feature sets.  We need to
357          * be more stringent than ext2fs_open().
358          */
359         if (fs->super->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) {
360                 com_err(program_name, EXT2_ET_UNSUPP_FEATURE,
361                         "(%s)", device_name);
362                 exit(1);
363         }
364
365         min_size = calculate_minimum_resize_size(fs, flags);
366
367         if (print_min_size) {
368                 printf(_("Estimated minimum size of the filesystem: %llu\n"),
369                        min_size);
370                 exit(0);
371         }
372
373         /* Determine the system page size if possible */
374 #ifdef HAVE_SYSCONF
375 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
376 #define _SC_PAGESIZE _SC_PAGE_SIZE
377 #endif
378 #ifdef _SC_PAGESIZE
379         sysval = sysconf(_SC_PAGESIZE);
380         if (sysval > 0)
381                 sys_page_size = sysval;
382 #endif /* _SC_PAGESIZE */
383 #endif /* HAVE_SYSCONF */
384
385         /*
386          * Get the size of the containing partition, and use this for
387          * defaults and for making sure the new filesystem doesn't
388          * exceed the partition size.
389          */
390         blocksize = fs->blocksize;
391         retval = ext2fs_get_device_size2(device_name, blocksize,
392                                          &max_size);
393         if (retval) {
394                 com_err(program_name, retval, "%s",
395                         _("while trying to determine filesystem size"));
396                 exit(1);
397         }
398         if (force_min_size)
399                 new_size = min_size;
400         else if (new_size_str) {
401                 new_size = parse_num_blocks2(new_size_str,
402                                              fs->super->s_log_block_size);
403                 if (new_size == 0) {
404                         com_err(program_name, 0,
405                                 _("Invalid new size: %s\n"), new_size_str);
406                         exit(1);
407                 }
408         } else {
409                 new_size = max_size;
410                 /* Round down to an even multiple of a pagesize */
411                 if (sys_page_size > blocksize)
412                         new_size &= ~((sys_page_size / blocksize)-1);
413         }
414         if (!EXT2_HAS_INCOMPAT_FEATURE(fs->super,
415                                        EXT4_FEATURE_INCOMPAT_64BIT)) {
416                 /* Take 16T down to 2^32-1 blocks */
417                 if (new_size == (1ULL << 32))
418                         new_size--;
419                 else if (new_size > (1ULL << 32)) {
420                         com_err(program_name, 0, "%s",
421                                 _("New size too large to be "
422                                   "expressed in 32 bits\n"));
423                         exit(1);
424                 }
425         }
426
427         if (!force && new_size < min_size) {
428                 com_err(program_name, 0,
429                         _("New size smaller than minimum (%llu)\n"), min_size);
430                 exit(1);
431         }
432         if (use_stride >= 0) {
433                 if (use_stride >= (int) fs->super->s_blocks_per_group) {
434                         com_err(program_name, 0, "%s",
435                                 _("Invalid stride length"));
436                         exit(1);
437                 }
438                 fs->stride = fs->super->s_raid_stride = use_stride;
439                 ext2fs_mark_super_dirty(fs);
440         } else
441                   determine_fs_stride(fs);
442
443         /*
444          * If we are resizing a plain file, and it's not big enough,
445          * automatically extend it in a sparse fashion by writing the
446          * last requested block.
447          */
448         new_file_size = ((__u64) new_size) * blocksize;
449         if ((__u64) new_file_size >
450             (((__u64) 1) << (sizeof(st_buf.st_size)*8 - 1)) - 1)
451                 fd = -1;
452         if ((new_file_size > st_buf.st_size) &&
453             (fd > 0)) {
454                 if ((ext2fs_llseek(fd, new_file_size-1, SEEK_SET) >= 0) &&
455                     (write(fd, "0", 1) == 1))
456                         max_size = new_size;
457         }
458         if (!force && (new_size > max_size)) {
459                 fprintf(stderr, _("The containing partition (or device)"
460                         " is only %llu (%dk) blocks.\nYou requested a new size"
461                         " of %llu blocks.\n\n"), max_size,
462                         blocksize / 1024, new_size);
463                 exit(1);
464         }
465         if (new_size == ext2fs_blocks_count(fs->super)) {
466                 fprintf(stderr, _("The filesystem is already %llu (%dk) "
467                         "blocks long.  Nothing to do!\n\n"), new_size,
468                         blocksize / 1024);
469                 exit(0);
470         }
471         if (mount_flags & EXT2_MF_MOUNTED) {
472                 bigalloc_check(fs, force);
473                 retval = online_resize_fs(fs, mtpt, &new_size, flags);
474         } else {
475                 bigalloc_check(fs, force);
476                 printf(_("Resizing the filesystem on "
477                          "%s to %llu (%dk) blocks.\n"),
478                        device_name, new_size, blocksize / 1024);
479                 retval = resize_fs(fs, &new_size, flags,
480                                    ((flags & RESIZE_PERCENT_COMPLETE) ?
481                                     resize_progress_func : 0));
482         }
483         free(mtpt);
484         if (retval) {
485                 com_err(program_name, retval, _("while trying to resize %s"),
486                         device_name);
487                 fprintf(stderr,
488                         _("Please run 'e2fsck -fy %s' to fix the filesystem\n"
489                           "after the aborted resize operation.\n"),
490                         device_name);
491                 ext2fs_close_free(&fs);
492                 exit(1);
493         }
494         printf(_("The filesystem on %s is now %llu (%dk) blocks long.\n\n"),
495                device_name, new_size, blocksize / 1024);
496
497         if ((st_buf.st_size > new_file_size) &&
498             (fd > 0)) {
499 #ifdef HAVE_FTRUNCATE64
500                 retval = ftruncate64(fd, new_file_size);
501 #else
502                 retval = 0;
503                 /* Only truncate if new_file_size doesn't overflow off_t */
504                 if (((off_t) new_file_size) == new_file_size)
505                         retval = ftruncate(fd, (off_t) new_file_size);
506 #endif
507                 if (retval)
508                         com_err(program_name, retval,
509                                 _("while trying to truncate %s"),
510                                 device_name);
511         }
512         if (fd > 0)
513                 close(fd);
514         remove_error_table(&et_ext2_error_table);
515         return (0);
516 }