3
0

mkfs_reiser.c 13 KB

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