1 /* vi: set sw=4 ts=4: */
3 * mkfs_reiser: utility to create ReiserFS filesystem
5 * Busybox'ed (2009) by Vladimir Dronnikov <dronnikov@gmail.com>
7 * Licensed under GPLv2, see file LICENSE in this source tree.
10 //usage:#define mkfs_reiser_trivial_usage
11 //usage: "[-f] [-l LABEL] BLOCKDEV [4K-BLOCKS]"
12 //usage:#define mkfs_reiser_full_usage "\n\n"
13 //usage: "Make a ReiserFS V3 filesystem\n"
14 //usage: "\n -f Force"
15 //usage: "\n -l LBL Volume label"
20 char BUG_wrong_field_size(void);
21 #define STORE_LE(field, value) \
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) \
30 BUG_wrong_field_size(); \
33 #define FETCH_LE32(field) \
34 (sizeof(field) == 4 ? SWAP_LE32(field) : BUG_wrong_field_size())
36 struct journal_params
{
37 uint32_t jp_journal_1st_block
; /* where does journal start from on its device */
38 uint32_t jp_journal_dev
; /* journal device st_rdev */
39 uint32_t jp_journal_size
; /* size of the journal on FS creation. used to make sure they don't overflow it */
40 uint32_t jp_journal_trans_max
; /* max number of blocks in a transaction. */
41 uint32_t jp_journal_magic
; /* random value made on fs creation (this was sb_journal_block_count) */
42 uint32_t jp_journal_max_batch
; /* max number of blocks to batch into a trans */
43 uint32_t jp_journal_max_commit_age
; /* in seconds, how old can an async commit be */
44 uint32_t jp_journal_max_trans_age
; /* in seconds, how old can a transaction be */
47 struct reiserfs_journal_header
{
48 uint32_t jh_last_flush_trans_id
; /* id of last fully flushed transaction */
49 uint32_t jh_first_unflushed_offset
; /* offset in the log of where to start replay after a crash */
51 struct journal_params jh_journal
;
52 uint32_t jh_last_check_mount_id
; /* the mount id of the fs during the last reiserfsck --check. */
55 struct reiserfs_super_block
{
56 uint32_t sb_block_count
; /* 0 number of block on data device */
57 uint32_t sb_free_blocks
; /* 4 free blocks count */
58 uint32_t sb_root_block
; /* 8 root of the tree */
60 struct journal_params sb_journal
; /* 12 */
62 uint16_t sb_blocksize
; /* 44 */
63 uint16_t sb_oid_maxsize
; /* 46 max size of object id array, see get_objectid() commentary */
64 uint16_t sb_oid_cursize
; /* 48 current size of object id array */
65 uint16_t sb_umount_state
; /* 50 this is set to 1 when filesystem was umounted, to 2 - when not */
67 char s_magic
[10]; /* 52 "ReIsErFs" or "ReIsEr2Fs" or "ReIsEr3Fs" */
68 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) */
69 uint32_t sb_hash_function_code
; /* 64 code of fuction which was/is/will be used to sort names in a directory. See codes in above */
70 uint16_t sb_tree_height
; /* 68 height of filesytem tree. Tree consisting of only one root block has 2 here */
71 uint16_t sb_bmap_nr
; /* 70 amount of bitmap blocks needed to address each block of file system */
72 uint16_t sb_version
; /* 72 this field is only reliable on filesystem with non-standard journal */
73 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 */
74 uint32_t sb_inode_generation
; /* 76 */
75 uint32_t sb_flags
; /* 80 Right now used only by inode-attributes, if enabled */
76 unsigned char s_uuid
[16]; /* 84 filesystem unique identifier */
77 unsigned char s_label
[16]; /* 100 filesystem volume label */
78 uint16_t sb_mnt_count
; /* 116 */
79 uint16_t sb_max_mnt_count
; /* 118 */
80 uint32_t sb_lastcheck
; /* 120 */
81 uint32_t sb_check_interval
; /* 124 */
82 /* zero filled by mkreiserfs and reiserfs_convert_objectid_map_v1() so any additions must be updated there as well. */
83 char s_unused
[76]; /* 128 */
87 /* Header of a disk block. More precisely, header of a formatted leaf
88 or internal node, and not the header of an unformatted node. */
90 uint16_t blk2_level
; /* Level of a block in the tree. */
91 uint16_t blk2_nr_item
; /* Number of keys/items in a block. */
92 uint16_t blk2_free_space
; /* Block free space in bytes. */
93 uint16_t blk_reserved
;
97 #define REISERFS_DISK_OFFSET_IN_BYTES (64 * 1024)
99 #define REISERFS_3_6_SUPER_MAGIC_STRING "ReIsEr2Fs"
100 #define REISERFS_FORMAT_3_6 2
101 #define DEFAULT_MAX_MNT_COUNT 30 /* 30 mounts */
102 #define DEFAULT_CHECK_INTERVAL (180 * 60 * 60 * 24) /* 180 days */
104 #define FS_CLEANLY_UMOUNTED 1 /* this was REISERFS_VALID_FS */
106 #define JOURNAL_MIN_SIZE 512
107 /* biggest possible single transaction, don't change for now (8/3/99) */
108 #define JOURNAL_TRANS_MAX 1024
109 #define JOURNAL_TRANS_MIN 256 /* need to check whether it works */
110 #define JOURNAL_DEFAULT_RATIO 8 /* default journal size / max trans length */
111 #define JOURNAL_MIN_RATIO 2
112 /* max blocks to batch into one transaction, don't make this any bigger than 900 */
113 #define JOURNAL_MAX_BATCH 900
114 #define JOURNAL_MAX_COMMIT_AGE 30
117 // Standard mkreiserfs 3.6.21:
118 // -b | --block-size N size of file-system block, in bytes
119 // -j | --journal-device FILE path to separate device to hold journal
120 // -s | --journal-size N size of the journal in blocks
121 // -o | --journal-offset N offset of the journal from the start of
122 // the separate device, in blocks
123 // -t | --transaction-max-size N maximal size of transaction, in blocks
124 // -B | --badblocks file store all bad blocks given in file on the fs
125 // -h | --hash rupasov|tea|r5 hash function to use by default
126 // -u | --uuid UUID store UUID in the superblock
127 // -l | --label LABEL store LABEL in the superblock
128 // --format 3.5|3.6 old 3.5 format or newer 3.6
129 // -f | --force specified once, make mkreiserfs the whole
130 // disk, not block device or mounted partition;
131 // specified twice, do not ask for confirmation
132 // -q | --quiet quiet work without messages, progress and
133 // questions. Useful if run in a script. For use
134 // by end users only.
135 // -d | --debug print debugging information during mkreiser
136 // -V print version and exit
138 // Options not commented below are taken but silently ignored:
148 OPT_l
= 1 << 8, // label
149 OPT_f
= 1 << 9, // ask no questions
152 //OPT_V = 1 << 12, // -V version. bbox applets don't support that
155 int mkfs_reiser_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
156 int mkfs_reiser_main(int argc UNUSED_PARAM
, char **argv
)
158 unsigned blocksize
= 4096;
159 unsigned journal_blocks
= 8192;
160 unsigned blocks
, bitmap_blocks
, i
, block
;
162 const char *label
= "";
166 struct reiserfs_super_block
*sb
;
167 struct journal_params
*jp
;
168 struct block_head
*root
;
170 // using global "option_mask32" instead of local "opts":
171 // we are register starved here
172 opt_complementary
= "-1:b+";
173 /*opts =*/ 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
; // argv[0] -- device
177 // check the device is a block device
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]);
183 // check if it is mounted
184 // N.B. what if we format a file? find_mount_point will return false negative since
185 // it is loop block device which is mounted!
186 if (find_mount_point(argv
[0], 0))
187 bb_error_msg_and_die("can't format mounted filesystem");
189 // open the device, get size in blocks
190 blocks
= get_volume_size_in_bytes(fd
, argv
[1], blocksize
, /*extend:*/ 1) / blocksize
;
192 // block number sanity check
193 // we have a limit: skipped area, super block, journal and root block
194 // all have to be addressed by one first bitmap
195 block
= REISERFS_DISK_OFFSET_IN_BYTES
/ blocksize
// boot area
198 + journal_blocks
+1 // journal
202 bitmap_blocks
= (blocks
- 1) / (blocksize
* 8) + 1;
203 i
= block
+ bitmap_blocks
;
206 if (MIN(blocksize
* 8, blocks
) < i
)
207 bb_error_msg_and_die("need >= %u blocks", i
);
212 // wipe out first REISERFS_DISK_OFFSET_IN_BYTES of device
213 // TODO: do we really need to wipe?!
214 xlseek(fd
, REISERFS_DISK_OFFSET_IN_BYTES
, SEEK_SET
);
217 sb
= (struct reiserfs_super_block
*)xzalloc(blocksize
);
219 STORE_LE(sb
->sb_block_count
, blocks
);
220 STORE_LE(sb
->sb_free_blocks
, blocks
- i
);
222 STORE_LE(sb
->sb_root_block
, block
);
223 // fill journal related fields
224 jp
= &sb
->sb_journal
;
225 STORE_LE(jp
->jp_journal_1st_block
, REISERFS_DISK_OFFSET_IN_BYTES
/ blocksize
+ 1/*sb*/ + 1/*bmp#0*/);
226 timestamp
= time(NULL
);
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
);
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); // "." and ".."
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
);
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); // R5_HASH
248 STORE_LE(sb
->sb_flags
, 1);
249 //STORE_LE(sb->sb_reserved_for_journal, 0);
251 generate_uuid(sb
->s_uuid
);
253 safe_strncpy((char *)sb
->s_label
, label
, sizeof(sb
->s_label
));
255 // TODO: EMPIRIC! ENDIANNESS!
256 // superblock has only 204 bytes. What are these?
262 xwrite(fd
, sb
, blocksize
);
265 buf
= xzalloc(blocksize
);
267 // bitmap #0 uses initial "block"+1 blocks
269 memset(buf
, 0xFF, i
/ 8);
270 buf
[i
/ 8] = (1 << (i
& 7)) - 1; //0..7 => 00000000..01111111
271 // mark trailing absent blocks, if any
272 if (blocks
< 8*blocksize
) {
273 unsigned n
= 8*blocksize
- blocks
;
275 buf
[blocksize
- i
- 1] |= 0x7F00 >> (n
& 7); //0..7 => 00000000..11111110
276 memset(buf
+ blocksize
- i
, 0xFF, i
); // N.B. no overflow here!
279 xwrite(fd
, buf
, blocksize
);
281 // now go journal blocks
282 memset(buf
, 0, blocksize
);
283 for (i
= 0; i
< journal_blocks
; i
++)
284 xwrite(fd
, buf
, blocksize
);
285 // dump journal control block
286 memcpy(&((struct reiserfs_journal_header
*)buf
)->jh_journal
, &sb
->sb_journal
, sizeof(sb
->sb_journal
));
287 xwrite(fd
, buf
, blocksize
);
289 // other bitmaps are in every (8*blocksize)-th block
290 // N.B. they use the only block -- namely bitmap itself!
293 for (i
= 1; i
< bitmap_blocks
; i
++) {
294 xlseek(fd
, i
*8*blocksize
* blocksize
, SEEK_SET
);
295 // mark trailing absent blocks, if any
296 if (i
== bitmap_blocks
- 1 && (blocks
% (8*blocksize
))) {
297 unsigned n
= 8*blocksize
- blocks
% (8*blocksize
);
299 buf
[blocksize
- j
- 1] |= 0x7F00 >> (n
& 7); //0..7 => 00000000..11111110
300 memset(buf
+ blocksize
- j
, 0xFF, j
); // N.B. no overflow here!
302 xwrite(fd
, buf
, blocksize
);
307 memset(buf
, 0, blocksize
);
308 root
= (struct block_head
*)buf
;
309 STORE_LE(root
->blk2_level
, 1); // leaf node
310 STORE_LE(root
->blk2_nr_item
, 2); // "." and ".."
311 STORE_LE(root
->blk2_free_space
, blocksize
- sizeof(struct block_head
));
314 // TODO: EMPIRIC! ENDIANNESS!
315 // TODO: indented assignments seem to be timestamps
342 buf
[4036] = 056; buf
[4037] = 056; // ".."
343 buf
[4044] = 056; // "."
364 xlseek(fd
, block
* blocksize
, SEEK_SET
);
365 xwrite(fd
, buf
, blocksize
);
368 if (ENABLE_FEATURE_CLEAN_UP
) {