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