2 * util.c --- helper functions used by tune2fs and mke2fs
4 * Copyright 1995, 1996, 1997, 1998, 1999, 2000 by Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
15 #include <linux/major.h>
20 #include "ext2fs/ext2_fs.h"
21 #include "ext2fs/ext2fs.h"
22 #include "blkid/blkid.h"
25 void proceed_question(void)
27 fputs("Proceed anyway? (y,n) ", stdout);
28 if (bb_ask_confirmation() == 0)
32 void check_plausibility(const char *device, int force)
37 val = stat64(device, &s);
40 val = stat(device, &s);
45 bb_perror_msg_and_die("Could not stat %s", device);
46 if (!S_ISBLK(s.st_mode)) {
47 printf("%s is not a block special device.\n", device);
52 #ifdef HAVE_LINUX_MAJOR_H
54 #define MAJOR(dev) ((dev)>>8)
55 #define MINOR(dev) ((dev) & 0xff)
57 #ifndef SCSI_BLK_MAJOR
58 #ifdef SCSI_DISK0_MAJOR
59 #ifdef SCSI_DISK8_MAJOR
60 #define SCSI_DISK_MAJOR(M) ((M) == SCSI_DISK0_MAJOR || \
61 ((M) >= SCSI_DISK1_MAJOR && (M) <= SCSI_DISK7_MAJOR) || \
62 ((M) >= SCSI_DISK8_MAJOR && (M) <= SCSI_DISK15_MAJOR))
64 #define SCSI_DISK_MAJOR(M) ((M) == SCSI_DISK0_MAJOR || \
65 ((M) >= SCSI_DISK1_MAJOR && (M) <= SCSI_DISK7_MAJOR))
66 #endif /* defined(SCSI_DISK8_MAJOR) */
67 #define SCSI_BLK_MAJOR(M) (SCSI_DISK_MAJOR((M)) || (M) == SCSI_CDROM_MAJOR)
69 #define SCSI_BLK_MAJOR(M) ((M) == SCSI_DISK_MAJOR || (M) == SCSI_CDROM_MAJOR)
70 #endif /* defined(SCSI_DISK0_MAJOR) */
71 #endif /* defined(SCSI_BLK_MAJOR) */
72 if (((MAJOR(s.st_rdev) == HD_MAJOR &&
73 MINOR(s.st_rdev)%64 == 0) ||
74 (SCSI_BLK_MAJOR(MAJOR(s.st_rdev)) &&
75 MINOR(s.st_rdev)%16 == 0))) {
76 printf("%s is entire device, not just one partition!\n", device);
82 void check_mount(const char *device, int force, const char *type)
87 retval = ext2fs_check_if_mounted(device, &mount_flags);
89 bb_error_msg("Could not determine if %s is mounted", device);
92 if (!(mount_flags & EXT2_MF_MOUNTED))
95 bb_error_msg("%s is mounted !", device);
97 bb_error_msg("forcing anyways and ignoring /etc/mtab status");
99 bb_error_msg_and_die("will not make a %s here!", type);
102 void parse_journal_opts(char **journal_device, int *journal_flags,
103 int *journal_size, const char *opts)
105 char *buf, *token, *next, *p, *arg;
106 int journal_usage = 0;
110 buf = xmalloc(len+1);
113 buf = bb_xstrdup(opts);
115 for (token = buf; token && *token; token = next) {
116 p = strchr(token, ',');
122 arg = strchr(token, '=');
127 if (strcmp(token, "device") == 0) {
128 *journal_device = blkid_get_devname(NULL, arg, NULL);
129 if (!journal_device) {
133 } else if (strcmp(token, "size") == 0) {
138 (*journal_size) = strtoul(arg, &p, 0);
141 } else if (strcmp(token, "v1_superblock") == 0) {
142 (*journal_flags) |= EXT2_MKJOURNAL_V1_SUPER;
148 bb_error_msg_and_die(
149 "\nBad journal options specified.\n\n"
150 "Journal options are separated by commas, "
151 "and may take an argument which\n"
152 "\tis set off by an equals ('=') sign.\n\n"
153 "Valid journal options are:\n"
154 "\tsize=<journal size in megabytes>\n"
155 "\tdevice=<journal device>\n\n"
156 "The journal size must be between "
157 "1024 and 102400 filesystem blocks.\n\n");
161 * Determine the number of journal blocks to use, either via
162 * user-specified # of megabytes, or via some intelligently selected
165 * Find a reasonable journal file size (in blocks) given the number of blocks
166 * in the filesystem. For very small filesystems, it is not reasonable to
167 * have a journal that fills more than half of the filesystem.
169 int figure_journal_size(int size, ext2_filsys fs)
173 if (fs->super->s_blocks_count < 2048) {
174 bb_error_msg("Filesystem too small for a journal");
179 j_blocks = size * 1024 / (fs->blocksize / 1024);
180 if (j_blocks < 1024 || j_blocks > 102400)
181 bb_error_msg_and_die("\nThe requested journal "
182 "size is %d blocks;\n it must be "
183 "between 1024 and 102400 blocks; Aborting",
185 if (j_blocks > fs->super->s_free_blocks_count)
186 bb_error_msg_and_die("Journal size too big for filesystem");
190 if (fs->super->s_blocks_count < 32768)
192 else if (fs->super->s_blocks_count < 262144)
200 void print_check_message(ext2_filsys fs)
202 printf("This filesystem will be automatically "
203 "checked every %d mounts or\n"
204 "%g days, whichever comes first. "
205 "Use tune2fs -c or -i to override.\n",
206 fs->super->s_max_mnt_count,
207 (double)fs->super->s_checkinterval / (3600 * 24));
210 void make_journal_device(char *journal_device, ext2_filsys fs, int quiet, int force)
216 check_plausibility(journal_device, force);
217 check_mount(journal_device, force, "journal");
218 io_ptr = unix_io_manager;
219 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
220 EXT2_FLAG_JOURNAL_DEV_OK, 0,
221 fs->blocksize, io_ptr, &jfs);
223 bb_error_msg_and_die("Could not journal device %s", journal_device);
225 printf("Adding journal to device %s: ", journal_device);
227 retval = ext2fs_add_journal_device(fs, jfs);
229 bb_error_msg_and_die("\nFailed to add journal to device %s", journal_device);
235 void make_journal_blocks(ext2_filsys fs, int journal_size, int journal_flags, int quiet)
237 unsigned long journal_blocks;
240 journal_blocks = figure_journal_size(journal_size, fs);
241 if (!journal_blocks) {
242 fs->super->s_feature_compat &=
243 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
247 printf("Creating journal (%ld blocks): ", journal_blocks);
249 retval = ext2fs_add_journal_inode(fs, journal_blocks,
252 bb_error_msg_and_die("Could not create journal");
257 char *e2fs_set_sbin_path(void)
259 char *oldpath = getenv("PATH");
260 /* Update our PATH to include /sbin */
261 #define PATH_SET "/sbin"
263 oldpath = bb_xasprintf("%s:%s", PATH_SET, oldpath);