Whamcloud - gitweb
ChangeLog, Makefile.in, jfs_user.h:
[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/ioctl.h>
23
24 #include "resize2fs.h"
25
26 #include "../version.h"
27
28 char *program_name, *device_name;
29
30 static volatile 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
110 int main (int argc, char ** argv)
111 {
112         errcode_t       retval;
113         ext2_filsys     fs;
114         int             c;
115         int             flags = 0;
116         int             flush = 0;
117         int             force = 0;
118         int             fd;
119         blk_t           new_size = 0;
120         blk_t           max_size = 0;
121         io_manager      io_ptr;
122         char            *tmp;
123         
124         initialize_ext2_error_table();
125
126         fprintf (stderr, _("resize2fs %s (%s)\n"),
127                  E2FSPROGS_VERSION, E2FSPROGS_DATE);
128         if (argc && *argv)
129                 program_name = *argv;
130
131         while ((c = getopt (argc, argv, "d:fFhp")) != EOF) {
132                 switch (c) {
133                 case 'h':
134                         usage(program_name);
135                         break;
136                 case 'f':
137                         force = 1;
138                         break;
139                 case 'F':
140                         flush = 1;
141                         break;
142                 case 'd':
143                         flags |= atoi(optarg);
144                         break;
145                 case 'p':
146                         flags |= RESIZE_PERCENT_COMPLETE;
147                         break;
148                 default:
149                         usage(program_name);
150                 }
151         }
152         if (optind == argc)
153                 usage(program_name);
154
155         device_name = argv[optind++];
156         if (optind < argc) {
157                 new_size = strtoul(argv[optind++], &tmp, 0);
158                 if (*tmp) {
159                         com_err(program_name, 0, _("bad filesystem size - %s"),
160                                 argv[optind - 1]);
161                         exit(1);
162                 }
163         }
164         if (optind < argc)
165                 usage(program_name);
166         
167         check_mount(device_name);
168         
169         if (flush) {
170 #ifdef BLKFLSBUF
171                 fd = open(device_name, O_RDONLY, 0);
172
173                 if (fd < 0) {
174                         com_err("open", errno,
175                                 _("while opening %s for flushing"),
176                                 device_name);
177                         exit(1);
178                 }
179                 if (ioctl(fd, BLKFLSBUF, 0) < 0) {
180                         com_err("BLKFLSBUF", errno,
181                                 _("while trying to flush %s"),
182                                 device_name);
183                         exit(1);
184                 }
185                 close(fd);
186 #else
187                 fprintf(stderr, _("BLKFLSBUF not supported"));
188                 exit(1);
189 #endif /* BLKFLSBUF */
190         }
191
192         if (flags & RESIZE_DEBUG_IO) {
193                 io_ptr = test_io_manager;
194                 test_io_backing_manager = unix_io_manager;
195         } else 
196                 io_ptr = unix_io_manager;
197
198         retval = ext2fs_open (device_name, EXT2_FLAG_RW, 0, 0,
199                               io_ptr, &fs);
200         if (retval) {
201                 com_err (program_name, retval, _("while trying to open %s"),
202                          device_name);
203                 printf (_("Couldn't find valid filesystem superblock.\n"));
204                 exit (1);
205         }
206         /*
207          * Check for compatibility with the feature sets.  We need to
208          * be more stringent than ext2fs_open().
209          */
210         if ((fs->super->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) ||
211             (fs->super->s_feature_incompat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP)) {
212                 com_err(program_name, EXT2_ET_UNSUPP_FEATURE,
213                         "(%s)", device_name);
214                 exit(1);
215         }
216         
217         /*
218          * Get the size of the containing partition, and use this for
219          * defaults and for making sure the new filesystme doesn't
220          * exceed the partition size.
221          */
222         retval = ext2fs_get_device_size(device_name, fs->blocksize,
223                                         &max_size);
224         if (retval) {
225                 com_err(program_name, retval,
226                         _("while trying to determine filesystem size"));
227                 exit(1);
228         }
229         if (!new_size)
230                 new_size = max_size;
231         if (!force && (new_size > max_size)) {
232                 fprintf(stderr, _("The containing partition (or device)"
233                         " is only %d blocks.\nYou requested a new size"
234                         " of %d blocks.\n\n"), max_size,
235                         new_size);
236                 exit(1);
237         }
238         if (new_size == fs->super->s_blocks_count) {
239                 fprintf(stderr, _("The filesystem is already %d blocks "
240                         "long.  Nothing to do!\n\n"), new_size);
241                 exit(0);
242         }
243         if (!force && (fs->super->s_lastcheck < fs->super->s_mtime)) {
244                 fprintf(stderr, _("Please run 'e2fsck -f %s' first.\n\n"),
245                         device_name);
246                 exit(1);
247         }
248         retval = resize_fs(fs, new_size, flags,
249                            ((flags & RESIZE_PERCENT_COMPLETE) ?
250                             resize_progress_func : 0));
251         if (retval) {
252                 com_err(program_name, retval, _("while trying to resize %s"),
253                         device_name);
254                 ext2fs_close (fs);
255         }
256         printf(_("The filesystem on %s is now %d blocks long.\n\n"),
257                device_name, new_size);
258         return (0);
259 }