blob: 390aef86c524d00910a617be171794cad19e9a8a [file] [log] [blame]
Vladimir Dronnikov0d8ea642009-11-02 10:41:46 +01001/* vi: set sw=4 ts=4: */
2/*
3 * mkfs_reiser: utility to create ReiserFS filesystem
4 *
5 * Busybox'ed (2009) by Vladimir Dronnikov <dronnikov@gmail.com>
6 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2, see file LICENSE in this source tree.
Vladimir Dronnikov0d8ea642009-11-02 10:41:46 +01008 */
Denys Vlasenkodd898c92016-11-23 11:46:32 +01009//config:config MKFS_REISER
10//config: bool "mkfs_reiser"
11//config: default n
12//config: select PLATFORM_LINUX
13//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020014//config: Utility to create ReiserFS filesystems.
15//config: Note: this applet needs a lot of testing and polishing.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010016
17//applet:IF_MKFS_REISER(APPLET_ODDNAME(mkfs.reiser, mkfs_reiser, BB_DIR_SBIN, BB_SUID_DROP, mkfs_reiser))
18
19//kbuild:lib-$(CONFIG_MKFS_REISER) += mkfs_reiser.o
Pere Orga5bc8c002011-04-11 03:29:49 +020020
21//usage:#define mkfs_reiser_trivial_usage
22//usage: "[-f] [-l LABEL] BLOCKDEV [4K-BLOCKS]"
23//usage:#define mkfs_reiser_full_usage "\n\n"
24//usage: "Make a ReiserFS V3 filesystem\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020025//usage: "\n -f Force"
26//usage: "\n -l LBL Volume label"
27
Vladimir Dronnikov0d8ea642009-11-02 10:41:46 +010028#include "libbb.h"
29#include <linux/fs.h>
Vladimir Dronnikov0d8ea642009-11-02 10:41:46 +010030
31char BUG_wrong_field_size(void);
32#define STORE_LE(field, value) \
33do { \
34 if (sizeof(field) == 4) \
Denys Vlasenko67743862010-05-09 00:13:40 +020035 field = SWAP_LE32(value); \
Vladimir Dronnikov0d8ea642009-11-02 10:41:46 +010036 else if (sizeof(field) == 2) \
Denys Vlasenko67743862010-05-09 00:13:40 +020037 field = SWAP_LE16(value); \
Vladimir Dronnikov0d8ea642009-11-02 10:41:46 +010038 else if (sizeof(field) == 1) \
39 field = (value); \
40 else \
41 BUG_wrong_field_size(); \
42} while (0)
43
44#define FETCH_LE32(field) \
Denys Vlasenko67743862010-05-09 00:13:40 +020045 (sizeof(field) == 4 ? SWAP_LE32(field) : BUG_wrong_field_size())
Vladimir Dronnikov0d8ea642009-11-02 10:41:46 +010046
47struct journal_params {
48 uint32_t jp_journal_1st_block; /* where does journal start from on its device */
49 uint32_t jp_journal_dev; /* journal device st_rdev */
50 uint32_t jp_journal_size; /* size of the journal on FS creation. used to make sure they don't overflow it */
51 uint32_t jp_journal_trans_max; /* max number of blocks in a transaction. */
52 uint32_t jp_journal_magic; /* random value made on fs creation (this was sb_journal_block_count) */
53 uint32_t jp_journal_max_batch; /* max number of blocks to batch into a trans */
54 uint32_t jp_journal_max_commit_age; /* in seconds, how old can an async commit be */
55 uint32_t jp_journal_max_trans_age; /* in seconds, how old can a transaction be */
56};
57
58struct reiserfs_journal_header {
59 uint32_t jh_last_flush_trans_id; /* id of last fully flushed transaction */
60 uint32_t jh_first_unflushed_offset; /* offset in the log of where to start replay after a crash */
61 uint32_t jh_mount_id;
62 struct journal_params jh_journal;
63 uint32_t jh_last_check_mount_id; /* the mount id of the fs during the last reiserfsck --check. */
64};
65
66struct reiserfs_super_block {
67 uint32_t sb_block_count; /* 0 number of block on data device */
68 uint32_t sb_free_blocks; /* 4 free blocks count */
69 uint32_t sb_root_block; /* 8 root of the tree */
70
71 struct journal_params sb_journal; /* 12 */
72
73 uint16_t sb_blocksize; /* 44 */
74 uint16_t sb_oid_maxsize; /* 46 max size of object id array, see get_objectid() commentary */
75 uint16_t sb_oid_cursize; /* 48 current size of object id array */
76 uint16_t sb_umount_state; /* 50 this is set to 1 when filesystem was umounted, to 2 - when not */
77
78 char s_magic[10]; /* 52 "ReIsErFs" or "ReIsEr2Fs" or "ReIsEr3Fs" */
79 uint16_t sb_fs_state; /* 62 it is set to used by fsck to mark which phase of rebuilding is done (used for fsck debugging) */
Maninder Singh97c64912015-05-25 13:46:36 +020080 uint32_t sb_hash_function_code; /* 64 code of function which was/is/will be used to sort names in a directory. See codes in above */
Vladimir Dronnikov0d8ea642009-11-02 10:41:46 +010081 uint16_t sb_tree_height; /* 68 height of filesytem tree. Tree consisting of only one root block has 2 here */
82 uint16_t sb_bmap_nr; /* 70 amount of bitmap blocks needed to address each block of file system */
83 uint16_t sb_version; /* 72 this field is only reliable on filesystem with non-standard journal */
84 uint16_t sb_reserved_for_journal; /* 74 size in blocks of journal area on main device, we need to keep after non-standard journal relocation */
85 uint32_t sb_inode_generation; /* 76 */
86 uint32_t sb_flags; /* 80 Right now used only by inode-attributes, if enabled */
87 unsigned char s_uuid[16]; /* 84 filesystem unique identifier */
88 unsigned char s_label[16]; /* 100 filesystem volume label */
89 uint16_t sb_mnt_count; /* 116 */
90 uint16_t sb_max_mnt_count; /* 118 */
91 uint32_t sb_lastcheck; /* 120 */
92 uint32_t sb_check_interval; /* 124 */
93/* zero filled by mkreiserfs and reiserfs_convert_objectid_map_v1() so any additions must be updated there as well. */
94 char s_unused[76]; /* 128 */
95 /* 204 */
96};
97
98/* Header of a disk block. More precisely, header of a formatted leaf
99 or internal node, and not the header of an unformatted node. */
100struct block_head {
101 uint16_t blk2_level; /* Level of a block in the tree. */
102 uint16_t blk2_nr_item; /* Number of keys/items in a block. */
103 uint16_t blk2_free_space; /* Block free space in bytes. */
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 /* 30 mounts */
113#define DEFAULT_CHECK_INTERVAL (180 * 60 * 60 * 24) /* 180 days */
114
115#define FS_CLEANLY_UMOUNTED 1 /* this was REISERFS_VALID_FS */
116
117#define JOURNAL_MIN_SIZE 512
118/* biggest possible single transaction, don't change for now (8/3/99) */
119#define JOURNAL_TRANS_MAX 1024
120#define JOURNAL_TRANS_MIN 256 /* need to check whether it works */
121#define JOURNAL_DEFAULT_RATIO 8 /* default journal size / max trans length */
122#define JOURNAL_MIN_RATIO 2
123/* max blocks to batch into one transaction, don't make this any bigger than 900 */
124#define JOURNAL_MAX_BATCH 900
125#define JOURNAL_MAX_COMMIT_AGE 30
126
127
128// Standard mkreiserfs 3.6.21:
129// -b | --block-size N size of file-system block, in bytes
130// -j | --journal-device FILE path to separate device to hold journal
131// -s | --journal-size N size of the journal in blocks
132// -o | --journal-offset N offset of the journal from the start of
133// the separate device, in blocks
134// -t | --transaction-max-size N maximal size of transaction, in blocks
135// -B | --badblocks file store all bad blocks given in file on the fs
136// -h | --hash rupasov|tea|r5 hash function to use by default
137// -u | --uuid UUID store UUID in the superblock
138// -l | --label LABEL store LABEL in the superblock
139// --format 3.5|3.6 old 3.5 format or newer 3.6
140// -f | --force specified once, make mkreiserfs the whole
141// disk, not block device or mounted partition;
142// specified twice, do not ask for confirmation
143// -q | --quiet quiet work without messages, progress and
144// questions. Useful if run in a script. For use
145// by end users only.
146// -d | --debug print debugging information during mkreiser
147// -V print version and exit
148
149// Options not commented below are taken but silently ignored:
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, // label
160 OPT_f = 1 << 9, // ask no questions
161 OPT_q = 1 << 10,
162 OPT_d = 1 << 11,
163 //OPT_V = 1 << 12, // -V version. bbox applets don't support that
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 // using global "option_mask32" instead of local "opts":
182 // we are register starved here
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200183 /*opts =*/ getopt32(argv, "^" "b:+j:s:o:t:B:h:u:l:fqd" "\0" "-1",
Vladimir Dronnikov0d8ea642009-11-02 10:41:46 +0100184 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &label);
185 argv += optind; // argv[0] -- device
186
187 // check the device is a block device
188 fd = xopen(argv[0], O_WRONLY | O_EXCL);
Denys Vlasenko8d3e2252010-08-31 12:42:06 +0200189 xfstat(fd, &st, argv[0]);
Vladimir Dronnikov0d8ea642009-11-02 10:41:46 +0100190 if (!S_ISBLK(st.st_mode) && !(option_mask32 & OPT_f))
Denys Vlasenko8d3e2252010-08-31 12:42:06 +0200191 bb_error_msg_and_die("%s: not a block device", argv[0]);
Vladimir Dronnikov0d8ea642009-11-02 10:41:46 +0100192
193 // check if it is mounted
194 // N.B. what if we format a file? find_mount_point will return false negative since
Denys Vlasenko40e7d252010-02-01 23:48:27 +0100195 // it is loop block device which is mounted!
Vladimir Dronnikov0d8ea642009-11-02 10:41:46 +0100196 if (find_mount_point(argv[0], 0))
197 bb_error_msg_and_die("can't format mounted filesystem");
198
199 // open the device, get size in blocks
Denys Vlasenko40e7d252010-02-01 23:48:27 +0100200 blocks = get_volume_size_in_bytes(fd, argv[1], blocksize, /*extend:*/ 1) / blocksize;
Vladimir Dronnikov0d8ea642009-11-02 10:41:46 +0100201
202 // block number sanity check
203 // we have a limit: skipped area, super block, journal and root block
204 // all have to be addressed by one first bitmap
205 block = REISERFS_DISK_OFFSET_IN_BYTES / blocksize // boot area
206 + 1 // sb
207 + 1 // bitmap#0
208 + journal_blocks+1 // journal
209 ;
210
211 // count overhead
212 bitmap_blocks = (blocks - 1) / (blocksize * 8) + 1;
213 i = block + bitmap_blocks;
214
215 // check overhead
216 if (MIN(blocksize * 8, blocks) < i)
217 bb_error_msg_and_die("need >= %u blocks", i);
218
219 // ask confirmation?
220 // TODO: ???
221
222 // wipe out first REISERFS_DISK_OFFSET_IN_BYTES of device
223 // TODO: do we really need to wipe?!
224 xlseek(fd, REISERFS_DISK_OFFSET_IN_BYTES, SEEK_SET);
225
226 // fill superblock
227 sb = (struct reiserfs_super_block *)xzalloc(blocksize);
228 // block count
229 STORE_LE(sb->sb_block_count, blocks);
230 STORE_LE(sb->sb_free_blocks, blocks - i);
231 // TODO: decypher!
232 STORE_LE(sb->sb_root_block, block);
233 // fill journal related fields
234 jp = &sb->sb_journal;
235 STORE_LE(jp->jp_journal_1st_block, REISERFS_DISK_OFFSET_IN_BYTES / blocksize + 1/*sb*/ + 1/*bmp#0*/);
236 timestamp = time(NULL);
Denys Vlasenko0ed5f7a2014-03-05 18:58:15 +0100237 srand(timestamp);
238 STORE_LE(jp->jp_journal_magic, rand());
Vladimir Dronnikov0d8ea642009-11-02 10:41:46 +0100239 STORE_LE(jp->jp_journal_size, journal_blocks);
240 STORE_LE(jp->jp_journal_trans_max, JOURNAL_TRANS_MAX);
241 STORE_LE(jp->jp_journal_max_batch, JOURNAL_MAX_BATCH);
242 STORE_LE(jp->jp_journal_max_commit_age, JOURNAL_MAX_COMMIT_AGE);
243 // sizes
244 STORE_LE(sb->sb_blocksize, blocksize);
245 STORE_LE(sb->sb_oid_maxsize, (blocksize - sizeof(*sb)) / sizeof(uint32_t) / 2 * 2);
246 STORE_LE(sb->sb_oid_cursize, 2); // "." and ".."
247 strcpy(sb->s_magic, REISERFS_3_6_SUPER_MAGIC_STRING);
248 STORE_LE(sb->sb_bmap_nr, (bitmap_blocks > ((1LL << 16) - 1)) ? 0 : bitmap_blocks);
249 // misc
250 STORE_LE(sb->sb_version, REISERFS_FORMAT_3_6);
251 STORE_LE(sb->sb_lastcheck, timestamp);
252 STORE_LE(sb->sb_check_interval, DEFAULT_CHECK_INTERVAL);
253 STORE_LE(sb->sb_mnt_count, 1);
254 STORE_LE(sb->sb_max_mnt_count, DEFAULT_MAX_MNT_COUNT);
255 STORE_LE(sb->sb_umount_state, FS_CLEANLY_UMOUNTED);
256 STORE_LE(sb->sb_tree_height, 2);
257 STORE_LE(sb->sb_hash_function_code, 3); // R5_HASH
258 STORE_LE(sb->sb_flags, 1);
259 //STORE_LE(sb->sb_reserved_for_journal, 0);
260 // create UUID
261 generate_uuid(sb->s_uuid);
262 // write the label
263 safe_strncpy((char *)sb->s_label, label, sizeof(sb->s_label));
264
265 // TODO: EMPIRIC! ENDIANNESS!
266 // superblock has only 204 bytes. What are these?
267 buf = (uint8_t *)sb;
268 buf[205] = 1;
269 buf[209] = 3;
270
271 // put superblock
272 xwrite(fd, sb, blocksize);
273
274 // create bitmaps
275 buf = xzalloc(blocksize);
276
277 // bitmap #0 uses initial "block"+1 blocks
278 i = block + 1;
279 memset(buf, 0xFF, i / 8);
280 buf[i / 8] = (1 << (i & 7)) - 1; //0..7 => 00000000..01111111
281 // mark trailing absent blocks, if any
282 if (blocks < 8*blocksize) {
283 unsigned n = 8*blocksize - blocks;
284 i = n / 8;
285 buf[blocksize - i - 1] |= 0x7F00 >> (n & 7); //0..7 => 00000000..11111110
286 memset(buf + blocksize - i, 0xFF, i); // N.B. no overflow here!
287 }
288 // put bitmap #0
289 xwrite(fd, buf, blocksize);
290
291 // now go journal blocks
292 memset(buf, 0, blocksize);
293 for (i = 0; i < journal_blocks; i++)
294 xwrite(fd, buf, blocksize);
295 // dump journal control block
296 memcpy(&((struct reiserfs_journal_header *)buf)->jh_journal, &sb->sb_journal, sizeof(sb->sb_journal));
297 xwrite(fd, buf, blocksize);
298
299 // other bitmaps are in every (8*blocksize)-th block
300 // N.B. they use the only block -- namely bitmap itself!
301 buf[0] = 0x01;
302 // put bitmaps
303 for (i = 1; i < bitmap_blocks; i++) {
304 xlseek(fd, i*8*blocksize * blocksize, SEEK_SET);
305 // mark trailing absent blocks, if any
306 if (i == bitmap_blocks - 1 && (blocks % (8*blocksize))) {
307 unsigned n = 8*blocksize - blocks % (8*blocksize);
308 unsigned j = n / 8;
309 buf[blocksize - j - 1] |= 0x7F00 >> (n & 7); //0..7 => 00000000..11111110
310 memset(buf + blocksize - j, 0xFF, j); // N.B. no overflow here!
311 }
312 xwrite(fd, buf, blocksize);
313 }
314
315 // fill root block
316 // block head
317 memset(buf, 0, blocksize);
318 root = (struct block_head *)buf;
319 STORE_LE(root->blk2_level, 1); // leaf node
320 STORE_LE(root->blk2_nr_item, 2); // "." and ".."
321 STORE_LE(root->blk2_free_space, blocksize - sizeof(struct block_head));
322 // item head
323 // root directory
324 // TODO: EMPIRIC! ENDIANNESS!
325 // TODO: indented assignments seem to be timestamps
326buf[4] = 0134;
327buf[24] = 01;
328buf[28] = 02;
329buf[42] = 054;
330buf[44] = 0324;
331buf[45] = 017;
332buf[46] = 01;
333buf[48] = 01;
334buf[52] = 02;
335buf[56] = 01;
336buf[60] = 0364;
337buf[61] = 01;
338buf[64] = 02;
339buf[66] = 060;
340buf[68] = 0244;
341buf[69] = 017;
342buf[4004] = 01;
343buf[4008] = 01;
344buf[4012] = 02;
345buf[4016] = 050;
346buf[4018] = 04;
347buf[4020] = 02;
348buf[4028] = 01;
349buf[4032] = 040;
350buf[4034] = 04;
351
352buf[4036] = 056; buf[4037] = 056; // ".."
353buf[4044] = 056; // "."
354
355buf[4052] = 0355;
356buf[4053] = 0101;
357buf[4056] = 03;
358buf[4060] = 060;
359 buf[4076] = 0173;
360 buf[4077] = 0240;
361 buf[4078] = 0344;
362 buf[4079] = 0112;
363 buf[4080] = 0173;
364 buf[4081] = 0240;
365 buf[4082] = 0344;
366 buf[4083] = 0112;
367 buf[4084] = 0173;
368 buf[4085] = 0240;
369 buf[4086] = 0344;
370 buf[4087] = 0112;
371buf[4088] = 01;
372
373 // put root block
374 xlseek(fd, block * blocksize, SEEK_SET);
375 xwrite(fd, buf, blocksize);
376
377 // cleanup
378 if (ENABLE_FEATURE_CLEAN_UP) {
379 free(buf);
380 free(sb);
381 }
382
383 xclose(fd);
384 return EXIT_SUCCESS;
385}