blob: d2eaf5f94185bf235df01582a0617513965acd6f [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
Vladimir Dronnikov0d8ea642009-11-02 10:41:46 +010031struct journal_params {
32 uint32_t jp_journal_1st_block; /* where does journal start from on its device */
33 uint32_t jp_journal_dev; /* journal device st_rdev */
34 uint32_t jp_journal_size; /* size of the journal on FS creation. used to make sure they don't overflow it */
35 uint32_t jp_journal_trans_max; /* max number of blocks in a transaction. */
36 uint32_t jp_journal_magic; /* random value made on fs creation (this was sb_journal_block_count) */
37 uint32_t jp_journal_max_batch; /* max number of blocks to batch into a trans */
38 uint32_t jp_journal_max_commit_age; /* in seconds, how old can an async commit be */
39 uint32_t jp_journal_max_trans_age; /* in seconds, how old can a transaction be */
40};
41
42struct reiserfs_journal_header {
43 uint32_t jh_last_flush_trans_id; /* id of last fully flushed transaction */
44 uint32_t jh_first_unflushed_offset; /* offset in the log of where to start replay after a crash */
45 uint32_t jh_mount_id;
46 struct journal_params jh_journal;
47 uint32_t jh_last_check_mount_id; /* the mount id of the fs during the last reiserfsck --check. */
48};
49
50struct reiserfs_super_block {
51 uint32_t sb_block_count; /* 0 number of block on data device */
52 uint32_t sb_free_blocks; /* 4 free blocks count */
53 uint32_t sb_root_block; /* 8 root of the tree */
54
55 struct journal_params sb_journal; /* 12 */
56
57 uint16_t sb_blocksize; /* 44 */
58 uint16_t sb_oid_maxsize; /* 46 max size of object id array, see get_objectid() commentary */
59 uint16_t sb_oid_cursize; /* 48 current size of object id array */
60 uint16_t sb_umount_state; /* 50 this is set to 1 when filesystem was umounted, to 2 - when not */
61
62 char s_magic[10]; /* 52 "ReIsErFs" or "ReIsEr2Fs" or "ReIsEr3Fs" */
63 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 +020064 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 +010065 uint16_t sb_tree_height; /* 68 height of filesytem tree. Tree consisting of only one root block has 2 here */
66 uint16_t sb_bmap_nr; /* 70 amount of bitmap blocks needed to address each block of file system */
67 uint16_t sb_version; /* 72 this field is only reliable on filesystem with non-standard journal */
68 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 */
69 uint32_t sb_inode_generation; /* 76 */
70 uint32_t sb_flags; /* 80 Right now used only by inode-attributes, if enabled */
71 unsigned char s_uuid[16]; /* 84 filesystem unique identifier */
72 unsigned char s_label[16]; /* 100 filesystem volume label */
73 uint16_t sb_mnt_count; /* 116 */
74 uint16_t sb_max_mnt_count; /* 118 */
75 uint32_t sb_lastcheck; /* 120 */
76 uint32_t sb_check_interval; /* 124 */
77/* zero filled by mkreiserfs and reiserfs_convert_objectid_map_v1() so any additions must be updated there as well. */
78 char s_unused[76]; /* 128 */
79 /* 204 */
80};
81
82/* Header of a disk block. More precisely, header of a formatted leaf
83 or internal node, and not the header of an unformatted node. */
84struct block_head {
85 uint16_t blk2_level; /* Level of a block in the tree. */
86 uint16_t blk2_nr_item; /* Number of keys/items in a block. */
87 uint16_t blk2_free_space; /* Block free space in bytes. */
88 uint16_t blk_reserved;
89 uint32_t reserved[4];
90};
91
92#define REISERFS_DISK_OFFSET_IN_BYTES (64 * 1024)
93
94#define REISERFS_3_6_SUPER_MAGIC_STRING "ReIsEr2Fs"
95#define REISERFS_FORMAT_3_6 2
96#define DEFAULT_MAX_MNT_COUNT 30 /* 30 mounts */
97#define DEFAULT_CHECK_INTERVAL (180 * 60 * 60 * 24) /* 180 days */
98
99#define FS_CLEANLY_UMOUNTED 1 /* this was REISERFS_VALID_FS */
100
101#define JOURNAL_MIN_SIZE 512
102/* biggest possible single transaction, don't change for now (8/3/99) */
103#define JOURNAL_TRANS_MAX 1024
104#define JOURNAL_TRANS_MIN 256 /* need to check whether it works */
105#define JOURNAL_DEFAULT_RATIO 8 /* default journal size / max trans length */
106#define JOURNAL_MIN_RATIO 2
107/* max blocks to batch into one transaction, don't make this any bigger than 900 */
108#define JOURNAL_MAX_BATCH 900
109#define JOURNAL_MAX_COMMIT_AGE 30
110
111
112// Standard mkreiserfs 3.6.21:
113// -b | --block-size N size of file-system block, in bytes
114// -j | --journal-device FILE path to separate device to hold journal
115// -s | --journal-size N size of the journal in blocks
116// -o | --journal-offset N offset of the journal from the start of
117// the separate device, in blocks
118// -t | --transaction-max-size N maximal size of transaction, in blocks
119// -B | --badblocks file store all bad blocks given in file on the fs
120// -h | --hash rupasov|tea|r5 hash function to use by default
121// -u | --uuid UUID store UUID in the superblock
122// -l | --label LABEL store LABEL in the superblock
123// --format 3.5|3.6 old 3.5 format or newer 3.6
124// -f | --force specified once, make mkreiserfs the whole
125// disk, not block device or mounted partition;
126// specified twice, do not ask for confirmation
127// -q | --quiet quiet work without messages, progress and
128// questions. Useful if run in a script. For use
129// by end users only.
130// -d | --debug print debugging information during mkreiser
131// -V print version and exit
132
133// Options not commented below are taken but silently ignored:
134enum {
135 OPT_b = 1 << 0,
136 OPT_j = 1 << 1,
137 OPT_s = 1 << 2,
138 OPT_o = 1 << 3,
139 OPT_t = 1 << 4,
140 OPT_B = 1 << 5,
141 OPT_h = 1 << 6,
142 OPT_u = 1 << 7,
143 OPT_l = 1 << 8, // label
144 OPT_f = 1 << 9, // ask no questions
145 OPT_q = 1 << 10,
146 OPT_d = 1 << 11,
147 //OPT_V = 1 << 12, // -V version. bbox applets don't support that
148};
149
150int mkfs_reiser_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
151int mkfs_reiser_main(int argc UNUSED_PARAM, char **argv)
152{
153 unsigned blocksize = 4096;
154 unsigned journal_blocks = 8192;
155 unsigned blocks, bitmap_blocks, i, block;
156 time_t timestamp;
157 const char *label = "";
158 struct stat st;
159 int fd;
160 uint8_t *buf;
161 struct reiserfs_super_block *sb;
162 struct journal_params *jp;
163 struct block_head *root;
164
165 // using global "option_mask32" instead of local "opts":
166 // we are register starved here
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200167 /*opts =*/ getopt32(argv, "^" "b:+j:s:o:t:B:h:u:l:fqd" "\0" "-1",
Vladimir Dronnikov0d8ea642009-11-02 10:41:46 +0100168 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &label);
169 argv += optind; // argv[0] -- device
170
171 // check the device is a block device
172 fd = xopen(argv[0], O_WRONLY | O_EXCL);
Denys Vlasenko8d3e2252010-08-31 12:42:06 +0200173 xfstat(fd, &st, argv[0]);
Vladimir Dronnikov0d8ea642009-11-02 10:41:46 +0100174 if (!S_ISBLK(st.st_mode) && !(option_mask32 & OPT_f))
Denys Vlasenko8d3e2252010-08-31 12:42:06 +0200175 bb_error_msg_and_die("%s: not a block device", argv[0]);
Vladimir Dronnikov0d8ea642009-11-02 10:41:46 +0100176
177 // check if it is mounted
178 // N.B. what if we format a file? find_mount_point will return false negative since
Denys Vlasenko40e7d252010-02-01 23:48:27 +0100179 // it is loop block device which is mounted!
Vladimir Dronnikov0d8ea642009-11-02 10:41:46 +0100180 if (find_mount_point(argv[0], 0))
James Byrne69374872019-07-02 11:35:03 +0200181 bb_simple_error_msg_and_die("can't format mounted filesystem");
Vladimir Dronnikov0d8ea642009-11-02 10:41:46 +0100182
183 // open the device, get size in blocks
Denys Vlasenko40e7d252010-02-01 23:48:27 +0100184 blocks = get_volume_size_in_bytes(fd, argv[1], blocksize, /*extend:*/ 1) / blocksize;
Vladimir Dronnikov0d8ea642009-11-02 10:41:46 +0100185
186 // block number sanity check
187 // we have a limit: skipped area, super block, journal and root block
188 // all have to be addressed by one first bitmap
189 block = REISERFS_DISK_OFFSET_IN_BYTES / blocksize // boot area
190 + 1 // sb
191 + 1 // bitmap#0
192 + journal_blocks+1 // journal
193 ;
194
195 // count overhead
196 bitmap_blocks = (blocks - 1) / (blocksize * 8) + 1;
197 i = block + bitmap_blocks;
198
199 // check overhead
200 if (MIN(blocksize * 8, blocks) < i)
201 bb_error_msg_and_die("need >= %u blocks", i);
202
203 // ask confirmation?
204 // TODO: ???
205
206 // wipe out first REISERFS_DISK_OFFSET_IN_BYTES of device
207 // TODO: do we really need to wipe?!
208 xlseek(fd, REISERFS_DISK_OFFSET_IN_BYTES, SEEK_SET);
209
210 // fill superblock
211 sb = (struct reiserfs_super_block *)xzalloc(blocksize);
212 // block count
213 STORE_LE(sb->sb_block_count, blocks);
214 STORE_LE(sb->sb_free_blocks, blocks - i);
215 // TODO: decypher!
216 STORE_LE(sb->sb_root_block, block);
217 // fill journal related fields
218 jp = &sb->sb_journal;
219 STORE_LE(jp->jp_journal_1st_block, REISERFS_DISK_OFFSET_IN_BYTES / blocksize + 1/*sb*/ + 1/*bmp#0*/);
220 timestamp = time(NULL);
Denys Vlasenko0ed5f7a2014-03-05 18:58:15 +0100221 srand(timestamp);
222 STORE_LE(jp->jp_journal_magic, rand());
Vladimir Dronnikov0d8ea642009-11-02 10:41:46 +0100223 STORE_LE(jp->jp_journal_size, journal_blocks);
224 STORE_LE(jp->jp_journal_trans_max, JOURNAL_TRANS_MAX);
225 STORE_LE(jp->jp_journal_max_batch, JOURNAL_MAX_BATCH);
226 STORE_LE(jp->jp_journal_max_commit_age, JOURNAL_MAX_COMMIT_AGE);
227 // sizes
228 STORE_LE(sb->sb_blocksize, blocksize);
229 STORE_LE(sb->sb_oid_maxsize, (blocksize - sizeof(*sb)) / sizeof(uint32_t) / 2 * 2);
230 STORE_LE(sb->sb_oid_cursize, 2); // "." and ".."
231 strcpy(sb->s_magic, REISERFS_3_6_SUPER_MAGIC_STRING);
232 STORE_LE(sb->sb_bmap_nr, (bitmap_blocks > ((1LL << 16) - 1)) ? 0 : bitmap_blocks);
233 // misc
234 STORE_LE(sb->sb_version, REISERFS_FORMAT_3_6);
235 STORE_LE(sb->sb_lastcheck, timestamp);
236 STORE_LE(sb->sb_check_interval, DEFAULT_CHECK_INTERVAL);
237 STORE_LE(sb->sb_mnt_count, 1);
238 STORE_LE(sb->sb_max_mnt_count, DEFAULT_MAX_MNT_COUNT);
239 STORE_LE(sb->sb_umount_state, FS_CLEANLY_UMOUNTED);
240 STORE_LE(sb->sb_tree_height, 2);
241 STORE_LE(sb->sb_hash_function_code, 3); // R5_HASH
242 STORE_LE(sb->sb_flags, 1);
243 //STORE_LE(sb->sb_reserved_for_journal, 0);
244 // create UUID
245 generate_uuid(sb->s_uuid);
246 // write the label
247 safe_strncpy((char *)sb->s_label, label, sizeof(sb->s_label));
248
249 // TODO: EMPIRIC! ENDIANNESS!
250 // superblock has only 204 bytes. What are these?
251 buf = (uint8_t *)sb;
252 buf[205] = 1;
253 buf[209] = 3;
254
255 // put superblock
256 xwrite(fd, sb, blocksize);
257
258 // create bitmaps
259 buf = xzalloc(blocksize);
260
261 // bitmap #0 uses initial "block"+1 blocks
262 i = block + 1;
263 memset(buf, 0xFF, i / 8);
264 buf[i / 8] = (1 << (i & 7)) - 1; //0..7 => 00000000..01111111
265 // mark trailing absent blocks, if any
266 if (blocks < 8*blocksize) {
267 unsigned n = 8*blocksize - blocks;
268 i = n / 8;
269 buf[blocksize - i - 1] |= 0x7F00 >> (n & 7); //0..7 => 00000000..11111110
270 memset(buf + blocksize - i, 0xFF, i); // N.B. no overflow here!
271 }
272 // put bitmap #0
273 xwrite(fd, buf, blocksize);
274
275 // now go journal blocks
276 memset(buf, 0, blocksize);
277 for (i = 0; i < journal_blocks; i++)
278 xwrite(fd, buf, blocksize);
279 // dump journal control block
280 memcpy(&((struct reiserfs_journal_header *)buf)->jh_journal, &sb->sb_journal, sizeof(sb->sb_journal));
281 xwrite(fd, buf, blocksize);
282
283 // other bitmaps are in every (8*blocksize)-th block
284 // N.B. they use the only block -- namely bitmap itself!
285 buf[0] = 0x01;
286 // put bitmaps
287 for (i = 1; i < bitmap_blocks; i++) {
288 xlseek(fd, i*8*blocksize * blocksize, SEEK_SET);
289 // mark trailing absent blocks, if any
290 if (i == bitmap_blocks - 1 && (blocks % (8*blocksize))) {
291 unsigned n = 8*blocksize - blocks % (8*blocksize);
292 unsigned j = n / 8;
293 buf[blocksize - j - 1] |= 0x7F00 >> (n & 7); //0..7 => 00000000..11111110
294 memset(buf + blocksize - j, 0xFF, j); // N.B. no overflow here!
295 }
296 xwrite(fd, buf, blocksize);
297 }
298
299 // fill root block
300 // block head
301 memset(buf, 0, blocksize);
302 root = (struct block_head *)buf;
303 STORE_LE(root->blk2_level, 1); // leaf node
304 STORE_LE(root->blk2_nr_item, 2); // "." and ".."
305 STORE_LE(root->blk2_free_space, blocksize - sizeof(struct block_head));
306 // item head
307 // root directory
308 // TODO: EMPIRIC! ENDIANNESS!
309 // TODO: indented assignments seem to be timestamps
310buf[4] = 0134;
311buf[24] = 01;
312buf[28] = 02;
313buf[42] = 054;
314buf[44] = 0324;
315buf[45] = 017;
316buf[46] = 01;
317buf[48] = 01;
318buf[52] = 02;
319buf[56] = 01;
320buf[60] = 0364;
321buf[61] = 01;
322buf[64] = 02;
323buf[66] = 060;
324buf[68] = 0244;
325buf[69] = 017;
326buf[4004] = 01;
327buf[4008] = 01;
328buf[4012] = 02;
329buf[4016] = 050;
330buf[4018] = 04;
331buf[4020] = 02;
332buf[4028] = 01;
333buf[4032] = 040;
334buf[4034] = 04;
335
336buf[4036] = 056; buf[4037] = 056; // ".."
337buf[4044] = 056; // "."
338
339buf[4052] = 0355;
340buf[4053] = 0101;
341buf[4056] = 03;
342buf[4060] = 060;
343 buf[4076] = 0173;
344 buf[4077] = 0240;
345 buf[4078] = 0344;
346 buf[4079] = 0112;
347 buf[4080] = 0173;
348 buf[4081] = 0240;
349 buf[4082] = 0344;
350 buf[4083] = 0112;
351 buf[4084] = 0173;
352 buf[4085] = 0240;
353 buf[4086] = 0344;
354 buf[4087] = 0112;
355buf[4088] = 01;
356
357 // put root block
358 xlseek(fd, block * blocksize, SEEK_SET);
359 xwrite(fd, buf, blocksize);
360
361 // cleanup
362 if (ENABLE_FEATURE_CLEAN_UP) {
363 free(buf);
364 free(sb);
365 }
366
367 xclose(fd);
368 return EXIT_SUCCESS;
369}