tune2fs.c revision a8862d9e90d0d94761ba28dbbf9674308fd7d7c0
1/*
2 * tune2fs.c - Change the file system parameters on an ext2 file system
3 *
4 * Copyright (C) 1992, 1993, 1994  Remy Card <card@masi.ibp.fr>
5 *                                 Laboratoire MASI, Institut Blaise Pascal
6 *                                 Universite Pierre et Marie Curie (Paris VI)
7 *
8 * Copyright 1995, 1996, 1997, 1998, 1999, 2000 by Theodore Ts'o.
9 *
10 * %Begin-Header%
11 * This file may be redistributed under the terms of the GNU Public
12 * License.
13 * %End-Header%
14 */
15
16/*
17 * History:
18 * 93/06/01	- Creation
19 * 93/10/31	- Added the -c option to change the maximal mount counts
20 * 93/12/14	- Added -l flag to list contents of superblock
21 *                M.J.E. Mol (marcel@duteca.et.tudelft.nl)
22 *                F.W. ten Wolde (franky@duteca.et.tudelft.nl)
23 * 93/12/29	- Added the -e option to change errors behavior
24 * 94/02/27	- Ported to use the ext2fs library
25 * 94/03/06	- Added the checks interval from Uwe Ohse (uwe@tirka.gun.de)
26 */
27
28#define _XOPEN_SOURCE 500 /* for inclusion of strptime() */
29#define _BSD_SOURCE /* for inclusion of strcasecmp() */
30#include <fcntl.h>
31#include <grp.h>
32#ifdef HAVE_GETOPT_H
33#include <getopt.h>
34#else
35extern char *optarg;
36extern int optind;
37#endif
38#include <pwd.h>
39#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42#include <time.h>
43#include <unistd.h>
44#include <sys/types.h>
45
46#include "ext2fs/ext2_fs.h"
47#include "ext2fs/ext2fs.h"
48#include "et/com_err.h"
49#include "uuid/uuid.h"
50#include "e2p/e2p.h"
51#include "jfs_user.h"
52#include "util.h"
53#include "blkid/blkid.h"
54
55#include "../version.h"
56#include "nls-enable.h"
57
58const char * program_name = "tune2fs";
59char * device_name;
60char * new_label, *new_last_mounted, *new_UUID;
61char * io_options;
62static int c_flag, C_flag, e_flag, f_flag, g_flag, i_flag, l_flag, L_flag;
63static int m_flag, M_flag, r_flag, s_flag = -1, u_flag, U_flag, T_flag;
64static time_t last_check_time;
65static int print_label;
66static int max_mount_count, mount_count, mount_flags;
67static unsigned long interval, reserved_blocks;
68static double reserved_ratio;
69static unsigned long resgid, resuid;
70static unsigned short errors;
71static int open_flag;
72static char *features_cmd;
73static char *mntopts_cmd;
74
75int journal_size, journal_flags;
76char *journal_device;
77
78static const char *please_fsck = N_("Please run e2fsck on the filesystem.\n");
79
80void do_findfs(int argc, char **argv);
81
82static void usage(void)
83{
84	fprintf(stderr,
85		_("Usage: %s [-c max_mounts_count] [-e errors_behavior] "
86		  "[-g group]\n"
87		  "\t[-i interval[d|m|w]] [-j] [-J journal_options]\n"
88		  "\t[-l] [-s sparse_flag] [-m reserved_blocks_percent]\n"
89		  "\t[-o [^]mount_options[,...]] [-r reserved_blocks_count]\n"
90		  "\t[-u user] [-C mount_count] [-L volume_label] "
91		  "[-M last_mounted_dir]\n"
92		  "\t[-O [^]feature[,...]] [-T last_check_time] [-U UUID]"
93		  " device\n"), program_name);
94	exit (1);
95}
96
97static __u32 ok_features[3] = {
98	EXT3_FEATURE_COMPAT_HAS_JOURNAL |
99		EXT2_FEATURE_COMPAT_DIR_INDEX,	/* Compat */
100	EXT2_FEATURE_INCOMPAT_FILETYPE,		/* Incompat */
101	EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER	/* R/O compat */
102};
103
104/*
105 * Remove an external journal from the filesystem
106 */
107static void remove_journal_device(ext2_filsys fs)
108{
109	char		*journal_path;
110	ext2_filsys	jfs;
111	char		buf[1024];
112	journal_superblock_t	*jsb;
113	int		i, nr_users;
114	errcode_t	retval;
115	int		commit_remove_journal = 0;
116	io_manager	io_ptr;
117
118	if (f_flag)
119		commit_remove_journal = 1; /* force removal even if error */
120
121	uuid_unparse(fs->super->s_journal_uuid, buf);
122	journal_path = blkid_get_devname(NULL, "UUID", buf);
123
124	if (!journal_path) {
125		journal_path =
126			ext2fs_find_block_device(fs->super->s_journal_dev);
127		if (!journal_path)
128			return;
129	}
130
131#ifdef CONFIG_TESTIO_DEBUG
132	io_ptr = test_io_manager;
133	test_io_backing_manager = unix_io_manager;
134#else
135	io_ptr = unix_io_manager;
136#endif
137	retval = ext2fs_open(journal_path, EXT2_FLAG_RW|
138			     EXT2_FLAG_JOURNAL_DEV_OK, 0,
139			     fs->blocksize, io_ptr, &jfs);
140	if (retval) {
141		com_err(program_name, retval,
142			_("while trying to open external journal"));
143		goto no_valid_journal;
144	}
145	if (!(jfs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
146		fprintf(stderr, _("%s is not a journal device.\n"),
147			journal_path);
148		goto no_valid_journal;
149	}
150
151	/* Get the journal superblock */
152	if ((retval = io_channel_read_blk(jfs->io, 1, -1024, buf))) {
153		com_err(program_name, retval,
154			_("while reading journal superblock"));
155		goto no_valid_journal;
156	}
157
158	jsb = (journal_superblock_t *) buf;
159	if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
160	    (jsb->s_header.h_blocktype != (unsigned) ntohl(JFS_SUPERBLOCK_V2))) {
161		fputs(_("Journal superblock not found!\n"), stderr);
162		goto no_valid_journal;
163	}
164
165	/* Find the filesystem UUID */
166	nr_users = ntohl(jsb->s_nr_users);
167	for (i=0; i < nr_users; i++) {
168		if (memcmp(fs->super->s_uuid,
169			   &jsb->s_users[i*16], 16) == 0)
170			break;
171	}
172	if (i >= nr_users) {
173		fputs(_("Filesystem's UUID not found on journal device.\n"),
174		      stderr);
175		commit_remove_journal = 1;
176		goto no_valid_journal;
177	}
178	nr_users--;
179	for (i=0; i < nr_users; i++)
180		memcpy(&jsb->s_users[i*16], &jsb->s_users[(i+1)*16], 16);
181	jsb->s_nr_users = htonl(nr_users);
182
183	/* Write back the journal superblock */
184	if ((retval = io_channel_write_blk(jfs->io, 1, -1024, buf))) {
185		com_err(program_name, retval,
186			"while writing journal superblock.");
187		goto no_valid_journal;
188	}
189
190	commit_remove_journal = 1;
191
192no_valid_journal:
193	if (commit_remove_journal == 0) {
194		fputs(_("Journal NOT removed\n"), stderr);
195		exit(1);
196	}
197	fs->super->s_journal_dev = 0;
198	uuid_clear(fs->super->s_journal_uuid);
199	ext2fs_mark_super_dirty(fs);
200	fputs(_("Journal removed\n"), stdout);
201	free(journal_path);
202}
203
204/* Helper function for remove_journal_inode */
205static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
206			       int blockcnt EXT2FS_ATTR((unused)),
207			       void *private EXT2FS_ATTR((unused)))
208{
209	blk_t	block;
210	int	group;
211
212	block = *blocknr;
213	ext2fs_unmark_block_bitmap(fs->block_map,block);
214	group = ext2fs_group_of_blk(fs, block);
215	fs->group_desc[group].bg_free_blocks_count++;
216	fs->super->s_free_blocks_count++;
217	return 0;
218}
219
220/*
221 * Remove the journal inode from the filesystem
222 */
223static void remove_journal_inode(ext2_filsys fs)
224{
225	struct ext2_inode	inode;
226	errcode_t		retval;
227	ino_t			ino = fs->super->s_journal_inum;
228
229	retval = ext2fs_read_inode(fs, ino,  &inode);
230	if (retval) {
231		com_err(program_name, retval,
232			_("while reading journal inode"));
233		exit(1);
234	}
235	if (ino == EXT2_JOURNAL_INO) {
236		retval = ext2fs_read_bitmaps(fs);
237		if (retval) {
238			com_err(program_name, retval,
239				_("while reading bitmaps"));
240			exit(1);
241		}
242		retval = ext2fs_block_iterate(fs, ino, 0, NULL,
243					      release_blocks_proc, NULL);
244		if (retval) {
245			com_err(program_name, retval,
246				_("while clearing journal inode"));
247			exit(1);
248		}
249		memset(&inode, 0, sizeof(inode));
250		ext2fs_mark_bb_dirty(fs);
251		fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
252	} else
253		inode.i_flags &= ~EXT2_IMMUTABLE_FL;
254	retval = ext2fs_write_inode(fs, ino, &inode);
255	if (retval) {
256		com_err(program_name, retval,
257			_("while writing journal inode"));
258		exit(1);
259	}
260	fs->super->s_journal_inum = 0;
261	ext2fs_mark_super_dirty(fs);
262}
263
264/*
265 * Update the default mount options
266 */
267static void update_mntopts(ext2_filsys fs, char *mntopts)
268{
269	struct ext2_super_block *sb= fs->super;
270
271	if (e2p_edit_mntopts(mntopts, &sb->s_default_mount_opts, ~0)) {
272		fprintf(stderr, _("Invalid mount option set: %s\n"),
273			mntopts);
274		exit(1);
275	}
276	ext2fs_mark_super_dirty(fs);
277}
278
279/*
280 * Update the feature set as provided by the user.
281 */
282static void update_feature_set(ext2_filsys fs, char *features)
283{
284	int sparse, old_sparse, filetype, old_filetype;
285	int journal, old_journal, dxdir, old_dxdir;
286	struct ext2_super_block *sb= fs->super;
287	__u32	old_compat, old_incompat, old_ro_compat;
288
289	old_compat = sb->s_feature_compat;
290	old_ro_compat = sb->s_feature_ro_compat;
291	old_incompat = sb->s_feature_incompat;
292
293	old_sparse = sb->s_feature_ro_compat &
294		EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
295	old_filetype = sb->s_feature_incompat &
296		EXT2_FEATURE_INCOMPAT_FILETYPE;
297	old_journal = sb->s_feature_compat &
298		EXT3_FEATURE_COMPAT_HAS_JOURNAL;
299	old_dxdir = sb->s_feature_compat &
300		EXT2_FEATURE_COMPAT_DIR_INDEX;
301	if (e2p_edit_feature(features, &sb->s_feature_compat,
302			     ok_features)) {
303		fprintf(stderr, _("Invalid filesystem option set: %s\n"),
304			features);
305		exit(1);
306	}
307	sparse = sb->s_feature_ro_compat &
308		EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
309	filetype = sb->s_feature_incompat &
310		EXT2_FEATURE_INCOMPAT_FILETYPE;
311	journal = sb->s_feature_compat &
312		EXT3_FEATURE_COMPAT_HAS_JOURNAL;
313	dxdir = sb->s_feature_compat &
314		EXT2_FEATURE_COMPAT_DIR_INDEX;
315	if (old_journal && !journal) {
316		if ((mount_flags & EXT2_MF_MOUNTED) &&
317		    !(mount_flags & EXT2_MF_READONLY)) {
318			fputs(_("The has_journal flag may only be "
319				"cleared when the filesystem is\n"
320				"unmounted or mounted "
321				"read-only.\n"), stderr);
322			exit(1);
323		}
324		if (sb->s_feature_incompat &
325		    EXT3_FEATURE_INCOMPAT_RECOVER) {
326			fputs(_("The needs_recovery flag is set.  "
327				"Please run e2fsck before clearing\n"
328				"the has_journal flag.\n"), stderr);
329			exit(1);
330		}
331		if (sb->s_journal_inum) {
332			remove_journal_inode(fs);
333		}
334		if (sb->s_journal_dev) {
335			remove_journal_device(fs);
336		}
337	}
338	if (journal && !old_journal) {
339		/*
340		 * If adding a journal flag, let the create journal
341		 * code below handle creating setting the flag and
342		 * creating the journal.  We supply a default size if
343		 * necessary.
344		 */
345		if (!journal_size)
346			journal_size = -1;
347		sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
348	}
349	if (dxdir && !old_dxdir) {
350		if (!sb->s_def_hash_version)
351			sb->s_def_hash_version = EXT2_HASH_TEA;
352		if (uuid_is_null((unsigned char *) sb->s_hash_seed))
353			uuid_generate((unsigned char *) sb->s_hash_seed);
354	}
355
356	if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
357	    (sb->s_feature_compat || sb->s_feature_ro_compat ||
358	     sb->s_feature_incompat))
359		ext2fs_update_dynamic_rev(fs);
360	if ((sparse != old_sparse) ||
361	    (filetype != old_filetype)) {
362		sb->s_state &= ~EXT2_VALID_FS;
363		printf("\n%s\n", _(please_fsck));
364	}
365	if ((old_compat != sb->s_feature_compat) ||
366	    (old_ro_compat != sb->s_feature_ro_compat) ||
367	    (old_incompat != sb->s_feature_incompat))
368		ext2fs_mark_super_dirty(fs);
369}
370
371/*
372 * Add a journal to the filesystem.
373 */
374static void add_journal(ext2_filsys fs)
375{
376	unsigned long journal_blocks;
377	errcode_t	retval;
378	ext2_filsys	jfs;
379	io_manager	io_ptr;
380
381	if (fs->super->s_feature_compat &
382	    EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
383		fputs(_("The filesystem already has a journal.\n"), stderr);
384		goto err;
385	}
386	if (journal_device) {
387		check_plausibility(journal_device);
388		check_mount(journal_device, 0, _("journal"));
389#ifdef CONFIG_TESTIO_DEBUG
390		io_ptr = test_io_manager;
391		test_io_backing_manager = unix_io_manager;
392#else
393		io_ptr = unix_io_manager;
394#endif
395		retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
396				     EXT2_FLAG_JOURNAL_DEV_OK, 0,
397				     fs->blocksize, io_ptr, &jfs);
398		if (retval) {
399			com_err(program_name, retval,
400				_("\n\twhile trying to open journal on %s\n"),
401				journal_device);
402			goto err;
403		}
404		printf(_("Creating journal on device %s: "),
405		       journal_device);
406		fflush(stdout);
407
408		retval = ext2fs_add_journal_device(fs, jfs);
409		ext2fs_close(jfs);
410		if (retval) {
411			com_err (program_name, retval,
412				 _("while adding filesystem to journal on %s"),
413				 journal_device);
414			goto err;
415		}
416		fputs(_("done\n"), stdout);
417	} else if (journal_size) {
418		fputs(_("Creating journal inode: "), stdout);
419		fflush(stdout);
420		journal_blocks = figure_journal_size(journal_size, fs);
421
422		retval = ext2fs_add_journal_inode(fs, journal_blocks,
423						  journal_flags);
424		if (retval) {
425			fprintf(stderr, "\n");
426			com_err(program_name, retval,
427				_("\n\twhile trying to create journal file"));
428			exit(1);
429		} else
430			fputs(_("done\n"), stdout);
431		/*
432		 * If the filesystem wasn't mounted, we need to force
433		 * the block group descriptors out.
434		 */
435		if ((mount_flags & EXT2_MF_MOUNTED) == 0)
436			fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
437	}
438	print_check_message(fs);
439	return;
440
441err:
442	if (journal_device)
443		free(journal_device);
444	exit(1);
445}
446
447
448static void parse_e2label_options(int argc, char ** argv)
449{
450	if ((argc < 2) || (argc > 3)) {
451		fputs(_("Usage: e2label device [newlabel]\n"), stderr);
452		exit(1);
453	}
454	io_options = strchr(argv[1], '?');
455	if (io_options)
456		*io_options++ = 0;
457	device_name = blkid_get_devname(NULL, argv[1], NULL);
458	if (!device_name) {
459		com_err("e2label", 0, _("Unable to resolve '%s'"),
460			argv[1]);
461		exit(1);
462	}
463	if (argc == 3) {
464		open_flag = EXT2_FLAG_RW | EXT2_FLAG_JOURNAL_DEV_OK;
465		L_flag = 1;
466		new_label = argv[2];
467	} else
468		print_label++;
469}
470
471static time_t parse_time(char *str)
472{
473	struct	tm	ts;
474
475	if (strcmp(str, "now") == 0) {
476		return (time(0));
477	}
478	memset(&ts, 0, sizeof(ts));
479#ifdef HAVE_STRPTIME
480	strptime(str, "%Y%m%d%H%M%S", &ts);
481#else
482	sscanf(str, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon,
483	       &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec);
484	ts.tm_year -= 1900;
485	ts.tm_mon -= 1;
486	if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 ||
487	    ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 ||
488	    ts.tm_min > 59 || ts.tm_sec > 61)
489		ts.tm_mday = 0;
490#endif
491	if (ts.tm_mday == 0) {
492		com_err(program_name, 0,
493			_("Couldn't parse date/time specifier: %s"),
494			str);
495		usage();
496	}
497	return (mktime(&ts));
498}
499
500static void parse_tune2fs_options(int argc, char **argv)
501{
502	int c;
503	char * tmp;
504	struct group * gr;
505	struct passwd * pw;
506
507	printf("tune2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
508	while ((c = getopt(argc, argv, "c:e:fg:i:jlm:o:r:s:u:C:J:L:M:O:T:U:")) != EOF)
509		switch (c)
510		{
511			case 'c':
512				max_mount_count = strtol (optarg, &tmp, 0);
513				if (*tmp || max_mount_count > 16000) {
514					com_err (program_name, 0,
515						 _("bad mounts count - %s"),
516						 optarg);
517					usage();
518				}
519				if (max_mount_count == 0)
520					max_mount_count = -1;
521				c_flag = 1;
522				open_flag = EXT2_FLAG_RW;
523				break;
524			case 'C':
525				mount_count = strtoul (optarg, &tmp, 0);
526				if (*tmp || mount_count > 16000) {
527					com_err (program_name, 0,
528						 _("bad mounts count - %s"),
529						 optarg);
530					usage();
531				}
532				C_flag = 1;
533				open_flag = EXT2_FLAG_RW;
534				break;
535			case 'e':
536				if (strcmp (optarg, "continue") == 0)
537					errors = EXT2_ERRORS_CONTINUE;
538				else if (strcmp (optarg, "remount-ro") == 0)
539					errors = EXT2_ERRORS_RO;
540				else if (strcmp (optarg, "panic") == 0)
541					errors = EXT2_ERRORS_PANIC;
542				else {
543					com_err (program_name, 0,
544						 _("bad error behavior - %s"),
545						 optarg);
546					usage();
547				}
548				e_flag = 1;
549				open_flag = EXT2_FLAG_RW;
550				break;
551			case 'f': /* Force */
552				f_flag = 1;
553				break;
554			case 'g':
555				resgid = strtoul (optarg, &tmp, 0);
556				if (*tmp) {
557					gr = getgrnam (optarg);
558					if (gr == NULL)
559						tmp = optarg;
560					else {
561						resgid = gr->gr_gid;
562						*tmp =0;
563					}
564				}
565				if (*tmp) {
566					com_err (program_name, 0,
567						 _("bad gid/group name - %s"),
568						 optarg);
569					usage();
570				}
571				g_flag = 1;
572				open_flag = EXT2_FLAG_RW;
573				break;
574			case 'i':
575				interval = strtoul (optarg, &tmp, 0);
576				switch (*tmp) {
577				case 's':
578					tmp++;
579					break;
580				case '\0':
581				case 'd':
582				case 'D': /* days */
583					interval *= 86400;
584					if (*tmp != '\0')
585						tmp++;
586					break;
587				case 'm':
588				case 'M': /* months! */
589					interval *= 86400 * 30;
590					tmp++;
591					break;
592				case 'w':
593				case 'W': /* weeks */
594					interval *= 86400 * 7;
595					tmp++;
596					break;
597				}
598				if (*tmp) {
599					com_err (program_name, 0,
600						_("bad interval - %s"), optarg);
601					usage();
602				}
603				i_flag = 1;
604				open_flag = EXT2_FLAG_RW;
605				break;
606			case 'j':
607				if (!journal_size)
608					journal_size = -1;
609				open_flag = EXT2_FLAG_RW;
610				break;
611			case 'J':
612				parse_journal_opts(optarg);
613				open_flag = EXT2_FLAG_RW;
614				break;
615			case 'l':
616				l_flag = 1;
617				break;
618			case 'L':
619				new_label = optarg;
620				L_flag = 1;
621				open_flag = EXT2_FLAG_RW |
622					EXT2_FLAG_JOURNAL_DEV_OK;
623				break;
624			case 'm':
625				reserved_ratio = strtod(optarg, &tmp);
626				if (*tmp || reserved_ratio > 50) {
627					com_err (program_name, 0,
628						 _("bad reserved block ratio - %s"),
629						 optarg);
630					usage();
631				}
632				m_flag = 1;
633				open_flag = EXT2_FLAG_RW;
634				break;
635			case 'M':
636				new_last_mounted = optarg;
637				M_flag = 1;
638				open_flag = EXT2_FLAG_RW;
639				break;
640			case 'o':
641				if (mntopts_cmd) {
642					com_err (program_name, 0,
643					 _("-o may only be specified once"));
644					usage();
645				}
646				mntopts_cmd = optarg;
647				open_flag = EXT2_FLAG_RW;
648				break;
649
650			case 'O':
651				if (features_cmd) {
652					com_err (program_name, 0,
653					 _("-O may only be specified once"));
654					usage();
655				}
656				features_cmd = optarg;
657				open_flag = EXT2_FLAG_RW;
658				break;
659			case 'r':
660				reserved_blocks = strtoul (optarg, &tmp, 0);
661				if (*tmp) {
662					com_err (program_name, 0,
663						 _("bad reserved blocks count - %s"),
664						 optarg);
665					usage();
666				}
667				r_flag = 1;
668				open_flag = EXT2_FLAG_RW;
669				break;
670			case 's':
671				s_flag = atoi(optarg);
672				open_flag = EXT2_FLAG_RW;
673				break;
674			case 'T':
675				T_flag = 1;
676				last_check_time = parse_time(optarg);
677				open_flag = EXT2_FLAG_RW;
678				break;
679			case 'u':
680				resuid = strtoul (optarg, &tmp, 0);
681				if (*tmp) {
682					pw = getpwnam (optarg);
683					if (pw == NULL)
684						tmp = optarg;
685					else {
686						resuid = pw->pw_uid;
687						*tmp = 0;
688					}
689				}
690				if (*tmp) {
691					com_err (program_name, 0,
692						 _("bad uid/user name - %s"),
693						 optarg);
694					usage();
695				}
696				u_flag = 1;
697				open_flag = EXT2_FLAG_RW;
698				break;
699			case 'U':
700				new_UUID = optarg;
701				U_flag = 1;
702				open_flag = EXT2_FLAG_RW |
703					EXT2_FLAG_JOURNAL_DEV_OK;
704				break;
705			default:
706				usage();
707		}
708	if (optind < argc - 1 || optind == argc)
709		usage();
710	if (!open_flag && !l_flag)
711		usage();
712	io_options = strchr(argv[optind], '?');
713	if (io_options)
714		*io_options++ = 0;
715	device_name = blkid_get_devname(NULL, argv[optind], NULL);
716	if (!device_name) {
717		com_err("tune2fs", 0, _("Unable to resolve '%s'"),
718			argv[optind]);
719		exit(1);
720	}
721}
722
723void do_findfs(int argc, char **argv)
724{
725	char	*dev;
726
727	if ((argc != 2) ||
728	    (strncmp(argv[1], "LABEL=", 6) && strncmp(argv[1], "UUID=", 5))) {
729		fprintf(stderr, "Usage: findfs LABEL=<label>|UUID=<uuid>\n");
730		exit(2);
731	}
732	dev = blkid_get_devname(NULL, argv[1], NULL);
733	if (!dev) {
734		com_err("findfs", 0, _("Unable to resolve '%s'"),
735			argv[1]);
736		exit(1);
737	}
738	puts(dev);
739	exit(0);
740}
741
742
743int main (int argc, char ** argv)
744{
745	errcode_t retval;
746	ext2_filsys fs;
747	struct ext2_super_block *sb;
748	io_manager io_ptr;
749
750#ifdef ENABLE_NLS
751	setlocale(LC_MESSAGES, "");
752	setlocale(LC_CTYPE, "");
753	bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
754	textdomain(NLS_CAT_NAME);
755#endif
756	if (argc && *argv)
757		program_name = *argv;
758	initialize_ext2_error_table();
759
760	if (strcmp(get_progname(argv[0]), "findfs") == 0)
761		do_findfs(argc, argv);
762	if (strcmp(get_progname(argv[0]), "e2label") == 0)
763		parse_e2label_options(argc, argv);
764	else
765		parse_tune2fs_options(argc, argv);
766
767#ifdef CONFIG_TESTIO_DEBUG
768	io_ptr = test_io_manager;
769	test_io_backing_manager = unix_io_manager;
770#else
771	io_ptr = unix_io_manager;
772#endif
773	retval = ext2fs_open2(device_name, io_options, open_flag,
774			      0, 0, io_ptr, &fs);
775        if (retval) {
776		com_err (program_name, retval, _("while trying to open %s"),
777			 device_name);
778		fprintf(stderr,
779			_("Couldn't find valid filesystem superblock.\n"));
780		exit(1);
781	}
782	sb = fs->super;
783	if (print_label) {
784		/* For e2label emulation */
785		printf("%.*s\n", (int) sizeof(sb->s_volume_name),
786		       sb->s_volume_name);
787		exit(0);
788	}
789	retval = ext2fs_check_if_mounted(device_name, &mount_flags);
790	if (retval) {
791		com_err("ext2fs_check_if_mount", retval,
792			_("while determining whether %s is mounted."),
793			device_name);
794		exit(1);
795	}
796	/* Normally we only need to write out the superblock */
797	fs->flags |= EXT2_FLAG_SUPER_ONLY;
798
799	if (c_flag) {
800		sb->s_max_mnt_count = max_mount_count;
801		ext2fs_mark_super_dirty(fs);
802		printf (_("Setting maximal mount count to %d\n"),
803			max_mount_count);
804	}
805	if (C_flag) {
806		sb->s_mnt_count = mount_count;
807		ext2fs_mark_super_dirty(fs);
808		printf (_("Setting current mount count to %d\n"), mount_count);
809	}
810	if (e_flag) {
811		sb->s_errors = errors;
812		ext2fs_mark_super_dirty(fs);
813		printf (_("Setting error behavior to %d\n"), errors);
814	}
815	if (g_flag) {
816		sb->s_def_resgid = resgid;
817		ext2fs_mark_super_dirty(fs);
818		printf (_("Setting reserved blocks gid to %lu\n"), resgid);
819	}
820	if (i_flag) {
821		sb->s_checkinterval = interval;
822		ext2fs_mark_super_dirty(fs);
823		printf (_("Setting interval between checks to %lu seconds\n"), interval);
824	}
825	if (m_flag) {
826		sb->s_r_blocks_count = e2p_percent(reserved_ratio,
827						   sb->s_blocks_count);
828		ext2fs_mark_super_dirty(fs);
829		printf (_("Setting reserved blocks percentage to %g%% (%u blocks)\n"),
830			reserved_ratio, sb->s_r_blocks_count);
831	}
832	if (r_flag) {
833		if (reserved_blocks >= sb->s_blocks_count/2) {
834			com_err (program_name, 0,
835				 _("reserved blocks count is too big (%lu)"),
836				 reserved_blocks);
837			exit (1);
838		}
839		sb->s_r_blocks_count = reserved_blocks;
840		ext2fs_mark_super_dirty(fs);
841		printf (_("Setting reserved blocks count to %lu\n"),
842			reserved_blocks);
843	}
844	if (s_flag == 1) {
845		if (sb->s_feature_ro_compat &
846		    EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)
847			fputs(_("\nThe filesystem already has sparse "
848				"superblocks.\n"), stderr);
849		else {
850			sb->s_feature_ro_compat |=
851				EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
852			sb->s_state &= ~EXT2_VALID_FS;
853			ext2fs_mark_super_dirty(fs);
854			printf(_("\nSparse superblock flag set.  %s"),
855			       _(please_fsck));
856		}
857	}
858	if (s_flag == 0) {
859		if (!(sb->s_feature_ro_compat &
860		      EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER))
861			fputs(_("\nThe filesystem already has sparse "
862				"superblocks disabled.\n"), stderr);
863		else {
864			sb->s_feature_ro_compat &=
865				~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
866			sb->s_state &= ~EXT2_VALID_FS;
867			fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
868			ext2fs_mark_super_dirty(fs);
869			printf(_("\nSparse superblock flag cleared.  %s"),
870			       _(please_fsck));
871		}
872	}
873	if (T_flag) {
874		sb->s_lastcheck = last_check_time;
875		ext2fs_mark_super_dirty(fs);
876		printf(_("Setting time filesystem last checked to %s\n"),
877		       ctime(&last_check_time));
878	}
879	if (u_flag) {
880		sb->s_def_resuid = resuid;
881		ext2fs_mark_super_dirty(fs);
882		printf (_("Setting reserved blocks uid to %lu\n"), resuid);
883	}
884	if (L_flag) {
885		if (strlen(new_label) > sizeof(sb->s_volume_name))
886			fputs(_("Warning: label too long, truncating.\n"),
887			      stderr);
888		memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name));
889		strncpy(sb->s_volume_name, new_label,
890			sizeof(sb->s_volume_name));
891		ext2fs_mark_super_dirty(fs);
892	}
893	if (M_flag) {
894		memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted));
895		strncpy(sb->s_last_mounted, new_last_mounted,
896			sizeof(sb->s_last_mounted));
897		ext2fs_mark_super_dirty(fs);
898	}
899	if (mntopts_cmd)
900		update_mntopts(fs, mntopts_cmd);
901	if (features_cmd)
902		update_feature_set(fs, features_cmd);
903	if (journal_size || journal_device)
904		add_journal(fs);
905
906	if (U_flag) {
907		if ((strcasecmp(new_UUID, "null") == 0) ||
908		    (strcasecmp(new_UUID, "clear") == 0)) {
909			uuid_clear(sb->s_uuid);
910		} else if (strcasecmp(new_UUID, "time") == 0) {
911			uuid_generate_time(sb->s_uuid);
912		} else if (strcasecmp(new_UUID, "random") == 0) {
913			uuid_generate(sb->s_uuid);
914		} else if (uuid_parse(new_UUID, sb->s_uuid)) {
915			com_err(program_name, 0, _("Invalid UUID format\n"));
916			exit(1);
917		}
918		ext2fs_mark_super_dirty(fs);
919	}
920
921	if (l_flag)
922		list_super (sb);
923	return (ext2fs_close (fs) ? 1 : 0);
924}
925