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