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