Whamcloud - gitweb
1576ce44b0e9504242a200128f67dc633014a3f6
[tools/e2fsprogs.git] / misc / tune2fs.c
1 /*
2  * tune2fs.c            - Change the file system parameters on
3  *                        an unmounted second extended file system
4  *
5  * Copyright (C) 1992, 1993, 1994  Remy Card <card@masi.ibp.fr>
6  *                                 Laboratoire MASI, Institut Blaise Pascal
7  *                                 Universite Pierre et Marie Curie (Paris VI)
8  *
9  * This file can be redistributed under the terms of the GNU General
10  * Public License
11  */
12
13 /*
14  * History:
15  * 93/06/01     - Creation
16  * 93/10/31     - Added the -c option to change the maximal mount counts
17  * 93/12/14     - Added -l flag to list contents of superblock
18  *                M.J.E. Mol (marcel@duteca.et.tudelft.nl)
19  *                F.W. ten Wolde (franky@duteca.et.tudelft.nl)
20  * 93/12/29     - Added the -e option to change errors behavior
21  * 94/02/27     - Ported to use the ext2fs library
22  * 94/03/06     - Added the checks interval from Uwe Ohse (uwe@tirka.gun.de)
23  */
24
25 #include <fcntl.h>
26 #include <grp.h>
27 #ifdef HAVE_GETOPT_H
28 #include <getopt.h>
29 #endif
30 #include <pwd.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <time.h>
35 #include <unistd.h>
36 #include <sys/types.h>
37
38 #include <linux/ext2_fs.h>
39
40 #include "ext2fs/ext2fs.h"
41 #include "et/com_err.h"
42 #include "e2p/e2p.h"
43
44 #include "../version.h"
45
46 const char * program_name = "tune2fs";
47 char * device_name = NULL;
48 int c_flag = 0;
49 int e_flag = 0;
50 int g_flag = 0;
51 int i_flag = 0;
52 int l_flag = 0;
53 int m_flag = 0;
54 int r_flag = 0;
55 int u_flag = 0;
56 int max_mount_count;
57 unsigned long interval;
58 unsigned long reserved_ratio = 0;
59 unsigned long reserved_blocks = 0;
60 unsigned short errors;
61 unsigned long resgid = 0;
62 unsigned long resuid = 0;
63
64 static volatile void usage (void)
65 {
66         fprintf (stderr, "Usage: %s [-c max-mounts-count] [-e errors-behavior] "
67                  "[-g group]\n"
68                  "\t[-i interval[d|m|w]] [-l] [-m reserved-blocks-percent]\n"
69                  "\t[-r reserved-blocks-count] [-u user] device\n", program_name);
70         exit (1);
71 }
72
73 void main (int argc, char ** argv)
74 {
75         char c;
76         char * tmp;
77         errcode_t retval;
78         ext2_filsys fs;
79         struct group * gr;
80         struct passwd * pw;
81
82         fprintf (stderr, "tune2fs %s, %s for EXT2 FS %s, %s\n",
83                  E2FSPROGS_VERSION, E2FSPROGS_DATE,
84                  EXT2FS_VERSION, EXT2FS_DATE);
85         if (argc && *argv)
86                 program_name = *argv;
87         initialize_ext2_error_table();
88         while ((c = getopt (argc, argv, "c:e:g:i:lm:r:u:")) != EOF)
89                 switch (c)
90                 {
91                         case 'c':
92                                 max_mount_count = strtoul (optarg, &tmp, 0);
93                                 if (*tmp || max_mount_count > 16000)
94                                 {
95                                         com_err (program_name, 0,
96                                                  "bad mounts count - %s",
97                                                  optarg);
98                                         usage ();
99                                 }
100                                 c_flag = 1;
101                                 break;
102                         case 'e':
103                                 if (strcmp (optarg, "continue") == 0)
104                                         errors = EXT2_ERRORS_CONTINUE;
105                                 else if (strcmp (optarg, "remount-ro") == 0)
106                                         errors = EXT2_ERRORS_RO;
107                                 else if (strcmp (optarg, "panic") == 0)
108                                         errors = EXT2_ERRORS_PANIC;
109                                 else
110                                 {
111                                         com_err (program_name, 0,
112                                                  "bad error behavior - %s",
113                                                  optarg);
114                                         usage ();
115                                 }
116                                 e_flag = 1;
117                                 break;
118                         case 'g':
119                                 resgid = strtoul (optarg, &tmp, 0);
120                                 if (*tmp)
121                                 {
122                                         gr = getgrnam (optarg);
123                                         if (gr == NULL)
124                                                 tmp = optarg;
125                                         else {
126                                                 resgid = gr->gr_gid;
127                                                 *tmp =0;
128                                         }
129                                 }
130                                 if (*tmp)
131                                 {
132                                         com_err (program_name, 0,
133                                                  "bad gid/group name - %s",
134                                                  optarg);
135                                         usage ();
136                                 }
137                                 g_flag = 1;
138                                 break;
139                         case 'i':
140                                 interval = strtoul (optarg, &tmp, 0);
141                                 switch (*tmp)
142                                 {
143                                         case '\0':
144                                         case 'd':
145                                         case 'D': /* days */
146                                                 interval *= 86400;
147                                                 if (*tmp != '\0')
148                                                         tmp++;
149                                                 break;
150                                         case 'm':
151                                         case 'M': /* months! */
152                                                 interval *= 86400 * 30;
153                                                 tmp++;
154                                                 break;
155                                         case 'w':
156                                         case 'W': /* weeks */
157                                                 interval *= 86400 * 7;
158                                                 tmp++;
159                                                 break;
160                                 }
161                                 if (*tmp || interval > (365 * 86400))
162                                 {
163                                         com_err (program_name, 0,
164                                                  "bad interval - %s", optarg);
165                                         usage ();
166                                 }
167                                 i_flag = 1;
168                                 break;
169                         case 'l':
170                                 l_flag = 1;
171                                 break;
172                         case 'm':
173                                 reserved_ratio = strtoul (optarg, &tmp, 0);
174                                 if (*tmp || reserved_ratio > 50)
175                                 {
176                                         com_err (program_name, 0,
177                                                  "bad reserved block ratio - %s",
178                                                  optarg);
179                                         usage ();
180                                 }
181                                 m_flag = 1;
182                                 break;
183                         case 'r':
184                                 reserved_blocks = strtoul (optarg, &tmp, 0);
185                                 if (*tmp)
186                                 {
187                                         com_err (program_name, 0,
188                                                  "bad reserved blocks count - %s",
189                                                  optarg);
190                                         usage ();
191                                 }
192                                 r_flag = 1;
193                                 break;
194                         case 'u':
195                                 resuid = strtoul (optarg, &tmp, 0);
196                                 if (*tmp)
197                                 {
198                                         pw = getpwnam (optarg);
199                                         if (pw == NULL)
200                                                 tmp = optarg;
201                                         else {
202                                                 resuid = pw->pw_uid;
203                                                 *tmp = 0;
204                                         }
205                                 }
206                                 if (*tmp)
207                                 {
208                                         com_err (program_name, 0,
209                                                  "bad uid/user name - %s",
210                                                  optarg);
211                                         usage ();
212                                 }
213                                 u_flag = 1;
214                                 break;
215                         default:
216                                 usage ();
217                 }
218         if (optind < argc - 1 || optind == argc)
219                 usage ();
220         if (!c_flag && !e_flag && !g_flag && !i_flag && !l_flag && !m_flag
221             && !r_flag && !u_flag)
222                 usage ();
223         device_name = argv[optind];
224         retval = ext2fs_open (device_name,
225                               (c_flag || e_flag || g_flag || i_flag || m_flag
226                                || r_flag || u_flag) ? EXT2_FLAG_RW : 0,
227                               0, 0, unix_io_manager, &fs);
228         if (retval)
229         {
230                 com_err (program_name, retval, "while trying to open %s",
231                          device_name);
232                 printf("Couldn't find valid filesystem superblock.\n");
233                 exit(1);
234         }
235
236         if (c_flag)
237         {
238                 fs->super->s_max_mnt_count = max_mount_count;
239                 ext2fs_mark_super_dirty(fs);
240                 printf ("Setting maximal mount count to %d\n", max_mount_count);
241         }
242         if (e_flag)
243         {
244                 fs->super->s_errors = errors;
245                 ext2fs_mark_super_dirty(fs);
246                 printf ("Setting error behavior to %d\n", errors);
247         }
248         if (g_flag)
249 #ifdef  EXT2_DEF_RESGID
250         {
251                 fs->super->s_def_resgid = resgid;
252                 ext2fs_mark_super_dirty(fs);
253                 printf ("Setting reserved blocks gid to %lu\n", resgid);
254         }
255 #else
256                 com_err (program_name, 0,
257                          "The -g option is not supported by this version -- "
258                          "Recompile with a newer kernel");
259 #endif
260         if (i_flag)
261         {
262                 fs->super->s_checkinterval = interval;
263                 ext2fs_mark_super_dirty(fs);
264                 printf ("Setting interval between check %lu seconds\n", interval);
265         }
266         if (m_flag)
267         {
268                 fs->super->s_r_blocks_count = (fs->super->s_blocks_count / 100)
269                         * reserved_ratio;
270                 ext2fs_mark_super_dirty(fs);
271                 printf ("Setting reserved blocks percentage to %lu (%lu blocks)\n",
272                         reserved_ratio, fs->super->s_r_blocks_count);
273         }
274         if (r_flag)
275         {
276                 if (reserved_blocks >= fs->super->s_blocks_count)
277                 {
278                         com_err (program_name, 0,
279                                  "reserved blocks count is too big (%ul)",
280                                  reserved_blocks);
281                         exit (1);
282                 }
283                 fs->super->s_r_blocks_count = reserved_blocks;
284                 ext2fs_mark_super_dirty(fs);
285                 printf ("Setting reserved blocks count to %lu\n",
286                         reserved_blocks);
287         }
288         if (u_flag)
289 #ifdef  EXT2_DEF_RESUID
290         {
291                 fs->super->s_def_resuid = resuid;
292                 ext2fs_mark_super_dirty(fs);
293                 printf ("Setting reserved blocks uid to %lu\n", resuid);
294         }
295 #else
296                 com_err (program_name, 0,
297                          "The -u option is not supported by this version -- "
298                          "Recompile with a newer kernel");
299 #endif
300         if (l_flag)
301                 list_super (fs->super);
302         ext2fs_close (fs);
303         exit (0);
304 }