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