Whamcloud - gitweb
tune2fs.8.in: Fix some whitespace errors in the command synopsis.
[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 by Theosore 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 #ifdef HAVE_GETOPT_H
16 #include <getopt.h>
17 #else
18 extern char *optarg;
19 extern int optind;
20 #endif
21 #include <fcntl.h>
22 #include <sys/stat.h>
23
24 #include "resize2fs.h"
25
26 #include "../version.h"
27
28 char *program_name, *device_name;
29
30 static void usage (char *prog)
31 {
32         fprintf (stderr, _("usage: %s [-d debug_flags] [-f] [-F] [-p] device [new-size]\n\n"), prog);
33
34         exit (1);
35 }
36
37 static errcode_t resize_progress_func(ext2_resize_t rfs, int pass,
38                                       unsigned long cur, unsigned long max)
39 {
40         ext2_sim_progmeter progress;
41         const char      *label;
42         errcode_t       retval;
43
44         progress = (ext2_sim_progmeter) rfs->prog_data;
45         if (max == 0)
46                 return 0;
47         if (cur == 0) {
48                 if (progress)
49                         ext2fs_progress_close(progress);
50                 progress = 0;
51                 switch (pass) {
52                 case E2_RSZ_EXTEND_ITABLE_PASS:
53                         label = _("Extending the inode table");
54                         break;
55                 case E2_RSZ_BLOCK_RELOC_PASS:
56                         label = _("Relocating blocks");
57                         break;
58                 case E2_RSZ_INODE_SCAN_PASS:
59                         label = _("Scanning inode table");
60                         break;
61                 case E2_RSZ_INODE_REF_UPD_PASS:
62                         label = _("Updating inode references");
63                         break;
64                 case E2_RSZ_MOVE_ITABLE_PASS:
65                         label = _("Moving inode table");
66                         break;
67                 default:
68                         label = _("Unknown pass?!?");
69                         break;
70                 }
71                 printf(_("Begin pass %d (max = %lu)\n"), pass, max);
72                 retval = ext2fs_progress_init(&progress, label, 30,
73                                               40, max, 0);
74                 if (retval)
75                         progress = 0;
76                 rfs->prog_data = (void *) progress;
77         }
78         if (progress)
79                 ext2fs_progress_update(progress, cur);
80         if (cur >= max) {
81                 if (progress)
82                         ext2fs_progress_close(progress);
83                 progress = 0;
84                 rfs->prog_data = 0;
85         }
86         return 0;
87 }
88
89 static void check_mount(char *device)
90 {
91         errcode_t       retval;
92         int             mount_flags;
93
94         retval = ext2fs_check_if_mounted(device, &mount_flags);
95         if (retval) {
96                 com_err("ext2fs_check_if_mount", retval,
97                         _("while determining whether %s is mounted."),
98                         device);
99                 return;
100         }
101         if (!(mount_flags & EXT2_MF_MOUNTED))
102                 return;
103         
104         fprintf(stderr, _("%s is mounted; can't resize a "
105                 "mounted filesystem!\n\n"), device);
106         exit(1);
107 }
108
109 static int get_units(const char *s)
110 {
111         if (strlen(s) != 1)
112                 return -1;
113         switch(s[0]) {
114         case 's':
115                 return 512;
116         case 'K':
117                 return 1024;
118         case 'M':
119                 return 1024*1024;
120         case 'G':
121                 return 1024*1024*1024;
122         }
123         return -1;
124 }
125
126 int main (int argc, char ** argv)
127 {
128         errcode_t       retval;
129         ext2_filsys     fs;
130         int             c;
131         int             flags = 0;
132         int             flush = 0;
133         int             force = 0;
134         int             fd;
135         blk_t           new_size = 0;
136         blk_t           max_size = 0;
137         int             units = 0;
138         io_manager      io_ptr;
139         char            *tmp;
140         struct stat     st_buf;
141         unsigned int    sys_page_size = 4096;
142         long            sysval;
143
144 #ifdef ENABLE_NLS
145         setlocale(LC_MESSAGES, "");
146         setlocale(LC_CTYPE, "");
147         bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
148         textdomain(NLS_CAT_NAME);
149 #endif
150
151         initialize_ext2_error_table();
152
153         fprintf (stderr, _("resize2fs %s (%s)\n"),
154                  E2FSPROGS_VERSION, E2FSPROGS_DATE);
155         if (argc && *argv)
156                 program_name = *argv;
157
158         while ((c = getopt (argc, argv, "d:fFhp")) != EOF) {
159                 switch (c) {
160                 case 'h':
161                         usage(program_name);
162                         break;
163                 case 'f':
164                         force = 1;
165                         break;
166                 case 'F':
167                         flush = 1;
168                         break;
169                 case 'd':
170                         flags |= atoi(optarg);
171                         break;
172                 case 'p':
173                         flags |= RESIZE_PERCENT_COMPLETE;
174                         break;
175                 default:
176                         usage(program_name);
177                 }
178         }
179         if (optind == argc)
180                 usage(program_name);
181
182         device_name = argv[optind++];
183         if (optind < argc) {
184                 new_size = strtoul(argv[optind++], &tmp, 0);
185                 if (*tmp) {
186                         units = get_units(tmp);
187                         if (units < 0) {
188                                 com_err(program_name, 0, 
189                                         _("bad filesystem size - %s"),
190                                         argv[optind - 1]);
191                                 exit(1);
192                         }
193                 }
194         }
195         if (optind < argc)
196                 usage(program_name);
197         
198         check_mount(device_name);
199         
200         if (flush) {
201                 fd = open(device_name, O_RDONLY, 0);
202
203                 if (fd < 0) {
204                         com_err("open", errno,
205                                 _("while opening %s for flushing"),
206                                 device_name);
207                         exit(1);
208                 }
209                 retval = ext2fs_sync_device(fd, 1);
210                 if (retval) {
211                         com_err(argv[0], retval, 
212                                 _("while trying to flush %s"),
213                                 device_name);
214                         exit(1);
215                 }
216                 close(fd);
217         }
218
219         if (flags & RESIZE_DEBUG_IO) {
220                 io_ptr = test_io_manager;
221                 test_io_backing_manager = unix_io_manager;
222         } else 
223                 io_ptr = unix_io_manager;
224
225         retval = ext2fs_open (device_name, EXT2_FLAG_RW, 0, 0,
226                               io_ptr, &fs);
227         if (retval) {
228                 com_err (program_name, retval, _("while trying to open %s"),
229                          device_name);
230                 printf (_("Couldn't find valid filesystem superblock.\n"));
231                 exit (1);
232         }
233         /*
234          * Check for compatibility with the feature sets.  We need to
235          * be more stringent than ext2fs_open().
236          */
237         if (fs->super->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) {
238                 com_err(program_name, EXT2_ET_UNSUPP_FEATURE,
239                         "(%s)", device_name);
240                 exit(1);
241         }
242         
243         /* Determine the system page size if possible */
244 #ifdef HAVE_SYSCONF
245 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
246 #define _SC_PAGESIZE _SC_PAGE_SIZE
247 #endif
248 #ifdef _SC_PAGESIZE
249         sysval = sysconf(_SC_PAGESIZE);
250         if (sysval > 0)
251                 sys_page_size = sysval;
252 #endif /* _SC_PAGESIZE */
253 #endif /* HAVE_SYSCONF */
254
255         /*
256          * Get the size of the containing partition, and use this for
257          * defaults and for making sure the new filesystme doesn't
258          * exceed the partition size.
259          */
260         retval = ext2fs_get_device_size(device_name, fs->blocksize,
261                                         &max_size);
262         if (retval) {
263                 com_err(program_name, retval,
264                         _("while trying to determine filesystem size"));
265                 exit(1);
266         }
267         if (units) {
268                 if ((unsigned) units < fs->blocksize)
269                         new_size = (new_size * units) / fs->blocksize;
270                 else if ((unsigned) units > fs->blocksize)
271                         new_size = new_size * (units / fs->blocksize);
272         }
273         if (!new_size) {
274                 new_size = max_size;
275                 /* Round down to an even multiple of a pagesize */
276                 if (sys_page_size > fs->blocksize)
277                         new_size &= ~((sys_page_size / fs->blocksize)-1);
278         }
279         
280         /*
281          * If we are resizing a plain file, and it's not big enough,
282          * automatically extend it in a sparse fashion by writing the
283          * last requested block.
284          */
285         if ((new_size > max_size) &&
286             (stat(device_name, &st_buf) == 0) &&
287             S_ISREG(st_buf.st_mode) &&
288             ((tmp = malloc(fs->blocksize)) != 0)) {
289                 memset(tmp, 0, fs->blocksize);
290                 retval = io_channel_write_blk(fs->io, new_size-1, 1, tmp);
291                 if (retval == 0)
292                         max_size = new_size;
293                 free(tmp);
294         }
295         if (!force && (new_size > max_size)) {
296                 fprintf(stderr, _("The containing partition (or device)"
297                         " is only %d (%dk) blocks.\nYou requested a new size"
298                         " of %d blocks.\n\n"), max_size,
299                         fs->blocksize / 1024, new_size);
300                 exit(1);
301         }
302         if (new_size == fs->super->s_blocks_count) {
303                 fprintf(stderr, _("The filesystem is already %d blocks "
304                         "long.  Nothing to do!\n\n"), new_size);
305                 exit(0);
306         }
307         if (!force && ((fs->super->s_lastcheck < fs->super->s_mtime) ||
308                        (fs->super->s_state & EXT2_ERROR_FS) ||
309                        ((fs->super->s_state & EXT2_VALID_FS) == 0))) {
310                 fprintf(stderr, _("Please run 'e2fsck -f %s' first.\n\n"),
311                         device_name);
312                 exit(1);
313         }
314         printf("Resizing the filesystem on %s to %d (%dk) blocks.\n",
315                device_name, new_size, fs->blocksize / 1024);
316         retval = resize_fs(fs, &new_size, flags,
317                            ((flags & RESIZE_PERCENT_COMPLETE) ?
318                             resize_progress_func : 0));
319         if (retval) {
320                 com_err(program_name, retval, _("while trying to resize %s"),
321                         device_name);
322                 ext2fs_close (fs);
323                 exit(1);
324         }
325         printf(_("The filesystem on %s is now %d blocks long.\n\n"),
326                device_name, new_size);
327         return (0);
328 }