OSDN Git Service

b60155742dccad31fb1cc9a3656b1201a95edef3
[android-x86/external-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, *device_name, *io_options;
40
41 static void usage (char *prog)
42 {
43         fprintf (stderr, _("Usage: %s [-d debug_flags] [-f] [-F] [-M] [-P] "
44                            "[-p] device [new_size]\n\n"), prog);
45
46         exit (1);
47 }
48
49 static errcode_t resize_progress_func(ext2_resize_t rfs, int pass,
50                                       unsigned long cur, unsigned long max)
51 {
52         ext2_sim_progmeter progress;
53         const char      *label;
54         errcode_t       retval;
55
56         progress = (ext2_sim_progmeter) rfs->prog_data;
57         if (max == 0)
58                 return 0;
59         if (cur == 0) {
60                 if (progress)
61                         ext2fs_progress_close(progress);
62                 progress = 0;
63                 switch (pass) {
64                 case E2_RSZ_EXTEND_ITABLE_PASS:
65                         label = _("Extending the inode table");
66                         break;
67                 case E2_RSZ_BLOCK_RELOC_PASS:
68                         label = _("Relocating blocks");
69                         break;
70                 case E2_RSZ_INODE_SCAN_PASS:
71                         label = _("Scanning inode table");
72                         break;
73                 case E2_RSZ_INODE_REF_UPD_PASS:
74                         label = _("Updating inode references");
75                         break;
76                 case E2_RSZ_MOVE_ITABLE_PASS:
77                         label = _("Moving inode table");
78                         break;
79                 default:
80                         label = _("Unknown pass?!?");
81                         break;
82                 }
83                 printf(_("Begin pass %d (max = %lu)\n"), pass, max);
84                 retval = ext2fs_progress_init(&progress, label, 30,
85                                               40, max, 0);
86                 if (retval)
87                         progress = 0;
88                 rfs->prog_data = (void *) progress;
89         }
90         if (progress)
91                 ext2fs_progress_update(progress, cur);
92         if (cur >= max) {
93                 if (progress)
94                         ext2fs_progress_close(progress);
95                 progress = 0;
96                 rfs->prog_data = 0;
97         }
98         return 0;
99 }
100
101 static void determine_fs_stride(ext2_filsys fs)
102 {
103         unsigned int    group;
104         unsigned long long sum;
105         unsigned int    has_sb, prev_has_sb, num;
106         int             i_stride, b_stride;
107
108         if (fs->stride)
109                 return;
110         num = 0; sum = 0;
111         for (group = 0; group < fs->group_desc_count; group++) {
112                 has_sb = ext2fs_bg_has_super(fs, group);
113                 if (group == 0 || has_sb != prev_has_sb)
114                         goto next;
115                 b_stride = ext2fs_block_bitmap_loc(fs, group) -
116                         ext2fs_block_bitmap_loc(fs, group - 1) -
117                         fs->super->s_blocks_per_group;
118                 i_stride = ext2fs_inode_bitmap_loc(fs, group) -
119                         ext2fs_inode_bitmap_loc(fs, group - 1) -
120                         fs->super->s_blocks_per_group;
121                 if (b_stride != i_stride ||
122                     b_stride < 0)
123                         goto next;
124
125                 /* printf("group %d has stride %d\n", group, b_stride); */
126                 sum += b_stride;
127                 num++;
128
129         next:
130                 prev_has_sb = has_sb;
131         }
132
133         if (fs->group_desc_count > 12 && num < 3)
134                 sum = 0;
135
136         if (num)
137                 fs->stride = sum / num;
138         else
139                 fs->stride = 0;
140
141         fs->super->s_raid_stride = fs->stride;
142         ext2fs_mark_super_dirty(fs);
143
144 #if 0
145         if (fs->stride)
146                 printf("Using RAID stride of %d\n", fs->stride);
147 #endif
148 }
149
150 int main (int argc, char ** argv)
151 {
152         errcode_t       retval;
153         ext2_filsys     fs;
154         int             c;
155         int             flags = 0;
156         int             flush = 0;
157         int             force = 0;
158         int             io_flags = 0;
159         int             force_min_size = 0;
160         int             print_min_size = 0;
161         int             fd, ret;
162         blk64_t         new_size = 0;
163         blk64_t         max_size = 0;
164         blk64_t         min_size = 0;
165         io_manager      io_ptr;
166         char            *new_size_str = 0;
167         int             use_stride = -1;
168         ext2fs_struct_stat st_buf;
169         __s64           new_file_size;
170         unsigned int    sys_page_size = 4096;
171         long            sysval;
172         int             len, mount_flags;
173         char            *mtpt;
174
175 #ifdef ENABLE_NLS
176         setlocale(LC_MESSAGES, "");
177         setlocale(LC_CTYPE, "");
178         bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
179         textdomain(NLS_CAT_NAME);
180         set_com_err_gettext(gettext);
181 #endif
182
183         add_error_table(&et_ext2_error_table);
184
185         fprintf (stderr, "resize2fs %s (%s)\n",
186                  E2FSPROGS_VERSION, E2FSPROGS_DATE);
187         if (argc && *argv)
188                 program_name = *argv;
189
190         while ((c = getopt (argc, argv, "d:fFhMPpS:")) != EOF) {
191                 switch (c) {
192                 case 'h':
193                         usage(program_name);
194                         break;
195                 case 'f':
196                         force = 1;
197                         break;
198                 case 'F':
199                         flush = 1;
200                         break;
201                 case 'M':
202                         force_min_size = 1;
203                         break;
204                 case 'P':
205                         print_min_size = 1;
206                         break;
207                 case 'd':
208                         flags |= atoi(optarg);
209                         break;
210                 case 'p':
211                         flags |= RESIZE_PERCENT_COMPLETE;
212                         break;
213                 case 'S':
214                         use_stride = atoi(optarg);
215                         break;
216                 default:
217                         usage(program_name);
218                 }
219         }
220         if (optind == argc)
221                 usage(program_name);
222
223         device_name = argv[optind++];
224         if (optind < argc)
225                 new_size_str = argv[optind++];
226         if (optind < argc)
227                 usage(program_name);
228
229         io_options = strchr(device_name, '?');
230         if (io_options)
231                 *io_options++ = 0;
232
233         /*
234          * Figure out whether or not the device is mounted, and if it is
235          * where it is mounted.
236          */
237         len=80;
238         while (1) {
239                 mtpt = malloc(len);
240                 if (!mtpt)
241                         return ENOMEM;
242                 mtpt[len-1] = 0;
243                 retval = ext2fs_check_mount_point(device_name, &mount_flags,
244                                                   mtpt, len);
245                 if (retval) {
246                         com_err("ext2fs_check_mount_point", retval,
247                                 _("while determining whether %s is mounted."),
248                                 device_name);
249                         exit(1);
250                 }
251                 if (!(mount_flags & EXT2_MF_MOUNTED) || (mtpt[len-1] == 0))
252                         break;
253                 free(mtpt);
254                 len = 2 * len;
255         }
256
257         fd = ext2fs_open_file(device_name, O_RDWR, 0);
258         if (fd < 0) {
259                 com_err("open", errno, _("while opening %s"),
260                         device_name);
261                 exit(1);
262         }
263
264         ret = ext2fs_fstat(fd, &st_buf);
265         if (ret < 0) {
266                 com_err("open", errno,
267                         _("while getting stat information for %s"),
268                         device_name);
269                 exit(1);
270         }
271
272         if (flush) {
273                 retval = ext2fs_sync_device(fd, 1);
274                 if (retval) {
275                         com_err(argv[0], retval,
276                                 _("while trying to flush %s"),
277                                 device_name);
278                         exit(1);
279                 }
280         }
281
282         if (!S_ISREG(st_buf.st_mode )) {
283                 close(fd);
284                 fd = -1;
285         }
286
287 #ifdef CONFIG_TESTIO_DEBUG
288         if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
289                 io_ptr = test_io_manager;
290                 test_io_backing_manager = unix_io_manager;
291         } else
292 #endif
293                 io_ptr = unix_io_manager;
294
295         if (!(mount_flags & EXT2_MF_MOUNTED))
296                 io_flags = EXT2_FLAG_RW | EXT2_FLAG_EXCLUSIVE;
297
298         io_flags |= EXT2_FLAG_64BITS;
299
300         retval = ext2fs_open2(device_name, io_options, io_flags,
301                               0, 0, io_ptr, &fs);
302         if (retval) {
303                 com_err (program_name, retval, _("while trying to open %s"),
304                          device_name);
305                 printf (_("Couldn't find valid filesystem superblock.\n"));
306                 exit (1);
307         }
308
309         /*
310          * Check for compatibility with the feature sets.  We need to
311          * be more stringent than ext2fs_open().
312          */
313         if (fs->super->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) {
314                 com_err(program_name, EXT2_ET_UNSUPP_FEATURE,
315                         "(%s)", device_name);
316                 exit(1);
317         }
318
319         min_size = calculate_minimum_resize_size(fs);
320
321         if (print_min_size) {
322                 if (!force && ((fs->super->s_state & EXT2_ERROR_FS) ||
323                                ((fs->super->s_state & EXT2_VALID_FS) == 0))) {
324                         fprintf(stderr,
325                                 _("Please run 'e2fsck -f %s' first.\n\n"),
326                                 device_name);
327                         exit(1);
328                 }
329                 printf(_("Estimated minimum size of the filesystem: %llu\n"),
330                        min_size);
331                 exit(0);
332         }
333
334         /* Determine the system page size if possible */
335 #ifdef HAVE_SYSCONF
336 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
337 #define _SC_PAGESIZE _SC_PAGE_SIZE
338 #endif
339 #ifdef _SC_PAGESIZE
340         sysval = sysconf(_SC_PAGESIZE);
341         if (sysval > 0)
342                 sys_page_size = sysval;
343 #endif /* _SC_PAGESIZE */
344 #endif /* HAVE_SYSCONF */
345
346         /*
347          * Get the size of the containing partition, and use this for
348          * defaults and for making sure the new filesystem doesn't
349          * exceed the partition size.
350          */
351         retval = ext2fs_get_device_size2(device_name, fs->blocksize,
352                                          &max_size);
353         if (retval) {
354                 com_err(program_name, retval,
355                         _("while trying to determine filesystem size"));
356                 exit(1);
357         }
358         if (force_min_size)
359                 new_size = min_size;
360         else if (new_size_str) {
361                 new_size = parse_num_blocks2(new_size_str,
362                                              fs->super->s_log_block_size);
363                 if (new_size == 0) {
364                         com_err(program_name, 0,
365                                 _("Invalid new size: %s\n"), new_size_str);
366                         exit(1);
367                 }
368         } else {
369                 new_size = max_size;
370                 /* Round down to an even multiple of a pagesize */
371                 if (sys_page_size > fs->blocksize)
372                         new_size &= ~((sys_page_size / fs->blocksize)-1);
373         }
374         if (!EXT2_HAS_INCOMPAT_FEATURE(fs->super,
375                                        EXT4_FEATURE_INCOMPAT_64BIT)) {
376                 /* Take 16T down to 2^32-1 blocks */
377                 if (new_size == (1ULL << 32))
378                         new_size--;
379                 else if (new_size > (1ULL << 32)) {
380                         com_err(program_name, 0,
381                                 _("New size too large to be "
382                                   "expressed in 32 bits\n"));
383                         exit(1);
384                 }
385         }
386
387         if (!force && new_size < min_size) {
388                 com_err(program_name, 0,
389                         _("New size smaller than minimum (%llu)\n"), min_size);
390                 exit(1);
391         }
392         if (use_stride >= 0) {
393                 if (use_stride >= (int) fs->super->s_blocks_per_group) {
394                         com_err(program_name, 0,
395                                 _("Invalid stride length"));
396                         exit(1);
397                 }
398                 fs->stride = fs->super->s_raid_stride = use_stride;
399                 ext2fs_mark_super_dirty(fs);
400         } else
401                   determine_fs_stride(fs);
402
403         /*
404          * If we are resizing a plain file, and it's not big enough,
405          * automatically extend it in a sparse fashion by writing the
406          * last requested block.
407          */
408         new_file_size = ((__u64) new_size) * fs->blocksize;
409         if ((__u64) new_file_size >
410             (((__u64) 1) << (sizeof(st_buf.st_size)*8 - 1)) - 1)
411                 fd = -1;
412         if ((new_file_size > st_buf.st_size) &&
413             (fd > 0)) {
414                 if ((ext2fs_llseek(fd, new_file_size-1, SEEK_SET) >= 0) &&
415                     (write(fd, "0", 1) == 1))
416                         max_size = new_size;
417         }
418         if (!force && (new_size > max_size)) {
419                 fprintf(stderr, _("The containing partition (or device)"
420                         " is only %llu (%dk) blocks.\nYou requested a new size"
421                         " of %llu blocks.\n\n"), max_size,
422                         fs->blocksize / 1024, new_size);
423                 exit(1);
424         }
425         if (new_size == ext2fs_blocks_count(fs->super)) {
426                 fprintf(stderr, _("The filesystem is already %llu blocks "
427                         "long.  Nothing to do!\n\n"), new_size);
428                 exit(0);
429         }
430         if (mount_flags & EXT2_MF_MOUNTED) {
431                 retval = online_resize_fs(fs, mtpt, &new_size, flags);
432         } else {
433                 if (!force && ((fs->super->s_lastcheck < fs->super->s_mtime) ||
434                                (fs->super->s_state & EXT2_ERROR_FS) ||
435                                ((fs->super->s_state & EXT2_VALID_FS) == 0))) {
436                         fprintf(stderr,
437                                 _("Please run 'e2fsck -f %s' first.\n\n"),
438                                 device_name);
439                         exit(1);
440                 }
441                 /*
442                  * XXXX The combination of flex_bg and !resize_inode
443                  * causes major problems for resize2fs, since when the
444                  * group descriptors grow in size this can potentially
445                  * require multiple inode tables to be moved aside to
446                  * make room, and resize2fs chokes rather badly in
447                  * this scenario.  It's a rare combination, except
448                  * when a filesystem is expanded more than a certain
449                  * size, so for now, we'll just prohibit that
450                  * combination.  This is something we should fix
451                  * eventually, though.
452                  */
453                 if ((fs->super->s_feature_incompat &
454                      EXT4_FEATURE_INCOMPAT_FLEX_BG) &&
455                     !(fs->super->s_feature_compat &
456                       EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
457                         com_err(program_name, 0, _("%s: The combination of "
458                                 "flex_bg and\n\t!resize_inode features "
459                                 "is not supported by resize2fs.\n"),
460                                 device_name);
461                         exit(1);
462                 }
463                 printf(_("Resizing the filesystem on "
464                          "%s to %llu (%dk) blocks.\n"),
465                        device_name, new_size, fs->blocksize / 1024);
466                 retval = resize_fs(fs, &new_size, flags,
467                                    ((flags & RESIZE_PERCENT_COMPLETE) ?
468                                     resize_progress_func : 0));
469         }
470         free(mtpt);
471         if (retval) {
472                 com_err(program_name, retval, _("while trying to resize %s"),
473                         device_name);
474                 fprintf(stderr,
475                         _("Please run 'e2fsck -fy %s' to fix the filesystem\n"
476                           "after the aborted resize operation.\n"),
477                         device_name);
478                 ext2fs_close(fs);
479                 exit(1);
480         }
481         printf(_("The filesystem on %s is now %llu blocks long.\n\n"),
482                device_name, new_size);
483
484         if ((st_buf.st_size > new_file_size) &&
485             (fd > 0)) {
486 #ifdef HAVE_FTRUNCATE64
487                 retval = ftruncate64(fd, new_file_size);
488 #else
489                 retval = 0;
490                 /* Only truncate if new_file_size doesn't overflow off_t */
491                 if (((off_t) new_file_size) == new_file_size)
492                         retval = ftruncate(fd, (off_t) new_file_size);
493 #endif
494                 if (retval)
495                         com_err(program_name, retval,
496                                 _("while trying to truncate %s"),
497                                 device_name);
498         }
499         if (fd > 0)
500                 close(fd);
501         remove_error_table(&et_ext2_error_table);
502         return (0);
503 }