OSDN Git Service

resize2fs: use EXT2_FLAG_64BITS
[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         /*
320          * XXXX   The combination of flex_bg and !resize_inode causes
321          * major problems for resize2fs, since when the group descriptors
322          * grow in size this can potentially require multiple inode
323          * tables to be moved aside to make room, and resize2fs chokes
324          * rather badly in this scenario.  It's a rare combination,
325          * except when a filesystem is expanded more than a certain
326          * size, so for now, we'll just prohibit that combination.
327          * This is something we should fix eventually, though.
328          */
329         if ((fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) &&
330             !(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
331                 com_err(program_name, 0, _("%s: The combination of flex_bg "
332                                            "and\n\t!resize_inode features "
333                                            "is not supported by resize2fs.\n"),
334                         device_name);
335                 exit(1);
336         }
337
338         min_size = calculate_minimum_resize_size(fs);
339
340         if (print_min_size) {
341                 if (!force && ((fs->super->s_state & EXT2_ERROR_FS) ||
342                                ((fs->super->s_state & EXT2_VALID_FS) == 0))) {
343                         fprintf(stderr,
344                                 _("Please run 'e2fsck -f %s' first.\n\n"),
345                                 device_name);
346                         exit(1);
347                 }
348                 printf(_("Estimated minimum size of the filesystem: %llu\n"),
349                        min_size);
350                 exit(0);
351         }
352
353         /* Determine the system page size if possible */
354 #ifdef HAVE_SYSCONF
355 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
356 #define _SC_PAGESIZE _SC_PAGE_SIZE
357 #endif
358 #ifdef _SC_PAGESIZE
359         sysval = sysconf(_SC_PAGESIZE);
360         if (sysval > 0)
361                 sys_page_size = sysval;
362 #endif /* _SC_PAGESIZE */
363 #endif /* HAVE_SYSCONF */
364
365         /*
366          * Get the size of the containing partition, and use this for
367          * defaults and for making sure the new filesystem doesn't
368          * exceed the partition size.
369          */
370         retval = ext2fs_get_device_size2(device_name, fs->blocksize,
371                                          &max_size);
372         if (retval) {
373                 com_err(program_name, retval,
374                         _("while trying to determine filesystem size"));
375                 exit(1);
376         }
377         if (force_min_size)
378                 new_size = min_size;
379         else if (new_size_str) {
380                 new_size = parse_num_blocks2(new_size_str,
381                                              fs->super->s_log_block_size);
382                 if (new_size == 0) {
383                         com_err(program_name, 0,
384                                 _("Invalid new size: %s\n"), new_size_str);
385                         exit(1);
386                 }
387         } else {
388                 /* Take down devices exactly 16T to 2^32-1 blocks */
389                 if (max_size == (1ULL << 32))
390                         max_size--;
391                 else if (max_size > (1ULL << 32)) {
392                         com_err(program_name, 0, _("New size too large to be "
393                                 "expressed in 32 bits\n"));
394                         exit(1);
395                 }
396                 new_size = max_size;
397                 /* Round down to an even multiple of a pagesize */
398                 if (sys_page_size > fs->blocksize)
399                         new_size &= ~((sys_page_size / fs->blocksize)-1);
400         }
401
402         if (!force && new_size < min_size) {
403                 com_err(program_name, 0,
404                         _("New size smaller than minimum (%llu)\n"), min_size);
405                 exit(1);
406         }
407         if (use_stride >= 0) {
408                 if (use_stride >= (int) fs->super->s_blocks_per_group) {
409                         com_err(program_name, 0,
410                                 _("Invalid stride length"));
411                         exit(1);
412                 }
413                 fs->stride = fs->super->s_raid_stride = use_stride;
414                 ext2fs_mark_super_dirty(fs);
415         } else
416                   determine_fs_stride(fs);
417
418         /*
419          * If we are resizing a plain file, and it's not big enough,
420          * automatically extend it in a sparse fashion by writing the
421          * last requested block.
422          */
423         new_file_size = ((__u64) new_size) * fs->blocksize;
424         if ((__u64) new_file_size >
425             (((__u64) 1) << (sizeof(st_buf.st_size)*8 - 1)) - 1)
426                 fd = -1;
427         if ((new_file_size > st_buf.st_size) &&
428             (fd > 0)) {
429                 if ((ext2fs_llseek(fd, new_file_size-1, SEEK_SET) >= 0) &&
430                     (write(fd, "0", 1) == 1))
431                         max_size = new_size;
432         }
433         if (!force && (new_size > max_size)) {
434                 fprintf(stderr, _("The containing partition (or device)"
435                         " is only %llu (%dk) blocks.\nYou requested a new size"
436                         " of %llu blocks.\n\n"), max_size,
437                         fs->blocksize / 1024, new_size);
438                 exit(1);
439         }
440         if (new_size == ext2fs_blocks_count(fs->super)) {
441                 fprintf(stderr, _("The filesystem is already %llu blocks "
442                         "long.  Nothing to do!\n\n"), new_size);
443                 exit(0);
444         }
445         if (mount_flags & EXT2_MF_MOUNTED) {
446                 retval = online_resize_fs(fs, mtpt, &new_size, flags);
447         } else {
448                 if (!force && ((fs->super->s_lastcheck < fs->super->s_mtime) ||
449                                (fs->super->s_state & EXT2_ERROR_FS) ||
450                                ((fs->super->s_state & EXT2_VALID_FS) == 0))) {
451                         fprintf(stderr,
452                                 _("Please run 'e2fsck -f %s' first.\n\n"),
453                                 device_name);
454                         exit(1);
455                 }
456                 printf(_("Resizing the filesystem on "
457                          "%s to %llu (%dk) blocks.\n"),
458                        device_name, new_size, fs->blocksize / 1024);
459                 retval = resize_fs(fs, &new_size, flags,
460                                    ((flags & RESIZE_PERCENT_COMPLETE) ?
461                                     resize_progress_func : 0));
462         }
463         free(mtpt);
464         if (retval) {
465                 com_err(program_name, retval, _("while trying to resize %s"),
466                         device_name);
467                 fprintf(stderr,
468                         _("Please run 'e2fsck -fy %s' to fix the filesystem\n"
469                           "after the aborted resize operation.\n"),
470                         device_name);
471                 ext2fs_close(fs);
472                 exit(1);
473         }
474         printf(_("The filesystem on %s is now %llu blocks long.\n\n"),
475                device_name, new_size);
476
477         if ((st_buf.st_size > new_file_size) &&
478             (fd > 0)) {
479 #ifdef HAVE_FTRUNCATE64
480                 retval = ftruncate64(fd, new_file_size);
481 #else
482                 retval = 0;
483                 /* Only truncate if new_file_size doesn't overflow off_t */
484                 if (((off_t) new_file_size) == new_file_size)
485                         retval = ftruncate(fd, (off_t) new_file_size);
486 #endif
487                 if (retval)
488                         com_err(program_name, retval,
489                                 _("while trying to truncate %s"),
490                                 device_name);
491         }
492         if (fd > 0)
493                 close(fd);
494         remove_error_table(&et_ext2_error_table);
495         return (0);
496 }