blob: d4d77b63b92a2dd482b9a4aa1d8fecc908462612 [file] [log] [blame]
Mike Frysinger38a33f92005-05-09 22:13:22 +00001/*
2 * util.c --- helper functions used by tune2fs and mke2fs
3 *
4 * Copyright 1995, 1996, 1997, 1998, 1999, 2000 by Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
10 */
11
12#include <stdio.h>
13#include <string.h>
14#include <errno.h>
15#include <linux/major.h>
16#include <sys/stat.h>
17
18#include "e2fsbb.h"
19#include "e2p/e2p.h"
20#include "ext2fs/ext2_fs.h"
21#include "ext2fs/ext2fs.h"
22#include "blkid/blkid.h"
23#include "util.h"
24
25void proceed_question(void)
26{
27 fputs("Proceed anyway? (y,n) ", stdout);
28 if (bb_ask_confirmation() == 0)
29 exit(1);
30}
31
32void check_plausibility(const char *device)
33{
34 int val;
35#ifdef CONFIG_LFS
36 struct stat64 s;
37 val = stat64(device, &s);
38#else
39 struct stat s;
40 val = stat(device, &s);
41#endif
42
43 if(val == -1)
44 bb_perror_msg_and_die("Could not stat %s", device);
45 if (!S_ISBLK(s.st_mode)) {
46 printf("%s is not a block special device.\n", device);
47 proceed_question();
48 return;
49 }
50
51#ifdef HAVE_LINUX_MAJOR_H
52#ifndef MAJOR
53#define MAJOR(dev) ((dev)>>8)
54#define MINOR(dev) ((dev) & 0xff)
55#endif
56#ifndef SCSI_BLK_MAJOR
57#ifdef SCSI_DISK0_MAJOR
58#ifdef SCSI_DISK8_MAJOR
59#define SCSI_DISK_MAJOR(M) ((M) == SCSI_DISK0_MAJOR || \
60 ((M) >= SCSI_DISK1_MAJOR && (M) <= SCSI_DISK7_MAJOR) || \
61 ((M) >= SCSI_DISK8_MAJOR && (M) <= SCSI_DISK15_MAJOR))
62#else
63#define SCSI_DISK_MAJOR(M) ((M) == SCSI_DISK0_MAJOR || \
64 ((M) >= SCSI_DISK1_MAJOR && (M) <= SCSI_DISK7_MAJOR))
65#endif /* defined(SCSI_DISK8_MAJOR) */
66#define SCSI_BLK_MAJOR(M) (SCSI_DISK_MAJOR((M)) || (M) == SCSI_CDROM_MAJOR)
67#else
68#define SCSI_BLK_MAJOR(M) ((M) == SCSI_DISK_MAJOR || (M) == SCSI_CDROM_MAJOR)
69#endif /* defined(SCSI_DISK0_MAJOR) */
70#endif /* defined(SCSI_BLK_MAJOR) */
71 if (((MAJOR(s.st_rdev) == HD_MAJOR &&
72 MINOR(s.st_rdev)%64 == 0) ||
73 (SCSI_BLK_MAJOR(MAJOR(s.st_rdev)) &&
74 MINOR(s.st_rdev)%16 == 0))) {
75 printf("%s is entire device, not just one partition!\n", device);
76 proceed_question();
77 }
78#endif
79}
80
81void check_mount(const char *device, int force, const char *type)
82{
83 errcode_t retval;
84 int mount_flags;
85
86 retval = ext2fs_check_if_mounted(device, &mount_flags);
87 if (retval) {
88 bb_error_msg("Could not determine if %s is mounted", device);
89 return;
90 }
91 if (!(mount_flags & EXT2_MF_MOUNTED))
92 return;
93
94 bb_error_msg("%s is mounted !", device);
95 if (force)
96 bb_error_msg("forcing anyways and ignoring /etc/mtab status");
97 else
98 bb_error_msg_and_die("will not make a %s here!", type);
99}
100
101void parse_journal_opts(char **journal_device, int *journal_flags,
102 int *journal_size, const char *opts)
103{
104 char *buf, *token, *next, *p, *arg;
105 int journal_usage = 0;
106#if 0
107 int len;
108 len = strlen(opts);
109 buf = xmalloc(len+1);
110 strcpy(buf, opts);
111#else
112 buf = bb_xstrdup(opts);
113#endif
114 for (token = buf; token && *token; token = next) {
115 p = strchr(token, ',');
116 next = 0;
117 if (p) {
118 *p = 0;
119 next = p+1;
120 }
121 arg = strchr(token, '=');
122 if (arg) {
123 *arg = 0;
124 arg++;
125 }
126 if (strcmp(token, "device") == 0) {
127 *journal_device = blkid_get_devname(NULL, arg, NULL);
128 if (!journal_device) {
129 journal_usage++;
130 continue;
131 }
132 } else if (strcmp(token, "size") == 0) {
133 if (!arg) {
134 journal_usage++;
135 continue;
136 }
137 (*journal_size) = strtoul(arg, &p, 0);
138 if (*p)
139 journal_usage++;
140 } else if (strcmp(token, "v1_superblock") == 0) {
141 (*journal_flags) |= EXT2_MKJOURNAL_V1_SUPER;
142 continue;
143 } else
144 journal_usage++;
145 }
146 if (journal_usage)
147 bb_error_msg_and_die(
148 "\nBad journal options specified.\n\n"
149 "Journal options are separated by commas, "
150 "and may take an argument which\n"
151 "\tis set off by an equals ('=') sign.\n\n"
152 "Valid journal options are:\n"
153 "\tsize=<journal size in megabytes>\n"
154 "\tdevice=<journal device>\n\n"
155 "The journal size must be between "
156 "1024 and 102400 filesystem blocks.\n\n");
157}
158
159/*
160 * Determine the number of journal blocks to use, either via
161 * user-specified # of megabytes, or via some intelligently selected
162 * defaults.
163 *
164 * Find a reasonable journal file size (in blocks) given the number of blocks
165 * in the filesystem. For very small filesystems, it is not reasonable to
166 * have a journal that fills more than half of the filesystem.
167 */
168int figure_journal_size(int size, ext2_filsys fs)
169{
170 blk_t j_blocks;
171
172 if (fs->super->s_blocks_count < 2048) {
173 bb_error_msg("Filesystem too small for a journal");
174 return 0;
175 }
176
177 if (size >= 0) {
178 j_blocks = size * 1024 / (fs->blocksize / 1024);
179 if (j_blocks < 1024 || j_blocks > 102400)
180 bb_error_msg_and_die("\nThe requested journal "
181 "size is %d blocks;\n it must be "
182 "between 1024 and 102400 blocks; Aborting",
183 j_blocks);
184 if (j_blocks > fs->super->s_free_blocks_count)
185 bb_error_msg_and_die("Journal size too big for filesystem");
186 return j_blocks;
187 }
188
189 if (fs->super->s_blocks_count < 32768)
190 j_blocks = 1024;
191 else if (fs->super->s_blocks_count < 262144)
192 j_blocks = 4096;
193 else
194 j_blocks = 8192;
195
196 return j_blocks;
197}
198
199void print_check_message(ext2_filsys fs)
200{
201 printf("This filesystem will be automatically "
202 "checked every %d mounts or\n"
203 "%g days, whichever comes first. "
204 "Use tune2fs -c or -i to override.\n",
205 fs->super->s_max_mnt_count,
206 (double)fs->super->s_checkinterval / (3600 * 24));
207}