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