Whamcloud - gitweb
ChangeLog, e2image.c, mke2fs.c, mklost+found.c, tune2fs.c, util.c, uuidgen.c:
[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  * Copyright 1995, 1996, 1997 by Theodore Ts'o.
10  *
11  * %Begin-Header%
12  * This file may be redistributed under the terms of the GNU Public
13  * License.
14  * %End-Header%
15  */
16
17 /*
18  * History:
19  * 93/06/01     - Creation
20  * 93/10/31     - Added the -c option to change the maximal mount counts
21  * 93/12/14     - Added -l flag to list contents of superblock
22  *                M.J.E. Mol (marcel@duteca.et.tudelft.nl)
23  *                F.W. ten Wolde (franky@duteca.et.tudelft.nl)
24  * 93/12/29     - Added the -e option to change errors behavior
25  * 94/02/27     - Ported to use the ext2fs library
26  * 94/03/06     - Added the checks interval from Uwe Ohse (uwe@tirka.gun.de)
27  */
28
29 #include <fcntl.h>
30 #include <grp.h>
31 #ifdef HAVE_GETOPT_H
32 #include <getopt.h>
33 #else
34 extern char *optarg;
35 extern int optind;
36 #endif
37 #include <pwd.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <time.h>
42 #include <unistd.h>
43 #include <sys/types.h>
44
45 #include <linux/ext2_fs.h>
46
47 #include "ext2fs/ext2fs.h"
48 #include "et/com_err.h"
49 #include "uuid/uuid.h"
50 #include "e2p/e2p.h"
51 #include "util.h"
52
53 #include "../version.h"
54 #include "nls-enable.h"
55
56 const char * program_name = "tune2fs";
57 char * device_name;
58 char * new_label, *new_last_mounted, *new_UUID;
59 const char *journal_opts;
60 static int c_flag, C_flag, e_flag, g_flag, i_flag, l_flag, L_flag;
61 static int m_flag, M_flag, r_flag, s_flag = -1, u_flag, U_flag;
62 static int print_label;
63 static int max_mount_count, mount_count, mount_flags;
64 static unsigned long interval, reserved_ratio, reserved_blocks;
65 static unsigned long resgid, resuid;
66 static unsigned short errors;
67 static int open_flag;
68 static char *features_cmd;
69
70 int journal_size, journal_flags;
71 char *journal_device;
72
73 static const char *please_fsck = N_("Please run e2fsck on the filesystem.\n");
74
75 static void usage(void)
76 {
77         fprintf(stderr,
78                 _("Usage: %s [-c max-mounts-count] [-e errors-behavior] "
79                   "[-g group]\n"
80                  "\t[-i interval[d|m|w]] [-j journal-options]\n"
81                  "\t[-l] [-s sparse-flag] [-m reserved-blocks-percent]\n"
82                   "\t[-r reserved-blocks-count] [-u user] [-C mount-count]\n"
83                   "\t[-L volume-label] [-M last-mounted-dir] [-U UUID]\n"
84                   "\t[-O [^]feature[,...]] device\n"), program_name);
85         exit (1);
86 }
87
88 static __u32 ok_features[3] = {
89         EXT3_FEATURE_COMPAT_HAS_JOURNAL,        /* Compat */
90         EXT2_FEATURE_INCOMPAT_FILETYPE,         /* Incompat */
91         EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER     /* R/O compat */
92 };
93
94 /*
95  * Update the feature set as provided by the user.
96  */
97 static void update_feature_set(ext2_filsys fs, char *features)
98 {
99         int sparse, old_sparse, filetype, old_filetype;
100         int journal, old_journal;
101         struct ext2_inode       inode;
102         struct ext2_super_block *sb= fs->super;
103         errcode_t               retval;
104
105         old_sparse = sb->s_feature_ro_compat &
106                 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
107         old_filetype = sb->s_feature_incompat &
108                 EXT2_FEATURE_INCOMPAT_FILETYPE;
109         old_journal = sb->s_feature_compat &
110                 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
111         if (e2p_edit_feature(features, &sb->s_feature_compat,
112                              ok_features)) {
113                 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
114                         features);
115                 exit(1);
116         }
117         sparse = sb->s_feature_ro_compat &
118                 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
119         filetype = sb->s_feature_incompat &
120                 EXT2_FEATURE_INCOMPAT_FILETYPE;
121         journal = sb->s_feature_compat &
122                 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
123         if (old_journal && !journal) {
124                 if ((mount_flags & EXT2_MF_MOUNTED) &&
125                     !(mount_flags & EXT2_MF_READONLY)) {
126                         fprintf(stderr,
127                                 _("The HAS_JOURNAL flag may only be "
128                                   "cleared when the filesystem is\n"
129                                   "unmounted or mounted "
130                                   "read-only.\n"));
131                         exit(1);
132                 }
133                 if (sb->s_feature_incompat &
134                     EXT3_FEATURE_INCOMPAT_RECOVER) {
135                         fprintf(stderr,
136                                 _("The NEEDS_RECOVERY flag is set.  "
137                                   "Please run e2fsck before clearing\n"
138                                   "the HAS_JOURNAL flag.\n"));
139                         exit(1);
140                 }
141                 /*
142                  * Remove the immutable flag on the journal inode
143                  */
144                 if (sb->s_journal_inum) {
145                         retval = ext2fs_read_inode(fs, sb->s_journal_inum, 
146                                                    &inode);
147                         if (retval) {
148                                 com_err(program_name, retval,
149                                         "while reading journal inode");
150                                 exit(1);
151                         }
152                         inode.i_flags &= ~EXT2_IMMUTABLE_FL;
153                         retval = ext2fs_write_inode(fs, sb->s_journal_inum, 
154                                                     &inode);
155                         if (retval) {
156                                 com_err(program_name, retval,
157                                         "while write journal inode");
158                                 exit(1);
159                         }
160                 }
161         }
162         if (journal && !old_journal) {
163                 /*
164                  * If adding a journal flag, let the create journal
165                  * code below handle creating setting the flag and
166                  * creating the journal.  We supply a default size if
167                  * necessary.
168                  */
169                 if (!journal_opts)
170                         journal_opts = "size=16";
171                 sb->s_feature_compat &=~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
172                 journal = old_journal;
173         }
174         
175         if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
176             (sb->s_feature_compat || sb->s_feature_ro_compat ||
177              sb->s_feature_incompat))
178                 ext2fs_update_dynamic_rev(fs);
179         if ((sparse != old_sparse) ||
180             (filetype != old_filetype) ||
181             (journal != old_journal)) {
182                 sb->s_state &= ~EXT2_VALID_FS;
183                 printf("\n%s\n", _(please_fsck));
184         }
185         ext2fs_mark_super_dirty(fs);
186 }
187
188 /*
189  * Add a journal to the filesystem.
190  */
191 static void add_journal(ext2_filsys fs)
192 {
193         unsigned long journal_blocks;
194         errcode_t       retval;
195
196         if (fs->super->s_feature_compat &
197             EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
198                 fprintf(stderr, _("The filesystem already has a journal.\n"));
199                 exit(1);
200         }
201         parse_journal_opts(journal_opts);
202         journal_blocks = journal_size * 1024 / (fs->blocksize / 1024);
203         if (journal_device) {
204                 check_plausibility(journal_device);
205                 check_mount(journal_device, 0, _("journal"));
206                 printf(_("Creating journal on device %s: "),
207                        journal_device);
208                 retval = ext2fs_add_journal_device(fs, journal_device,
209                                                    journal_blocks,
210                                                    journal_flags);
211                 if (retval) {
212                         com_err (program_name, retval,
213                                  _("while trying to create journal on device %s"),
214                                  journal_device);
215                         exit(1);
216                 }
217                 printf(_("done\n"));
218         } else if (journal_size) {
219                 printf(_("Creating journal inode: "));
220                 fflush(stdout);
221                 retval = ext2fs_add_journal_inode(fs, journal_blocks,
222                                                   journal_flags);
223                 if (retval) {
224                         printf("\n");
225                         com_err(program_name, retval,
226                                 _("while trying to create journal"));
227                         exit(1);
228                 }
229                 printf(_("done\n"));
230                 /*
231                  * If the filesystem wasn't mounted, we need to force
232                  * the block group descriptors out.
233                  */
234                 if ((mount_flags & EXT2_MF_MOUNTED) == 0)
235                         fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
236         }
237 }
238
239 /*
240  * Given argv[0], return the program name.
241  */
242 static char *get_progname(char *argv_zero)
243 {
244         char    *cp;
245
246         cp = strrchr(argv_zero, '/');
247         if (!cp )
248                 return argv_zero;
249         else
250                 return cp+1;
251 }
252
253
254 static void parse_e2label_options(int argc, char ** argv)
255 {
256         if ((argc < 2) || (argc > 3)) {
257                 fprintf(stderr, _("Usage: e2label device [newlabel]\n"));
258                 exit(1);
259         }
260         device_name = argv[1];
261         if (argc == 3) {
262                 open_flag = EXT2_FLAG_RW;
263                 L_flag = 1;
264                 new_label = argv[2];
265         } else 
266                 print_label++;
267 }
268
269
270 static void parse_tune2fs_options(int argc, char **argv)
271 {
272         int c;
273         char * tmp;
274         struct group * gr;
275         struct passwd * pw;
276
277         fprintf (stderr, _("tune2fs %s, %s for EXT2 FS %s, %s\n"),
278                  E2FSPROGS_VERSION, E2FSPROGS_DATE,
279                  EXT2FS_VERSION, EXT2FS_DATE);
280         while ((c = getopt (argc, argv, "c:e:g:i:j:lm:r:s:u:C:L:M:O:U:")) != EOF)
281                 switch (c)
282                 {
283                         case 'c':
284                                 max_mount_count = strtol (optarg, &tmp, 0);
285                                 if (*tmp || max_mount_count > 16000) {
286                                         com_err (program_name, 0,
287                                                  _("bad mounts count - %s"),
288                                                  optarg);
289                                         usage();
290                                 }
291                                 c_flag = 1;
292                                 open_flag = EXT2_FLAG_RW;
293                                 break;
294                         case 'C':
295                                 mount_count = strtoul (optarg, &tmp, 0);
296                                 if (*tmp || mount_count > 16000) {
297                                         com_err (program_name, 0,
298                                                  _("bad mounts count - %s"),
299                                                  optarg);
300                                         usage();
301                                 }
302                                 C_flag = 1;
303                                 open_flag = EXT2_FLAG_RW;
304                                 break;
305                         case 'e':
306                                 if (strcmp (optarg, "continue") == 0)
307                                         errors = EXT2_ERRORS_CONTINUE;
308                                 else if (strcmp (optarg, "remount-ro") == 0)
309                                         errors = EXT2_ERRORS_RO;
310                                 else if (strcmp (optarg, "panic") == 0)
311                                         errors = EXT2_ERRORS_PANIC;
312                                 else {
313                                         com_err (program_name, 0,
314                                                  _("bad error behavior - %s"),
315                                                  optarg);
316                                         usage();
317                                 }
318                                 e_flag = 1;
319                                 open_flag = EXT2_FLAG_RW;
320                                 break;
321                         case 'g':
322                                 resgid = strtoul (optarg, &tmp, 0);
323                                 if (*tmp) {
324                                         gr = getgrnam (optarg);
325                                         if (gr == NULL)
326                                                 tmp = optarg;
327                                         else {
328                                                 resgid = gr->gr_gid;
329                                                 *tmp =0;
330                                         }
331                                 }
332                                 if (*tmp) {
333                                         com_err (program_name, 0,
334                                                  _("bad gid/group name - %s"),
335                                                  optarg);
336                                         usage();
337                                 }
338                                 g_flag = 1;
339                                 open_flag = EXT2_FLAG_RW;
340                                 break;
341                         case 'i':
342                                 interval = strtoul (optarg, &tmp, 0);
343                                 switch (*tmp) {
344                                 case 's':
345                                         tmp++;
346                                         break;
347                                 case '\0':
348                                 case 'd':
349                                 case 'D': /* days */
350                                         interval *= 86400;
351                                         if (*tmp != '\0')
352                                                 tmp++;
353                                         break;
354                                 case 'm':
355                                 case 'M': /* months! */
356                                         interval *= 86400 * 30;
357                                         tmp++;
358                                         break;
359                                 case 'w':
360                                 case 'W': /* weeks */
361                                         interval *= 86400 * 7;
362                                         tmp++;
363                                         break;
364                                 }
365                                 if (*tmp || interval > (365 * 86400)) {
366                                         com_err (program_name, 0,
367                                                 _("bad interval - %s"), optarg);
368                                         usage();
369                                 }
370                                 i_flag = 1;
371                                 open_flag = EXT2_FLAG_RW;
372                                 break;
373                         case 'l':
374                                 l_flag = 1;
375                                 break;
376                         case 'j':
377                                 journal_opts = optarg;
378                                 open_flag = EXT2_FLAG_RW;
379                                 break;
380                         case 'L':
381                                 new_label = optarg;
382                                 L_flag = 1;
383                                 open_flag = EXT2_FLAG_RW;
384                                 break;
385                         case 'm':
386                                 reserved_ratio = strtoul (optarg, &tmp, 0);
387                                 if (*tmp || reserved_ratio > 50) {
388                                         com_err (program_name, 0,
389                                                  _("bad reserved block ratio - %s"),
390                                                  optarg);
391                                         usage();
392                                 }
393                                 m_flag = 1;
394                                 open_flag = EXT2_FLAG_RW;
395                                 break;
396                         case 'M':
397                                 new_last_mounted = optarg;
398                                 M_flag = 1;
399                                 open_flag = EXT2_FLAG_RW;
400                                 break;
401                         case 'O':
402                                 features_cmd = optarg;
403                                 open_flag = EXT2_FLAG_RW;
404                                 break;
405                         case 'r':
406                                 reserved_blocks = strtoul (optarg, &tmp, 0);
407                                 if (*tmp) {
408                                         com_err (program_name, 0,
409                                                  _("bad reserved blocks count - %s"),
410                                                  optarg);
411                                         usage();
412                                 }
413                                 r_flag = 1;
414                                 open_flag = EXT2_FLAG_RW;
415                                 break;
416                         case 's':
417                                 s_flag = atoi(optarg);
418                                 open_flag = EXT2_FLAG_RW;
419                                 break;
420                         case 'u':
421                                 resuid = strtoul (optarg, &tmp, 0);
422                                 if (*tmp) {
423                                         pw = getpwnam (optarg);
424                                         if (pw == NULL)
425                                                 tmp = optarg;
426                                         else {
427                                                 resuid = pw->pw_uid;
428                                                 *tmp = 0;
429                                         }
430                                 }
431                                 if (*tmp) {
432                                         com_err (program_name, 0,
433                                                  _("bad uid/user name - %s"),
434                                                  optarg);
435                                         usage();
436                                 }
437                                 u_flag = 1;
438                                 open_flag = EXT2_FLAG_RW;
439                                 break;
440                         case 'U':
441                                 new_UUID = optarg;
442                                 U_flag = 1;
443                                 open_flag = EXT2_FLAG_RW;
444                                 break;
445                         default:
446                                 usage();
447                 }
448         if (optind < argc - 1 || optind == argc)
449                 usage();
450         if (!open_flag && !l_flag)
451                 usage();
452         device_name = argv[optind];
453 }       
454
455
456
457 int main (int argc, char ** argv)
458 {
459         errcode_t retval;
460         ext2_filsys fs;
461         struct ext2_super_block *sb;
462
463 #ifdef ENABLE_NLS
464         setlocale(LC_MESSAGES, "");
465         bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
466         textdomain(NLS_CAT_NAME);
467 #endif
468         if (argc && *argv)
469                 program_name = *argv;
470         initialize_ext2_error_table();
471
472         if (strcmp(get_progname(argv[0]), "e2label") == 0)
473                 parse_e2label_options(argc, argv);
474         else
475                 parse_tune2fs_options(argc, argv);
476         
477         retval = ext2fs_open (device_name, open_flag, 0, 0,
478                               unix_io_manager, &fs);
479         if (retval) {
480                 com_err (program_name, retval, _("while trying to open %s"),
481                          device_name);
482                 printf(_("Couldn't find valid filesystem superblock.\n"));
483                 exit(1);
484         }
485         sb = fs->super;
486         if (print_label) {
487                 /* For e2label emulation */
488                 printf("%.*s\n", (int) sizeof(sb->s_volume_name),
489                        sb->s_volume_name);
490                 exit(0);
491         }
492         retval = ext2fs_check_if_mounted(device_name, &mount_flags);
493         if (retval) {
494                 com_err("ext2fs_check_if_mount", retval,
495                         _("while determining whether %s is mounted."),
496                         device_name);
497                 exit(0);
498         }
499         /* Normally we only need to write out the superblock */
500         fs->flags |= EXT2_FLAG_SUPER_ONLY;
501
502         if (c_flag) {
503                 sb->s_max_mnt_count = max_mount_count;
504                 ext2fs_mark_super_dirty(fs);
505                 printf (_("Setting maximal mount count to %d\n"),
506                         max_mount_count);
507         }
508         if (C_flag) {
509                 sb->s_mnt_count = mount_count;
510                 ext2fs_mark_super_dirty(fs);
511                 printf (_("Setting current mount count to %d\n"), mount_count);
512         }
513         if (e_flag) {
514                 sb->s_errors = errors;
515                 ext2fs_mark_super_dirty(fs);
516                 printf (_("Setting error behavior to %d\n"), errors);
517         }
518         if (g_flag) {
519                 sb->s_def_resgid = resgid;
520                 ext2fs_mark_super_dirty(fs);
521                 printf (_("Setting reserved blocks gid to %lu\n"), resgid);
522         }
523         if (i_flag) {
524                 sb->s_checkinterval = interval;
525                 ext2fs_mark_super_dirty(fs);
526                 printf (_("Setting interval between check %lu seconds\n"), interval);
527         }
528         if (m_flag) {
529                 sb->s_r_blocks_count = (sb->s_blocks_count / 100)
530                         * reserved_ratio;
531                 ext2fs_mark_super_dirty(fs);
532                 printf (_("Setting reserved blocks percentage to %lu (%u blocks)\n"),
533                         reserved_ratio, sb->s_r_blocks_count);
534         }
535         if (r_flag) {
536                 if (reserved_blocks >= sb->s_blocks_count) {
537                         com_err (program_name, 0,
538                                  _("reserved blocks count is too big (%ul)"),
539                                  reserved_blocks);
540                         exit (1);
541                 }
542                 sb->s_r_blocks_count = reserved_blocks;
543                 ext2fs_mark_super_dirty(fs);
544                 printf (_("Setting reserved blocks count to %lu\n"),
545                         reserved_blocks);
546         }
547         if (s_flag == 1) {
548                 if (sb->s_feature_ro_compat &
549                     EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)
550                         fprintf(stderr, _("\nThe filesystem already"
551                                 " has sparse superblocks.\n"));
552                 else {
553                         sb->s_feature_ro_compat |=
554                                 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
555                         sb->s_state &= ~EXT2_VALID_FS;
556                         ext2fs_mark_super_dirty(fs);
557                         printf(_("\nSparse superblock flag set.  %s"),
558                                _(please_fsck));
559                 }
560         }
561         if (s_flag == 0) {
562                 if (!(sb->s_feature_ro_compat &
563                       EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER))
564                         fprintf(stderr, _("\nThe filesystem already"
565                                 " has sparse superblocks disabled.\n"));
566                 else {
567                         sb->s_feature_ro_compat &=
568                                 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
569                         sb->s_state &= ~EXT2_VALID_FS;
570                         fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
571                         ext2fs_mark_super_dirty(fs);
572                         printf(_("\nSparse superblock flag cleared.  %s"),
573                                _(please_fsck));
574                 }
575         }
576         if (u_flag) {
577                 sb->s_def_resuid = resuid;
578                 ext2fs_mark_super_dirty(fs);
579                 printf (_("Setting reserved blocks uid to %lu\n"), resuid);
580         }
581         if (L_flag) {
582                 if (strlen(new_label) > sizeof(sb->s_volume_name))
583                         fprintf(stderr, _("Warning: label too "
584                                 "long, truncating.\n"));
585                 memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name));
586                 strncpy(sb->s_volume_name, new_label,
587                         sizeof(sb->s_volume_name));
588                 ext2fs_mark_super_dirty(fs);
589         }
590         if (M_flag) {
591                 memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted));
592                 strncpy(sb->s_last_mounted, new_last_mounted,
593                         sizeof(sb->s_last_mounted));
594                 ext2fs_mark_super_dirty(fs);
595         }
596         if (features_cmd)
597                 update_feature_set(fs, features_cmd);
598         if (journal_opts)
599                 add_journal(fs);
600         
601         if (U_flag) {
602                 if (strcasecmp(new_UUID, "null") == 0) {
603                         uuid_clear(sb->s_uuid);
604                 } else if (strcasecmp(new_UUID, "time") == 0) {
605                         uuid_generate_time(sb->s_uuid);
606                 } else if (strcasecmp(new_UUID, "random") == 0) {
607                         uuid_generate(sb->s_uuid);
608                 } else if (uuid_parse(new_UUID, sb->s_uuid)) {
609                         com_err(program_name, 0, _("Invalid UUID format\n"));
610                         exit(1);
611                 }
612                 ext2fs_mark_super_dirty(fs);
613         }
614
615         if (l_flag)
616                 list_super (sb);
617         ext2fs_close (fs);
618         exit (0);
619 }