1 /* vi: set sw=4 ts=4: */
3 * util.c --- helper functions used by tune2fs and mke2fs
5 * Copyright 1995, 1996, 1997, 1998, 1999, 2000 by Theodore Ts'o.
7 * Licensed under GPLv2, see file LICENSE in this source tree.
13 #include <linux/major.h>
18 #include "ext2fs/ext2_fs.h"
19 #include "ext2fs/ext2fs.h"
20 #include "volume_id.h"
23 void proceed_question(void)
25 fputs("Proceed anyway? (y,n) ", stdout
);
26 if (bb_ask_confirmation() == 0)
30 void check_plausibility(const char *device
, int force
)
34 val
= stat(device
, &s
);
38 bb_perror_msg_and_die("can't stat '%s'", device
);
39 if (!S_ISBLK(s
.st_mode
)) {
40 printf("%s is not a block special device.\n", device
);
45 #ifdef HAVE_LINUX_MAJOR_H
47 #define MAJOR(dev) ((dev)>>8)
48 #define MINOR(dev) ((dev) & 0xff)
50 #ifndef SCSI_BLK_MAJOR
51 #ifdef SCSI_DISK0_MAJOR
52 #ifdef SCSI_DISK8_MAJOR
53 #define SCSI_DISK_MAJOR(M) ((M) == SCSI_DISK0_MAJOR || \
54 ((M) >= SCSI_DISK1_MAJOR && (M) <= SCSI_DISK7_MAJOR) || \
55 ((M) >= SCSI_DISK8_MAJOR && (M) <= SCSI_DISK15_MAJOR))
57 #define SCSI_DISK_MAJOR(M) ((M) == SCSI_DISK0_MAJOR || \
58 ((M) >= SCSI_DISK1_MAJOR && (M) <= SCSI_DISK7_MAJOR))
59 #endif /* defined(SCSI_DISK8_MAJOR) */
60 #define SCSI_BLK_MAJOR(M) (SCSI_DISK_MAJOR((M)) || (M) == SCSI_CDROM_MAJOR)
62 #define SCSI_BLK_MAJOR(M) ((M) == SCSI_DISK_MAJOR || (M) == SCSI_CDROM_MAJOR)
63 #endif /* defined(SCSI_DISK0_MAJOR) */
64 #endif /* defined(SCSI_BLK_MAJOR) */
65 if (((MAJOR(s
.st_rdev
) == HD_MAJOR
&&
66 MINOR(s
.st_rdev
)%64 == 0) ||
67 (SCSI_BLK_MAJOR(MAJOR(s
.st_rdev
)) &&
68 MINOR(s
.st_rdev
)%16 == 0))) {
69 printf("%s is entire device, not just one partition!\n", device
);
75 void check_mount(const char *device
, int force
, const char *type UNUSED_PARAM
)
80 retval
= ext2fs_check_if_mounted(device
, &mount_flags
);
82 bb_error_msg("can't determine if %s is mounted", device
);
85 if (mount_flags
& EXT2_MF_MOUNTED
) {
86 bb_error_msg("%s is mounted !", device
);
89 bb_error_msg("badblocks forced anyways");
91 bb_error_msg_and_die("it's not safe to run badblocks!");
94 if (mount_flags
& EXT2_MF_BUSY
) {
95 bb_error_msg("%s is apparently in use by the system", device
);
100 void parse_journal_opts(char **journal_device
, int *journal_flags
,
101 int *journal_size
, const char *opts
)
103 char *buf
, *token
, *next
, *p
, *arg
;
104 int journal_usage
= 0;
106 for (token
= buf
; token
&& *token
; token
= next
) {
107 p
= strchr(token
, ',');
113 arg
= strchr(token
, '=');
118 if (strcmp(token
, "device") == 0) {
119 *journal_device
= arg
;
120 if (resolve_mount_spec(journal_device
) < 0 ||
121 !(*journal_device
)) {
125 } else if (strcmp(token
, "size") == 0) {
130 (*journal_size
) = strtoul(arg
, &p
, 0);
133 } else if (strcmp(token
, "v1_superblock") == 0) {
134 (*journal_flags
) |= EXT2_MKJOURNAL_V1_SUPER
;
140 bb_error_msg_and_die(
141 "\nBad journal options specified.\n\n"
142 "Journal options are separated by commas, "
143 "and may take an argument which\n"
144 "\tis set off by an equals ('=') sign.\n\n"
145 "Valid journal options are:\n"
146 "\tsize=<journal size in megabytes>\n"
147 "\tdevice=<journal device>\n\n"
148 "The journal size must be between "
149 "1024 and 102400 filesystem blocks.\n\n");
153 * Determine the number of journal blocks to use, either via
154 * user-specified # of megabytes, or via some intelligently selected
157 * Find a reasonable journal file size (in blocks) given the number of blocks
158 * in the filesystem. For very small filesystems, it is not reasonable to
159 * have a journal that fills more than half of the filesystem.
161 int figure_journal_size(int size
, ext2_filsys fs
)
165 if (fs
->super
->s_blocks_count
< 2048) {
166 bb_error_msg("Filesystem too small for a journal");
171 j_blocks
= size
* 1024 / (fs
->blocksize
/ 1024);
172 if (j_blocks
< 1024 || j_blocks
> 102400)
173 bb_error_msg_and_die("\nThe requested journal "
174 "size is %d blocks;\n it must be "
175 "between 1024 and 102400 blocks; Aborting",
177 if (j_blocks
> fs
->super
->s_free_blocks_count
)
178 bb_error_msg_and_die("Journal size too big for filesystem");
182 if (fs
->super
->s_blocks_count
< 32768)
184 else if (fs
->super
->s_blocks_count
< 256*1024)
186 else if (fs
->super
->s_blocks_count
< 512*1024)
188 else if (fs
->super
->s_blocks_count
< 1024*1024)
196 void print_check_message(ext2_filsys fs
)
198 printf("This filesystem will be automatically "
199 "checked every %d mounts or\n"
200 "%g days, whichever comes first. "
201 "Use tune2fs -c or -i to override.\n",
202 fs
->super
->s_max_mnt_count
,
203 (double)fs
->super
->s_checkinterval
/ (3600 * 24));
206 void make_journal_device(char *journal_device
, ext2_filsys fs
, int quiet
, int force
)
212 check_plausibility(journal_device
, force
);
213 check_mount(journal_device
, force
, "journal");
214 io_ptr
= unix_io_manager
;
215 retval
= ext2fs_open(journal_device
, EXT2_FLAG_RW
|
216 EXT2_FLAG_JOURNAL_DEV_OK
, 0,
217 fs
->blocksize
, io_ptr
, &jfs
);
219 bb_error_msg_and_die("can't journal device %s", journal_device
);
221 printf("Adding journal to device %s: ", journal_device
);
223 retval
= ext2fs_add_journal_device(fs
, jfs
);
225 bb_error_msg_and_die("\nFailed to add journal to device %s", journal_device
);
231 void make_journal_blocks(ext2_filsys fs
, int journal_size
, int journal_flags
, int quiet
)
233 unsigned long journal_blocks
;
236 journal_blocks
= figure_journal_size(journal_size
, fs
);
237 if (!journal_blocks
) {
238 fs
->super
->s_feature_compat
&=
239 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL
;
243 printf("Creating journal (%ld blocks): ", journal_blocks
);
245 retval
= ext2fs_add_journal_inode(fs
, journal_blocks
,
248 bb_error_msg_and_die("can't create journal");
253 char *e2fs_set_sbin_path(void)
255 char *oldpath
= getenv("PATH");
256 /* Update our PATH to include /sbin */
257 #define PATH_SET "/sbin"
259 oldpath
= xasprintf("%s:%s", PATH_SET
, oldpath
);
261 oldpath
= (char *)PATH_SET
;