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