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