Whamcloud - gitweb
f25df40596f21dc97245e6886328af9e2833a047
[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 #ifndef _LARGEFILE_SOURCE
16 #define _LARGEFILE_SOURCE
17 #endif
18 #ifndef _LARGEFILE64_SOURCE
19 #define _LARGEFILE64_SOURCE
20 #endif
21
22 #include "config.h"
23 #ifdef HAVE_GETOPT_H
24 #include <getopt.h>
25 #else
26 extern char *optarg;
27 extern int optind;
28 #endif
29 #include <unistd.h>
30 #ifdef HAVE_STDLIB_H
31 #include <stdlib.h>
32 #endif
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <fcntl.h>
36 #include <libgen.h>
37
38 #include "e2p/e2p.h"
39
40 #include "resize2fs.h"
41
42 #include "../version.h"
43
44 char *program_name;
45 static char *device_name, *io_options;
46
47 static void usage (char *prog)
48 {
49         fprintf (stderr, _("Usage: %s [-d debug_flags] [-f] [-F] [-M] [-P] "
50                            "[-p] device [-b|-s|new_size] [-z undo_file]\n\n"),
51                  prog);
52
53         exit (1);
54 }
55
56 static errcode_t resize_progress_func(ext2_resize_t rfs, int pass,
57                                       unsigned long cur, unsigned long max)
58 {
59         ext2_sim_progmeter progress;
60         const char      *label;
61         errcode_t       retval;
62
63         progress = (ext2_sim_progmeter) rfs->prog_data;
64         if (max == 0)
65                 return 0;
66         if (cur == 0) {
67                 if (progress)
68                         ext2fs_progress_close(progress);
69                 progress = 0;
70                 switch (pass) {
71                 case E2_RSZ_EXTEND_ITABLE_PASS:
72                         label = _("Extending the inode table");
73                         break;
74                 case E2_RSZ_BLOCK_RELOC_PASS:
75                         label = _("Relocating blocks");
76                         break;
77                 case E2_RSZ_INODE_SCAN_PASS:
78                         label = _("Scanning inode table");
79                         break;
80                 case E2_RSZ_INODE_REF_UPD_PASS:
81                         label = _("Updating inode references");
82                         break;
83                 case E2_RSZ_MOVE_ITABLE_PASS:
84                         label = _("Moving inode table");
85                         break;
86                 default:
87                         label = _("Unknown pass?!?");
88                         break;
89                 }
90                 printf(_("Begin pass %d (max = %lu)\n"), pass, max);
91                 retval = ext2fs_progress_init(&progress, label, 30,
92                                               40, max, 0);
93                 if (retval)
94                         progress = 0;
95                 rfs->prog_data = (void *) progress;
96         }
97         if (progress)
98                 ext2fs_progress_update(progress, cur);
99         if (cur >= max) {
100                 if (progress)
101                         ext2fs_progress_close(progress);
102                 progress = 0;
103                 rfs->prog_data = 0;
104         }
105         return 0;
106 }
107
108 static void determine_fs_stride(ext2_filsys fs)
109 {
110         unsigned int    group;
111         unsigned long long sum;
112         unsigned int    has_sb, prev_has_sb = 0, num;
113         int             i_stride, b_stride;
114         int             flexbg_size = 1 << fs->super->s_log_groups_per_flex;
115
116         if (fs->stride)
117                 return;
118         num = 0; sum = 0;
119         for (group = 0; group < fs->group_desc_count; group++) {
120                 has_sb = ext2fs_bg_has_super(fs, group);
121                 if (group == 0 || has_sb != prev_has_sb)
122                         goto next;
123                 b_stride = ext2fs_block_bitmap_loc(fs, group) -
124                         ext2fs_block_bitmap_loc(fs, group - 1) -
125                         fs->super->s_blocks_per_group;
126                 i_stride = ext2fs_inode_bitmap_loc(fs, group) -
127                         ext2fs_inode_bitmap_loc(fs, group - 1) -
128                         fs->super->s_blocks_per_group;
129                 if (b_stride != i_stride ||
130                     b_stride < 0 ||
131                     (flexbg_size > 1 && (group % flexbg_size == 0)))
132                         goto next;
133
134                 /* printf("group %d has stride %d\n", group, b_stride); */
135                 sum += b_stride;
136                 num++;
137
138         next:
139                 prev_has_sb = has_sb;
140         }
141
142         if (fs->group_desc_count > 12 && num < 3)
143                 sum = 0;
144
145         if (num)
146                 fs->stride = sum / num;
147         else
148                 fs->stride = 0;
149
150         fs->super->s_raid_stride = fs->stride;
151         ext2fs_mark_super_dirty(fs);
152
153 #if 0
154         if (fs->stride)
155                 printf("Using RAID stride of %d\n", fs->stride);
156 #endif
157 }
158
159 static void bigalloc_check(ext2_filsys fs, int force)
160 {
161         if (!force && ext2fs_has_feature_bigalloc(fs->super)) {
162                 fprintf(stderr, "%s", _("\nResizing bigalloc file systems has "
163                                         "not been fully tested.  Proceed at\n"
164                                         "your own risk!  Use the force option "
165                                         "if you want to go ahead anyway.\n\n"));
166                 exit(1);
167         }
168 }
169
170 static int resize2fs_setup_tdb(const char *device_name, char *undo_file,
171                                io_manager *io_ptr)
172 {
173         errcode_t retval = ENOMEM;
174         char *tdb_dir = NULL, *tdb_file = NULL;
175         char *dev_name, *tmp_name;
176
177         /* (re)open a specific undo file */
178         if (undo_file && undo_file[0] != 0) {
179                 retval = set_undo_io_backing_manager(*io_ptr);
180                 if (retval)
181                         goto err;
182                 *io_ptr = undo_io_manager;
183                 retval = set_undo_io_backup_file(undo_file);
184                 if (retval)
185                         goto err;
186                 printf(_("Overwriting existing filesystem; this can be undone "
187                          "using the command:\n"
188                          "    e2undo %s %s\n\n"),
189                         undo_file, device_name);
190                 return retval;
191         }
192
193         /*
194          * Configuration via a conf file would be
195          * nice
196          */
197         tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
198         if (!tdb_dir)
199                 tdb_dir = "/var/lib/e2fsprogs";
200
201         if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
202             access(tdb_dir, W_OK))
203                 return 0;
204
205         tmp_name = strdup(device_name);
206         if (!tmp_name)
207                 goto errout;
208         dev_name = basename(tmp_name);
209         tdb_file = malloc(strlen(tdb_dir) + 11 + strlen(dev_name) + 7 + 1);
210         if (!tdb_file) {
211                 free(tmp_name);
212                 goto errout;
213         }
214         sprintf(tdb_file, "%s/resize2fs-%s.e2undo", tdb_dir, dev_name);
215         free(tmp_name);
216
217         if ((unlink(tdb_file) < 0) && (errno != ENOENT)) {
218                 retval = errno;
219                 com_err(program_name, retval,
220                         _("while trying to delete %s"), tdb_file);
221                 goto errout;
222         }
223
224         retval = set_undo_io_backing_manager(*io_ptr);
225         if (retval)
226                 goto errout;
227         *io_ptr = undo_io_manager;
228         retval = set_undo_io_backup_file(tdb_file);
229         if (retval)
230                 goto errout;
231         printf(_("Overwriting existing filesystem; this can be undone "
232                  "using the command:\n"
233                  "    e2undo %s %s\n\n"), tdb_file, device_name);
234
235         free(tdb_file);
236         return 0;
237 errout:
238         free(tdb_file);
239 err:
240         com_err(program_name, retval, "%s",
241                 _("while trying to setup undo file\n"));
242         return retval;
243 }
244
245 int main (int argc, char ** argv)
246 {
247         errcode_t       retval;
248         ext2_filsys     fs;
249         int             c;
250         int             flags = 0;
251         int             flush = 0;
252         int             force = 0;
253         int             io_flags = 0;
254         int             force_min_size = 0;
255         int             print_min_size = 0;
256         int             fd, ret;
257         blk64_t         new_size = 0;
258         blk64_t         max_size = 0;
259         blk64_t         min_size = 0;
260         io_manager      io_ptr;
261         char            *new_size_str = 0;
262         int             use_stride = -1;
263         ext2fs_struct_stat st_buf;
264         __s64           new_file_size;
265         unsigned int    sys_page_size = 4096;
266         unsigned int    blocksize;
267         long            sysval;
268         int             len, mount_flags;
269         char            *mtpt, *undo_file = NULL;
270
271 #ifdef ENABLE_NLS
272         setlocale(LC_MESSAGES, "");
273         setlocale(LC_CTYPE, "");
274         bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
275         textdomain(NLS_CAT_NAME);
276         set_com_err_gettext(gettext);
277 #endif
278
279         add_error_table(&et_ext2_error_table);
280
281         fprintf (stderr, "resize2fs %s (%s)\n",
282                  E2FSPROGS_VERSION, E2FSPROGS_DATE);
283         if (argc && *argv)
284                 program_name = *argv;
285
286         while ((c = getopt(argc, argv, "d:fFhMPpS:bsz:")) != EOF) {
287                 switch (c) {
288                 case 'h':
289                         usage(program_name);
290                         break;
291                 case 'f':
292                         force = 1;
293                         break;
294                 case 'F':
295                         flush = 1;
296                         break;
297                 case 'M':
298                         force_min_size = 1;
299                         break;
300                 case 'P':
301                         print_min_size = 1;
302                         break;
303                 case 'd':
304                         flags |= atoi(optarg);
305                         break;
306                 case 'p':
307                         flags |= RESIZE_PERCENT_COMPLETE;
308                         break;
309                 case 'S':
310                         use_stride = atoi(optarg);
311                         break;
312                 case 'b':
313                         flags |= RESIZE_ENABLE_64BIT;
314                         break;
315                 case 's':
316                         flags |= RESIZE_DISABLE_64BIT;
317                         break;
318                 case 'z':
319                         undo_file = optarg;
320                         break;
321                 default:
322                         usage(program_name);
323                 }
324         }
325         if (optind == argc)
326                 usage(program_name);
327
328         device_name = argv[optind++];
329         if (optind < argc)
330                 new_size_str = argv[optind++];
331         if (optind < argc)
332                 usage(program_name);
333
334         io_options = strchr(device_name, '?');
335         if (io_options)
336                 *io_options++ = 0;
337
338         /*
339          * Figure out whether or not the device is mounted, and if it is
340          * where it is mounted.
341          */
342         len=80;
343         while (1) {
344                 mtpt = malloc(len);
345                 if (!mtpt)
346                         return ENOMEM;
347                 mtpt[len-1] = 0;
348                 retval = ext2fs_check_mount_point(device_name, &mount_flags,
349                                                   mtpt, len);
350                 if (retval) {
351                         com_err("ext2fs_check_mount_point", retval,
352                                 _("while determining whether %s is mounted."),
353                                 device_name);
354                         exit(1);
355                 }
356                 if (!(mount_flags & EXT2_MF_MOUNTED) || (mtpt[len-1] == 0))
357                         break;
358                 free(mtpt);
359                 len = 2 * len;
360         }
361
362         fd = ext2fs_open_file(device_name, O_RDWR, 0);
363         if (fd < 0) {
364                 com_err("open", errno, _("while opening %s"),
365                         device_name);
366                 exit(1);
367         }
368
369         ret = ext2fs_fstat(fd, &st_buf);
370         if (ret < 0) {
371                 com_err("open", errno,
372                         _("while getting stat information for %s"),
373                         device_name);
374                 exit(1);
375         }
376
377         if (flush) {
378                 retval = ext2fs_sync_device(fd, 1);
379                 if (retval) {
380                         com_err(argv[0], retval,
381                                 _("while trying to flush %s"),
382                                 device_name);
383                         exit(1);
384                 }
385         }
386
387         if (!S_ISREG(st_buf.st_mode )) {
388                 close(fd);
389                 fd = -1;
390         }
391
392 #ifdef CONFIG_TESTIO_DEBUG
393         if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
394                 io_ptr = test_io_manager;
395                 test_io_backing_manager = unix_io_manager;
396         } else
397 #endif
398                 io_ptr = unix_io_manager;
399
400         if (!(mount_flags & EXT2_MF_MOUNTED))
401                 io_flags = EXT2_FLAG_RW | EXT2_FLAG_EXCLUSIVE;
402
403         io_flags |= EXT2_FLAG_64BITS;
404         if (undo_file) {
405                 retval = resize2fs_setup_tdb(device_name, undo_file, &io_ptr);
406                 if (retval)
407                         exit(1);
408         }
409         retval = ext2fs_open2(device_name, io_options, io_flags,
410                               0, 0, io_ptr, &fs);
411         if (retval) {
412                 com_err(program_name, retval, _("while trying to open %s"),
413                         device_name);
414                 printf("%s", _("Couldn't find valid filesystem superblock.\n"));
415                 exit (1);
416         }
417         fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
418
419         /*
420          * Before acting on an unmounted filesystem, make sure it's ok,
421          * unless the user is forcing it.
422          *
423          * We do ERROR and VALID checks even if we're only printing the
424          * minimimum size, because traversal of a badly damaged filesystem
425          * can cause issues as well.  We don't require it to be fscked after
426          * the last mount time in this case, though, as this is a bit less
427          * risky.
428          */
429         if (!force && !(mount_flags & EXT2_MF_MOUNTED)) {
430                 int checkit = 0;
431
432                 if (fs->super->s_state & EXT2_ERROR_FS)
433                         checkit = 1;
434
435                 if ((fs->super->s_state & EXT2_VALID_FS) == 0)
436                         checkit = 1;
437
438                 if ((fs->super->s_lastcheck < fs->super->s_mtime) &&
439                     !print_min_size)
440                         checkit = 1;
441
442                 if (checkit) {
443                         fprintf(stderr,
444                                 _("Please run 'e2fsck -f %s' first.\n\n"),
445                                 device_name);
446                         exit(1);
447                 }
448         }
449
450         /*
451          * Check for compatibility with the feature sets.  We need to
452          * be more stringent than ext2fs_open().
453          */
454         if (fs->super->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) {
455                 com_err(program_name, EXT2_ET_UNSUPP_FEATURE,
456                         "(%s)", device_name);
457                 exit(1);
458         }
459
460         min_size = calculate_minimum_resize_size(fs, flags);
461
462         if (print_min_size) {
463                 printf(_("Estimated minimum size of the filesystem: %llu\n"),
464                        min_size);
465                 exit(0);
466         }
467
468         /* Determine the system page size if possible */
469 #ifdef HAVE_SYSCONF
470 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
471 #define _SC_PAGESIZE _SC_PAGE_SIZE
472 #endif
473 #ifdef _SC_PAGESIZE
474         sysval = sysconf(_SC_PAGESIZE);
475         if (sysval > 0)
476                 sys_page_size = sysval;
477 #endif /* _SC_PAGESIZE */
478 #endif /* HAVE_SYSCONF */
479
480         /*
481          * Get the size of the containing partition, and use this for
482          * defaults and for making sure the new filesystem doesn't
483          * exceed the partition size.
484          */
485         blocksize = fs->blocksize;
486         retval = ext2fs_get_device_size2(device_name, blocksize,
487                                          &max_size);
488         if (retval) {
489                 com_err(program_name, retval, "%s",
490                         _("while trying to determine filesystem size"));
491                 exit(1);
492         }
493         if (force_min_size)
494                 new_size = min_size;
495         else if (new_size_str) {
496                 new_size = parse_num_blocks2(new_size_str,
497                                              fs->super->s_log_block_size);
498                 if (new_size == 0) {
499                         com_err(program_name, 0,
500                                 _("Invalid new size: %s\n"), new_size_str);
501                         exit(1);
502                 }
503         } else {
504                 new_size = max_size;
505                 /* Round down to an even multiple of a pagesize */
506                 if (sys_page_size > blocksize)
507                         new_size &= ~((sys_page_size / blocksize)-1);
508         }
509         /* If changing 64bit, don't change the filesystem size. */
510         if (flags & (RESIZE_DISABLE_64BIT | RESIZE_ENABLE_64BIT)) {
511                 new_size = ext2fs_blocks_count(fs->super);
512         }
513         if (!ext2fs_has_feature_64bit(fs->super)) {
514                 /* Take 16T down to 2^32-1 blocks */
515                 if (new_size == (1ULL << 32))
516                         new_size--;
517                 else if (new_size > (1ULL << 32)) {
518                         com_err(program_name, 0, "%s",
519                                 _("New size too large to be "
520                                   "expressed in 32 bits\n"));
521                         exit(1);
522                 }
523         }
524
525         if (!force && new_size < min_size) {
526                 com_err(program_name, 0,
527                         _("New size smaller than minimum (%llu)\n"), min_size);
528                 exit(1);
529         }
530         if (use_stride >= 0) {
531                 if (use_stride >= (int) fs->super->s_blocks_per_group) {
532                         com_err(program_name, 0, "%s",
533                                 _("Invalid stride length"));
534                         exit(1);
535                 }
536                 fs->stride = fs->super->s_raid_stride = use_stride;
537                 ext2fs_mark_super_dirty(fs);
538         } else
539                   determine_fs_stride(fs);
540
541         /*
542          * If we are resizing a plain file, and it's not big enough,
543          * automatically extend it in a sparse fashion by writing the
544          * last requested block.
545          */
546         new_file_size = ((__u64) new_size) * blocksize;
547         if ((__u64) new_file_size >
548             (((__u64) 1) << (sizeof(st_buf.st_size)*8 - 1)) - 1)
549                 fd = -1;
550         if ((new_file_size > st_buf.st_size) &&
551             (fd > 0)) {
552                 if ((ext2fs_llseek(fd, new_file_size-1, SEEK_SET) >= 0) &&
553                     (write(fd, "0", 1) == 1))
554                         max_size = new_size;
555         }
556         if (!force && (new_size > max_size)) {
557                 fprintf(stderr, _("The containing partition (or device)"
558                         " is only %llu (%dk) blocks.\nYou requested a new size"
559                         " of %llu blocks.\n\n"), max_size,
560                         blocksize / 1024, new_size);
561                 exit(1);
562         }
563         if ((flags & RESIZE_DISABLE_64BIT) && (flags & RESIZE_ENABLE_64BIT)) {
564                 fprintf(stderr, _("Cannot set and unset 64bit feature.\n"));
565                 exit(1);
566         } else if (flags & (RESIZE_DISABLE_64BIT | RESIZE_ENABLE_64BIT)) {
567                 if (new_size >= (1ULL << 32)) {
568                         fprintf(stderr, _("Cannot change the 64bit feature "
569                                 "on a filesystem that is larger than "
570                                 "2^32 blocks.\n"));
571                         exit(1);
572                 }
573                 if (mount_flags & EXT2_MF_MOUNTED) {
574                         fprintf(stderr, _("Cannot change the 64bit feature "
575                                 "while the filesystem is mounted.\n"));
576                         exit(1);
577                 }
578                 if (flags & RESIZE_ENABLE_64BIT &&
579                     !ext2fs_has_feature_extents(fs->super)) {
580                         fprintf(stderr, _("Please enable the extents feature "
581                                 "with tune2fs before enabling the 64bit "
582                                 "feature.\n"));
583                         exit(1);
584                 }
585         } else if (new_size == ext2fs_blocks_count(fs->super)) {
586                 fprintf(stderr, _("The filesystem is already %llu (%dk) "
587                         "blocks long.  Nothing to do!\n\n"), new_size,
588                         blocksize / 1024);
589                 exit(0);
590         }
591         if ((flags & RESIZE_ENABLE_64BIT) &&
592             ext2fs_has_feature_64bit(fs->super)) {
593                 fprintf(stderr, _("The filesystem is already 64-bit.\n"));
594                 exit(0);
595         }
596         if ((flags & RESIZE_DISABLE_64BIT) &&
597             !ext2fs_has_feature_64bit(fs->super)) {
598                 fprintf(stderr, _("The filesystem is already 32-bit.\n"));
599                 exit(0);
600         }
601         if (mount_flags & EXT2_MF_MOUNTED) {
602                 bigalloc_check(fs, force);
603                 retval = online_resize_fs(fs, mtpt, &new_size, flags);
604         } else {
605                 bigalloc_check(fs, force);
606                 if (flags & RESIZE_ENABLE_64BIT)
607                         printf(_("Converting the filesystem to 64-bit.\n"));
608                 else if (flags & RESIZE_DISABLE_64BIT)
609                         printf(_("Converting the filesystem to 32-bit.\n"));
610                 else
611                         printf(_("Resizing the filesystem on "
612                                  "%s to %llu (%dk) blocks.\n"),
613                                device_name, new_size, blocksize / 1024);
614                 retval = resize_fs(fs, &new_size, flags,
615                                    ((flags & RESIZE_PERCENT_COMPLETE) ?
616                                     resize_progress_func : 0));
617         }
618         free(mtpt);
619         if (retval) {
620                 com_err(program_name, retval, _("while trying to resize %s"),
621                         device_name);
622                 fprintf(stderr,
623                         _("Please run 'e2fsck -fy %s' to fix the filesystem\n"
624                           "after the aborted resize operation.\n"),
625                         device_name);
626                 ext2fs_close_free(&fs);
627                 exit(1);
628         }
629         printf(_("The filesystem on %s is now %llu (%dk) blocks long.\n\n"),
630                device_name, new_size, blocksize / 1024);
631
632         if ((st_buf.st_size > new_file_size) &&
633             (fd > 0)) {
634 #ifdef HAVE_FTRUNCATE64
635                 retval = ftruncate64(fd, new_file_size);
636 #else
637                 retval = 0;
638                 /* Only truncate if new_file_size doesn't overflow off_t */
639                 if (((off_t) new_file_size) == new_file_size)
640                         retval = ftruncate(fd, (off_t) new_file_size);
641 #endif
642                 if (retval)
643                         com_err(program_name, retval,
644                                 _("while trying to truncate %s"),
645                                 device_name);
646         }
647         if (fd > 0)
648                 close(fd);
649         remove_error_table(&et_ext2_error_table);
650         return (0);
651 }