123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385 |
- #include "libbb.h"
- #include <linux/fs.h>
- char BUG_wrong_field_size(void);
- #define STORE_LE(field, value) \
- do { \
- if (sizeof(field) == 4) \
- field = SWAP_LE32(value); \
- else if (sizeof(field) == 2) \
- field = SWAP_LE16(value); \
- else if (sizeof(field) == 1) \
- field = (value); \
- else \
- BUG_wrong_field_size(); \
- } while (0)
- #define FETCH_LE32(field) \
- (sizeof(field) == 4 ? SWAP_LE32(field) : BUG_wrong_field_size())
- struct journal_params {
- uint32_t jp_journal_1st_block;
- uint32_t jp_journal_dev;
- uint32_t jp_journal_size;
- uint32_t jp_journal_trans_max;
- uint32_t jp_journal_magic;
- uint32_t jp_journal_max_batch;
- uint32_t jp_journal_max_commit_age;
- uint32_t jp_journal_max_trans_age;
- };
- struct reiserfs_journal_header {
- uint32_t jh_last_flush_trans_id;
- uint32_t jh_first_unflushed_offset;
- uint32_t jh_mount_id;
- struct journal_params jh_journal;
- uint32_t jh_last_check_mount_id;
- };
- struct reiserfs_super_block {
- uint32_t sb_block_count;
- uint32_t sb_free_blocks;
- uint32_t sb_root_block;
- struct journal_params sb_journal;
- uint16_t sb_blocksize;
- uint16_t sb_oid_maxsize;
- uint16_t sb_oid_cursize;
- uint16_t sb_umount_state;
- char s_magic[10];
- uint16_t sb_fs_state;
- uint32_t sb_hash_function_code;
- uint16_t sb_tree_height;
- uint16_t sb_bmap_nr;
- uint16_t sb_version;
- uint16_t sb_reserved_for_journal;
- uint32_t sb_inode_generation;
- uint32_t sb_flags;
- unsigned char s_uuid[16];
- unsigned char s_label[16];
- uint16_t sb_mnt_count;
- uint16_t sb_max_mnt_count;
- uint32_t sb_lastcheck;
- uint32_t sb_check_interval;
- char s_unused[76];
-
- };
- struct block_head {
- uint16_t blk2_level;
- uint16_t blk2_nr_item;
- uint16_t blk2_free_space;
- uint16_t blk_reserved;
- uint32_t reserved[4];
- };
- #define REISERFS_DISK_OFFSET_IN_BYTES (64 * 1024)
- #define REISERFS_3_6_SUPER_MAGIC_STRING "ReIsEr2Fs"
- #define REISERFS_FORMAT_3_6 2
- #define DEFAULT_MAX_MNT_COUNT 30
- #define DEFAULT_CHECK_INTERVAL (180 * 60 * 60 * 24)
- #define FS_CLEANLY_UMOUNTED 1
- #define JOURNAL_MIN_SIZE 512
- #define JOURNAL_TRANS_MAX 1024
- #define JOURNAL_TRANS_MIN 256
- #define JOURNAL_DEFAULT_RATIO 8
- #define JOURNAL_MIN_RATIO 2
- #define JOURNAL_MAX_BATCH 900
- #define JOURNAL_MAX_COMMIT_AGE 30
- enum {
- OPT_b = 1 << 0,
- OPT_j = 1 << 1,
- OPT_s = 1 << 2,
- OPT_o = 1 << 3,
- OPT_t = 1 << 4,
- OPT_B = 1 << 5,
- OPT_h = 1 << 6,
- OPT_u = 1 << 7,
- OPT_l = 1 << 8,
- OPT_f = 1 << 9,
- OPT_q = 1 << 10,
- OPT_d = 1 << 11,
-
- };
- int mkfs_reiser_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int mkfs_reiser_main(int argc UNUSED_PARAM, char **argv)
- {
- unsigned blocksize = 4096;
- unsigned journal_blocks = 8192;
- unsigned blocks, bitmap_blocks, i, block;
- time_t timestamp;
- const char *label = "";
- struct stat st;
- int fd;
- uint8_t *buf;
- struct reiserfs_super_block *sb;
- struct journal_params *jp;
- struct block_head *root;
-
-
- getopt32(argv, "^" "b:+j:s:o:t:B:h:u:l:fqd" "\0" "-1",
- NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &label);
- argv += optind;
-
- fd = xopen(argv[0], O_WRONLY | O_EXCL);
- xfstat(fd, &st, argv[0]);
- if (!S_ISBLK(st.st_mode) && !(option_mask32 & OPT_f))
- bb_error_msg_and_die("%s: not a block device", argv[0]);
-
-
-
- if (find_mount_point(argv[0], 0))
- bb_error_msg_and_die("can't format mounted filesystem");
-
- blocks = get_volume_size_in_bytes(fd, argv[1], blocksize, 1) / blocksize;
-
-
-
- block = REISERFS_DISK_OFFSET_IN_BYTES / blocksize
- + 1
- + 1
- + journal_blocks+1
- ;
-
- bitmap_blocks = (blocks - 1) / (blocksize * 8) + 1;
- i = block + bitmap_blocks;
-
- if (MIN(blocksize * 8, blocks) < i)
- bb_error_msg_and_die("need >= %u blocks", i);
-
-
-
-
- xlseek(fd, REISERFS_DISK_OFFSET_IN_BYTES, SEEK_SET);
-
- sb = (struct reiserfs_super_block *)xzalloc(blocksize);
-
- STORE_LE(sb->sb_block_count, blocks);
- STORE_LE(sb->sb_free_blocks, blocks - i);
-
- STORE_LE(sb->sb_root_block, block);
-
- jp = &sb->sb_journal;
- STORE_LE(jp->jp_journal_1st_block, REISERFS_DISK_OFFSET_IN_BYTES / blocksize + 1 + 1);
- timestamp = time(NULL);
- srand(timestamp);
- STORE_LE(jp->jp_journal_magic, rand());
- STORE_LE(jp->jp_journal_size, journal_blocks);
- STORE_LE(jp->jp_journal_trans_max, JOURNAL_TRANS_MAX);
- STORE_LE(jp->jp_journal_max_batch, JOURNAL_MAX_BATCH);
- STORE_LE(jp->jp_journal_max_commit_age, JOURNAL_MAX_COMMIT_AGE);
-
- STORE_LE(sb->sb_blocksize, blocksize);
- STORE_LE(sb->sb_oid_maxsize, (blocksize - sizeof(*sb)) / sizeof(uint32_t) / 2 * 2);
- STORE_LE(sb->sb_oid_cursize, 2);
- strcpy(sb->s_magic, REISERFS_3_6_SUPER_MAGIC_STRING);
- STORE_LE(sb->sb_bmap_nr, (bitmap_blocks > ((1LL << 16) - 1)) ? 0 : bitmap_blocks);
-
- STORE_LE(sb->sb_version, REISERFS_FORMAT_3_6);
- STORE_LE(sb->sb_lastcheck, timestamp);
- STORE_LE(sb->sb_check_interval, DEFAULT_CHECK_INTERVAL);
- STORE_LE(sb->sb_mnt_count, 1);
- STORE_LE(sb->sb_max_mnt_count, DEFAULT_MAX_MNT_COUNT);
- STORE_LE(sb->sb_umount_state, FS_CLEANLY_UMOUNTED);
- STORE_LE(sb->sb_tree_height, 2);
- STORE_LE(sb->sb_hash_function_code, 3);
- STORE_LE(sb->sb_flags, 1);
-
-
- generate_uuid(sb->s_uuid);
-
- safe_strncpy((char *)sb->s_label, label, sizeof(sb->s_label));
-
-
- buf = (uint8_t *)sb;
- buf[205] = 1;
- buf[209] = 3;
-
- xwrite(fd, sb, blocksize);
-
- buf = xzalloc(blocksize);
-
- i = block + 1;
- memset(buf, 0xFF, i / 8);
- buf[i / 8] = (1 << (i & 7)) - 1;
-
- if (blocks < 8*blocksize) {
- unsigned n = 8*blocksize - blocks;
- i = n / 8;
- buf[blocksize - i - 1] |= 0x7F00 >> (n & 7);
- memset(buf + blocksize - i, 0xFF, i);
- }
-
- xwrite(fd, buf, blocksize);
-
- memset(buf, 0, blocksize);
- for (i = 0; i < journal_blocks; i++)
- xwrite(fd, buf, blocksize);
-
- memcpy(&((struct reiserfs_journal_header *)buf)->jh_journal, &sb->sb_journal, sizeof(sb->sb_journal));
- xwrite(fd, buf, blocksize);
-
-
- buf[0] = 0x01;
-
- for (i = 1; i < bitmap_blocks; i++) {
- xlseek(fd, i*8*blocksize * blocksize, SEEK_SET);
-
- if (i == bitmap_blocks - 1 && (blocks % (8*blocksize))) {
- unsigned n = 8*blocksize - blocks % (8*blocksize);
- unsigned j = n / 8;
- buf[blocksize - j - 1] |= 0x7F00 >> (n & 7);
- memset(buf + blocksize - j, 0xFF, j);
- }
- xwrite(fd, buf, blocksize);
- }
-
-
- memset(buf, 0, blocksize);
- root = (struct block_head *)buf;
- STORE_LE(root->blk2_level, 1);
- STORE_LE(root->blk2_nr_item, 2);
- STORE_LE(root->blk2_free_space, blocksize - sizeof(struct block_head));
-
-
-
-
- buf[4] = 0134;
- buf[24] = 01;
- buf[28] = 02;
- buf[42] = 054;
- buf[44] = 0324;
- buf[45] = 017;
- buf[46] = 01;
- buf[48] = 01;
- buf[52] = 02;
- buf[56] = 01;
- buf[60] = 0364;
- buf[61] = 01;
- buf[64] = 02;
- buf[66] = 060;
- buf[68] = 0244;
- buf[69] = 017;
- buf[4004] = 01;
- buf[4008] = 01;
- buf[4012] = 02;
- buf[4016] = 050;
- buf[4018] = 04;
- buf[4020] = 02;
- buf[4028] = 01;
- buf[4032] = 040;
- buf[4034] = 04;
- buf[4036] = 056; buf[4037] = 056;
- buf[4044] = 056;
- buf[4052] = 0355;
- buf[4053] = 0101;
- buf[4056] = 03;
- buf[4060] = 060;
- buf[4076] = 0173;
- buf[4077] = 0240;
- buf[4078] = 0344;
- buf[4079] = 0112;
- buf[4080] = 0173;
- buf[4081] = 0240;
- buf[4082] = 0344;
- buf[4083] = 0112;
- buf[4084] = 0173;
- buf[4085] = 0240;
- buf[4086] = 0344;
- buf[4087] = 0112;
- buf[4088] = 01;
-
- xlseek(fd, block * blocksize, SEEK_SET);
- xwrite(fd, buf, blocksize);
-
- if (ENABLE_FEATURE_CLEAN_UP) {
- free(buf);
- free(sb);
- }
- xclose(fd);
- return EXIT_SUCCESS;
- }
|