blob: 3e7ee8e757f264450379f1f7df0a31bb87f5bff1 [file] [log] [blame]
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001/* vi: set sw=4 ts=4: */
2/*
3 * util.c --- helper functions used by tune2fs and mke2fs
4 *
5 * Copyright 1995, 1996, 1997, 1998, 1999, 2000 by Theodore Ts'o.
6 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00008 */
9
10#include <stdio.h>
11#include <string.h>
12#include <errno.h>
13#include <linux/major.h>
14#include <sys/stat.h>
15
16#include "e2fsbb.h"
17#include "e2p/e2p.h"
18#include "ext2fs/ext2_fs.h"
19#include "ext2fs/ext2fs.h"
20#include "blkid/blkid.h"
21#include "util.h"
22
23void proceed_question(void)
24{
25 fputs("Proceed anyway? (y,n) ", stdout);
26 if (bb_ask_confirmation() == 0)
27 exit(1);
28}
29
30void check_plausibility(const char *device, int force)
31{
32 int val;
33 struct stat s;
34 val = stat(device, &s);
35 if (force)
36 return;
Denis Vlasenkod6855d12008-09-27 14:03:25 +000037 if (val == -1)
Denys Vlasenko651a2692010-03-23 16:25:17 +010038 bb_perror_msg_and_die("can't stat '%s'", device);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000039 if (!S_ISBLK(s.st_mode)) {
40 printf("%s is not a block special device.\n", device);
41 proceed_question();
42 return;
43 }
44
45#ifdef HAVE_LINUX_MAJOR_H
46#ifndef MAJOR
47#define MAJOR(dev) ((dev)>>8)
48#define MINOR(dev) ((dev) & 0xff)
49#endif
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))
56#else
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)
61#else
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);
70 proceed_question();
71 }
72#endif
73}
74
75void check_mount(const char *device, int force, const char *type)
76{
77 errcode_t retval;
78 int mount_flags;
79
80 retval = ext2fs_check_if_mounted(device, &mount_flags);
81 if (retval) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +010082 bb_error_msg("can't determine if %s is mounted", device);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000083 return;
84 }
85 if (mount_flags & EXT2_MF_MOUNTED) {
86 bb_error_msg("%s is mounted !", device);
87force_check:
88 if (force)
89 bb_error_msg("badblocks forced anyways");
90 else
91 bb_error_msg_and_die("it's not safe to run badblocks!");
92 }
93
94 if (mount_flags & EXT2_MF_BUSY) {
95 bb_error_msg("%s is apparently in use by the system", device);
96 goto force_check;
97 }
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000098}
99
100void parse_journal_opts(char **journal_device, int *journal_flags,
101 int *journal_size, const char *opts)
102{
103 char *buf, *token, *next, *p, *arg;
104 int journal_usage = 0;
105 buf = xstrdup(opts);
106 for (token = buf; token && *token; token = next) {
107 p = strchr(token, ',');
108 next = 0;
109 if (p) {
110 *p = 0;
111 next = p+1;
112 }
113 arg = strchr(token, '=');
114 if (arg) {
115 *arg = 0;
116 arg++;
117 }
118 if (strcmp(token, "device") == 0) {
119 *journal_device = blkid_get_devname(NULL, arg, NULL);
Denys Vlasenko9b2a9f02013-11-29 16:43:33 +0100120 if (!*journal_device) {
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000121 journal_usage++;
122 continue;
123 }
124 } else if (strcmp(token, "size") == 0) {
125 if (!arg) {
126 journal_usage++;
127 continue;
128 }
129 (*journal_size) = strtoul(arg, &p, 0);
130 if (*p)
131 journal_usage++;
132 } else if (strcmp(token, "v1_superblock") == 0) {
133 (*journal_flags) |= EXT2_MKJOURNAL_V1_SUPER;
134 continue;
135 } else
136 journal_usage++;
137 }
138 if (journal_usage)
139 bb_error_msg_and_die(
140 "\nBad journal options specified.\n\n"
141 "Journal options are separated by commas, "
142 "and may take an argument which\n"
143 "\tis set off by an equals ('=') sign.\n\n"
144 "Valid journal options are:\n"
145 "\tsize=<journal size in megabytes>\n"
146 "\tdevice=<journal device>\n\n"
147 "The journal size must be between "
148 "1024 and 102400 filesystem blocks.\n\n");
149}
150
151/*
152 * Determine the number of journal blocks to use, either via
153 * user-specified # of megabytes, or via some intelligently selected
154 * defaults.
155 *
156 * Find a reasonable journal file size (in blocks) given the number of blocks
157 * in the filesystem. For very small filesystems, it is not reasonable to
158 * have a journal that fills more than half of the filesystem.
159 */
160int figure_journal_size(int size, ext2_filsys fs)
161{
162 blk_t j_blocks;
163
164 if (fs->super->s_blocks_count < 2048) {
165 bb_error_msg("Filesystem too small for a journal");
166 return 0;
167 }
168
169 if (size >= 0) {
170 j_blocks = size * 1024 / (fs->blocksize / 1024);
171 if (j_blocks < 1024 || j_blocks > 102400)
172 bb_error_msg_and_die("\nThe requested journal "
173 "size is %d blocks;\n it must be "
174 "between 1024 and 102400 blocks; Aborting",
175 j_blocks);
176 if (j_blocks > fs->super->s_free_blocks_count)
177 bb_error_msg_and_die("Journal size too big for filesystem");
178 return j_blocks;
179 }
180
181 if (fs->super->s_blocks_count < 32768)
182 j_blocks = 1024;
183 else if (fs->super->s_blocks_count < 256*1024)
184 j_blocks = 4096;
185 else if (fs->super->s_blocks_count < 512*1024)
186 j_blocks = 8192;
187 else if (fs->super->s_blocks_count < 1024*1024)
188 j_blocks = 16384;
189 else
190 j_blocks = 32768;
191
192 return j_blocks;
193}
194
195void print_check_message(ext2_filsys fs)
196{
197 printf("This filesystem will be automatically "
198 "checked every %d mounts or\n"
199 "%g days, whichever comes first. "
200 "Use tune2fs -c or -i to override.\n",
201 fs->super->s_max_mnt_count,
202 (double)fs->super->s_checkinterval / (3600 * 24));
203}
204
205void make_journal_device(char *journal_device, ext2_filsys fs, int quiet, int force)
206{
207 errcode_t retval;
208 ext2_filsys jfs;
209 io_manager io_ptr;
210
211 check_plausibility(journal_device, force);
212 check_mount(journal_device, force, "journal");
213 io_ptr = unix_io_manager;
214 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
215 EXT2_FLAG_JOURNAL_DEV_OK, 0,
216 fs->blocksize, io_ptr, &jfs);
217 if (retval)
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100218 bb_error_msg_and_die("can't journal device %s", journal_device);
Denis Vlasenkod6855d12008-09-27 14:03:25 +0000219 if (!quiet)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000220 printf("Adding journal to device %s: ", journal_device);
221 fflush(stdout);
222 retval = ext2fs_add_journal_device(fs, jfs);
Denis Vlasenkod6855d12008-09-27 14:03:25 +0000223 if (retval)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000224 bb_error_msg_and_die("\nFailed to add journal to device %s", journal_device);
Denis Vlasenkod6855d12008-09-27 14:03:25 +0000225 if (!quiet)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000226 puts("done");
227 ext2fs_close(jfs);
228}
229
230void make_journal_blocks(ext2_filsys fs, int journal_size, int journal_flags, int quiet)
231{
232 unsigned long journal_blocks;
233 errcode_t retval;
234
235 journal_blocks = figure_journal_size(journal_size, fs);
236 if (!journal_blocks) {
237 fs->super->s_feature_compat &=
238 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
239 return;
240 }
Denis Vlasenkod6855d12008-09-27 14:03:25 +0000241 if (!quiet)
Denys Vlasenko327f5502013-11-29 16:45:45 +0100242 printf("Creating journal (%lu blocks): ", journal_blocks);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000243 fflush(stdout);
244 retval = ext2fs_add_journal_inode(fs, journal_blocks,
245 journal_flags);
Denis Vlasenkod6855d12008-09-27 14:03:25 +0000246 if (retval)
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100247 bb_error_msg_and_die("can't create journal");
Denis Vlasenkod6855d12008-09-27 14:03:25 +0000248 if (!quiet)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000249 puts("done");
250}
251
252char *e2fs_set_sbin_path(void)
253{
254 char *oldpath = getenv("PATH");
255 /* Update our PATH to include /sbin */
256#define PATH_SET "/sbin"
257 if (oldpath)
258 oldpath = xasprintf("%s:%s", PATH_SET, oldpath);
259 else
260 oldpath = PATH_SET;
Denis Vlasenkod6855d12008-09-27 14:03:25 +0000261 putenv(oldpath);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000262 return oldpath;
263}