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