1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#include "libbb.h"
28#include <linux/fs.h>
29
30struct journal_params {
31 uint32_t jp_journal_1st_block;
32 uint32_t jp_journal_dev;
33 uint32_t jp_journal_size;
34 uint32_t jp_journal_trans_max;
35 uint32_t jp_journal_magic;
36 uint32_t jp_journal_max_batch;
37 uint32_t jp_journal_max_commit_age;
38 uint32_t jp_journal_max_trans_age;
39};
40
41struct reiserfs_journal_header {
42 uint32_t jh_last_flush_trans_id;
43 uint32_t jh_first_unflushed_offset;
44 uint32_t jh_mount_id;
45 struct journal_params jh_journal;
46 uint32_t jh_last_check_mount_id;
47};
48
49struct reiserfs_super_block {
50 uint32_t sb_block_count;
51 uint32_t sb_free_blocks;
52 uint32_t sb_root_block;
53
54 struct journal_params sb_journal;
55
56 uint16_t sb_blocksize;
57 uint16_t sb_oid_maxsize;
58 uint16_t sb_oid_cursize;
59 uint16_t sb_umount_state;
60
61 char s_magic[10];
62 uint16_t sb_fs_state;
63 uint32_t sb_hash_function_code;
64 uint16_t sb_tree_height;
65 uint16_t sb_bmap_nr;
66 uint16_t sb_version;
67 uint16_t sb_reserved_for_journal;
68 uint32_t sb_inode_generation;
69 uint32_t sb_flags;
70 unsigned char s_uuid[16];
71 unsigned char s_label[16];
72 uint16_t sb_mnt_count;
73 uint16_t sb_max_mnt_count;
74 uint32_t sb_lastcheck;
75 uint32_t sb_check_interval;
76
77 char s_unused[76];
78
79};
80
81
82
83struct block_head {
84 uint16_t blk2_level;
85 uint16_t blk2_nr_item;
86 uint16_t blk2_free_space;
87 uint16_t blk_reserved;
88 uint32_t reserved[4];
89};
90
91#define REISERFS_DISK_OFFSET_IN_BYTES (64 * 1024)
92
93#define REISERFS_3_6_SUPER_MAGIC_STRING "ReIsEr2Fs"
94#define REISERFS_FORMAT_3_6 2
95#define DEFAULT_MAX_MNT_COUNT 30
96#define DEFAULT_CHECK_INTERVAL (180 * 60 * 60 * 24)
97
98#define FS_CLEANLY_UMOUNTED 1
99
100#define JOURNAL_MIN_SIZE 512
101
102#define JOURNAL_TRANS_MAX 1024
103#define JOURNAL_TRANS_MIN 256
104#define JOURNAL_DEFAULT_RATIO 8
105#define JOURNAL_MIN_RATIO 2
106
107#define JOURNAL_MAX_BATCH 900
108#define JOURNAL_MAX_COMMIT_AGE 30
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133enum {
134 OPT_b = 1 << 0,
135 OPT_j = 1 << 1,
136 OPT_s = 1 << 2,
137 OPT_o = 1 << 3,
138 OPT_t = 1 << 4,
139 OPT_B = 1 << 5,
140 OPT_h = 1 << 6,
141 OPT_u = 1 << 7,
142 OPT_l = 1 << 8,
143 OPT_f = 1 << 9,
144 OPT_q = 1 << 10,
145 OPT_d = 1 << 11,
146
147};
148
149int mkfs_reiser_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
150int mkfs_reiser_main(int argc UNUSED_PARAM, char **argv)
151{
152 unsigned blocksize = 4096;
153 unsigned journal_blocks = 8192;
154 unsigned blocks, bitmap_blocks, i, block;
155 time_t timestamp;
156 const char *label = "";
157 struct stat st;
158 int fd;
159 uint8_t *buf;
160 struct reiserfs_super_block *sb;
161 struct journal_params *jp;
162 struct block_head *root;
163
164
165
166 getopt32(argv, "^" "b:+j:s:o:t:B:h:u:l:fqd" "\0" "-1",
167 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &label);
168 argv += optind;
169
170
171 fd = xopen(argv[0], O_WRONLY | O_EXCL);
172 xfstat(fd, &st, argv[0]);
173 if (!S_ISBLK(st.st_mode) && !(option_mask32 & OPT_f))
174 bb_error_msg_and_die("%s: not a block device", argv[0]);
175
176
177
178
179 if (find_mount_point(argv[0], 0))
180 bb_simple_error_msg_and_die("can't format mounted filesystem");
181
182
183 blocks = get_volume_size_in_bytes(fd, argv[1], blocksize, 1) / blocksize;
184
185
186
187
188 block = REISERFS_DISK_OFFSET_IN_BYTES / blocksize
189 + 1
190 + 1
191 + journal_blocks+1
192 ;
193
194
195 bitmap_blocks = (blocks - 1) / (blocksize * 8) + 1;
196 i = block + bitmap_blocks;
197
198
199 if (MIN(blocksize * 8, blocks) < i)
200 bb_error_msg_and_die("need >= %u blocks", i);
201
202
203
204
205
206
207 xlseek(fd, REISERFS_DISK_OFFSET_IN_BYTES, SEEK_SET);
208
209
210 sb = (struct reiserfs_super_block *)xzalloc(blocksize);
211
212 STORE_LE(sb->sb_block_count, blocks);
213 STORE_LE(sb->sb_free_blocks, blocks - i);
214
215 STORE_LE(sb->sb_root_block, block);
216
217 jp = &sb->sb_journal;
218 STORE_LE(jp->jp_journal_1st_block, REISERFS_DISK_OFFSET_IN_BYTES / blocksize + 1 + 1);
219 timestamp = time(NULL);
220 srand(timestamp);
221 STORE_LE(jp->jp_journal_magic, rand());
222 STORE_LE(jp->jp_journal_size, journal_blocks);
223 STORE_LE(jp->jp_journal_trans_max, JOURNAL_TRANS_MAX);
224 STORE_LE(jp->jp_journal_max_batch, JOURNAL_MAX_BATCH);
225 STORE_LE(jp->jp_journal_max_commit_age, JOURNAL_MAX_COMMIT_AGE);
226
227 STORE_LE(sb->sb_blocksize, blocksize);
228 STORE_LE(sb->sb_oid_maxsize, (blocksize - sizeof(*sb)) / sizeof(uint32_t) / 2 * 2);
229 STORE_LE(sb->sb_oid_cursize, 2);
230 strcpy(sb->s_magic, REISERFS_3_6_SUPER_MAGIC_STRING);
231 STORE_LE(sb->sb_bmap_nr, (bitmap_blocks > ((1LL << 16) - 1)) ? 0 : bitmap_blocks);
232
233 STORE_LE(sb->sb_version, REISERFS_FORMAT_3_6);
234 STORE_LE(sb->sb_lastcheck, timestamp);
235 STORE_LE(sb->sb_check_interval, DEFAULT_CHECK_INTERVAL);
236 STORE_LE(sb->sb_mnt_count, 1);
237 STORE_LE(sb->sb_max_mnt_count, DEFAULT_MAX_MNT_COUNT);
238 STORE_LE(sb->sb_umount_state, FS_CLEANLY_UMOUNTED);
239 STORE_LE(sb->sb_tree_height, 2);
240 STORE_LE(sb->sb_hash_function_code, 3);
241 STORE_LE(sb->sb_flags, 1);
242
243
244 generate_uuid(sb->s_uuid);
245
246 safe_strncpy((char *)sb->s_label, label, sizeof(sb->s_label));
247
248
249
250 buf = (uint8_t *)sb;
251 buf[205] = 1;
252 buf[209] = 3;
253
254
255 xwrite(fd, sb, blocksize);
256
257
258 buf = xzalloc(blocksize);
259
260
261 i = block + 1;
262 memset(buf, 0xFF, i / 8);
263 buf[i / 8] = (1 << (i & 7)) - 1;
264
265 if (blocks < 8*blocksize) {
266 unsigned n = 8*blocksize - blocks;
267 i = n / 8;
268 buf[blocksize - i - 1] |= 0x7F00 >> (n & 7);
269 memset(buf + blocksize - i, 0xFF, i);
270 }
271
272 xwrite(fd, buf, blocksize);
273
274
275 memset(buf, 0, blocksize);
276 for (i = 0; i < journal_blocks; i++)
277 xwrite(fd, buf, blocksize);
278
279 memcpy(&((struct reiserfs_journal_header *)buf)->jh_journal, &sb->sb_journal, sizeof(sb->sb_journal));
280 xwrite(fd, buf, blocksize);
281
282
283
284 buf[0] = 0x01;
285
286 for (i = 1; i < bitmap_blocks; i++) {
287 xlseek(fd, i*8*blocksize * blocksize, SEEK_SET);
288
289 if (i == bitmap_blocks - 1 && (blocks % (8*blocksize))) {
290 unsigned n = 8*blocksize - blocks % (8*blocksize);
291 unsigned j = n / 8;
292 buf[blocksize - j - 1] |= 0x7F00 >> (n & 7);
293 memset(buf + blocksize - j, 0xFF, j);
294 }
295 xwrite(fd, buf, blocksize);
296 }
297
298
299
300 memset(buf, 0, blocksize);
301 root = (struct block_head *)buf;
302 STORE_LE(root->blk2_level, 1);
303 STORE_LE(root->blk2_nr_item, 2);
304 STORE_LE(root->blk2_free_space, blocksize - sizeof(struct block_head));
305
306
307
308
309buf[4] = 0134;
310buf[24] = 01;
311buf[28] = 02;
312buf[42] = 054;
313buf[44] = 0324;
314buf[45] = 017;
315buf[46] = 01;
316buf[48] = 01;
317buf[52] = 02;
318buf[56] = 01;
319buf[60] = 0364;
320buf[61] = 01;
321buf[64] = 02;
322buf[66] = 060;
323buf[68] = 0244;
324buf[69] = 017;
325buf[4004] = 01;
326buf[4008] = 01;
327buf[4012] = 02;
328buf[4016] = 050;
329buf[4018] = 04;
330buf[4020] = 02;
331buf[4028] = 01;
332buf[4032] = 040;
333buf[4034] = 04;
334
335buf[4036] = 056; buf[4037] = 056;
336buf[4044] = 056;
337
338buf[4052] = 0355;
339buf[4053] = 0101;
340buf[4056] = 03;
341buf[4060] = 060;
342 buf[4076] = 0173;
343 buf[4077] = 0240;
344 buf[4078] = 0344;
345 buf[4079] = 0112;
346 buf[4080] = 0173;
347 buf[4081] = 0240;
348 buf[4082] = 0344;
349 buf[4083] = 0112;
350 buf[4084] = 0173;
351 buf[4085] = 0240;
352 buf[4086] = 0344;
353 buf[4087] = 0112;
354buf[4088] = 01;
355
356
357 xlseek(fd, block * blocksize, SEEK_SET);
358 xwrite(fd, buf, blocksize);
359
360
361 if (ENABLE_FEATURE_CLEAN_UP) {
362 free(buf);
363 free(sb);
364 }
365
366 xclose(fd);
367 return EXIT_SUCCESS;
368}
369