mke2fs.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. /* mke2fs.c - Create an ext2 filesystem image.
  2. *
  3. * Copyright 2006, 2007 Rob Landley <rob@landley.net>
  4. // Still to go: "E:jJ:L:m:O:"
  5. USE_MKE2FS(NEWTOY(mke2fs, "<1>2g:Fnqm#N#i#b#", TOYFLAG_SBIN))
  6. config MKE2FS
  7. bool "mke2fs"
  8. default n
  9. help
  10. usage: mke2fs [-Fnq] [-b ###] [-N|i ###] [-m ###] device
  11. Create an ext2 filesystem on a block device or filesystem image.
  12. -F Force to run on a mounted device
  13. -n Don't write to device
  14. -q Quiet (no output)
  15. -b size Block size (1024, 2048, or 4096)
  16. -N inodes Allocate this many inodes
  17. -i bytes Allocate one inode for every XXX bytes of device
  18. -m percent Reserve this percent of filesystem space for root user
  19. config MKE2FS_JOURNAL
  20. bool "Journaling support (ext3)"
  21. default n
  22. depends on MKE2FS
  23. help
  24. usage: mke2fs [-j] [-J size=###,device=XXX]
  25. -j Create journal (ext3)
  26. -J Journal options
  27. size: Number of blocks (1024-102400)
  28. device: Specify an external journal
  29. config MKE2FS_GEN
  30. bool "Generate (gene2fs)"
  31. default n
  32. depends on MKE2FS
  33. help
  34. usage: gene2fs [options] device filename
  35. The [options] are the same as mke2fs.
  36. config MKE2FS_LABEL
  37. bool "Label support"
  38. default n
  39. depends on MKE2FS
  40. help
  41. usage: mke2fs [-L label] [-M path] [-o string]
  42. -L Volume label
  43. -M Path to mount point
  44. -o Created by
  45. config MKE2FS_EXTENDED
  46. bool "Extended options"
  47. default n
  48. depends on MKE2FS
  49. help
  50. usage: mke2fs [-E stride=###] [-O option[,option]]
  51. -E stride= Set RAID stripe size (in blocks)
  52. -O [opts] Specify fewer ext2 option flags (for old kernels)
  53. All of these are on by default (as appropriate)
  54. none Clear default options (all but journaling)
  55. dir_index Use htree indexes for large directories
  56. filetype Store file type info in directory entry
  57. has_journal Set by -j
  58. journal_dev Set by -J device=XXX
  59. sparse_super Don't allocate huge numbers of redundant superblocks
  60. */
  61. #define FOR_mke2fs
  62. #include "toys.h"
  63. GLOBALS(
  64. // Command line arguments.
  65. long blocksize;
  66. long bytes_per_inode;
  67. long inodes; // Total inodes in filesystem.
  68. long reserved_percent; // Integer precent of space to reserve for root.
  69. char *gendir; // Where to read dirtree from.
  70. // Internal data.
  71. struct dirtree *dt; // Tree of files to copy into the new filesystem.
  72. unsigned treeblocks; // Blocks used by dt
  73. unsigned treeinodes; // Inodes used by dt
  74. unsigned blocks; // Total blocks in the filesystem.
  75. unsigned freeblocks; // Free blocks in the filesystem.
  76. unsigned inodespg; // Inodes per group
  77. unsigned groups; // Total number of block groups.
  78. unsigned blockbits; // Bits per block. (Also blocks per group.)
  79. // For gene2fs
  80. unsigned nextblock; // Next data block to allocate
  81. unsigned nextgroup; // Next group we'll be allocating from
  82. int fsfd; // File descriptor of filesystem (to output to).
  83. )
  84. // Stuff defined in linux/ext2_fs.h
  85. #define EXT2_SUPER_MAGIC 0xEF53
  86. struct ext2_superblock {
  87. uint32_t inodes_count; // Inodes count
  88. uint32_t blocks_count; // Blocks count
  89. uint32_t r_blocks_count; // Reserved blocks count
  90. uint32_t free_blocks_count; // Free blocks count
  91. uint32_t free_inodes_count; // Free inodes count
  92. uint32_t first_data_block; // First Data Block
  93. uint32_t log_block_size; // Block size
  94. uint32_t log_frag_size; // Fragment size
  95. uint32_t blocks_per_group; // Blocks per group
  96. uint32_t frags_per_group; // Fragments per group
  97. uint32_t inodes_per_group; // Inodes per group
  98. uint32_t mtime; // Mount time
  99. uint32_t wtime; // Write time
  100. uint16_t mnt_count; // Mount count
  101. uint16_t max_mnt_count; // Maximal mount count
  102. uint16_t magic; // Magic signature
  103. uint16_t state; // File system state
  104. uint16_t errors; // Behaviour when detecting errors
  105. uint16_t minor_rev_level; // minor revision level
  106. uint32_t lastcheck; // time of last check
  107. uint32_t checkinterval; // max. time between checks
  108. uint32_t creator_os; // OS
  109. uint32_t rev_level; // Revision level
  110. uint16_t def_resuid; // Default uid for reserved blocks
  111. uint16_t def_resgid; // Default gid for reserved blocks
  112. uint32_t first_ino; // First non-reserved inode
  113. uint16_t inode_size; // size of inode structure
  114. uint16_t block_group_nr; // block group # of this superblock
  115. uint32_t feature_compat; // compatible feature set
  116. uint32_t feature_incompat; // incompatible feature set
  117. uint32_t feature_ro_compat; // readonly-compatible feature set
  118. char uuid[16]; // 128-bit uuid for volume
  119. char volume_name[16]; // volume name
  120. char last_mounted[64]; // directory where last mounted
  121. uint32_t alg_usage_bitmap; // For compression
  122. // For EXT2_COMPAT_PREALLOC
  123. uint8_t prealloc_blocks; // Nr of blocks to try to preallocate
  124. uint8_t prealloc_dir_blocks; //Nr to preallocate for dirs
  125. uint16_t padding1;
  126. // For EXT3_FEATURE_COMPAT_HAS_JOURNAL
  127. uint8_t journal_uuid[16]; // uuid of journal superblock
  128. uint32_t journal_inum; // inode number of journal file
  129. uint32_t journal_dev; // device number of journal file
  130. uint32_t last_orphan; // start of list of inodes to delete
  131. uint32_t hash_seed[4]; // HTREE hash seed
  132. uint8_t def_hash_version; // Default hash version to use
  133. uint8_t padding2[3];
  134. uint32_t default_mount_opts;
  135. uint32_t first_meta_bg; // First metablock block group
  136. uint32_t mkfs_time; // Creation timestamp
  137. uint32_t jnl_blocks[17]; // Backup of journal inode
  138. // uint32_t reserved[172]; // Padding to the end of the block
  139. };
  140. struct ext2_group
  141. {
  142. uint32_t block_bitmap; // Block number of block bitmap
  143. uint32_t inode_bitmap; // Block number of inode bitmap
  144. uint32_t inode_table; // Block number of inode table
  145. uint16_t free_blocks_count; // How many free blocks in this group?
  146. uint16_t free_inodes_count; // How many free inodes in this group?
  147. uint16_t used_dirs_count; // How many directories?
  148. uint16_t reserved[7]; // pad to 32 bytes
  149. };
  150. struct ext2_dentry {
  151. uint32_t inode; // Inode number
  152. uint16_t rec_len; // Directory entry length
  153. uint8_t name_len; // Name length
  154. uint8_t file_type;
  155. char name[0]; // File name
  156. };
  157. struct ext2_inode {
  158. uint16_t mode; // File mode
  159. uint16_t uid; // Low 16 bits of Owner Uid
  160. uint32_t size; // Size in bytes
  161. uint32_t atime; // Access time
  162. uint32_t ctime; // Creation time
  163. uint32_t mtime; // Modification time
  164. uint32_t dtime; // Deletion Time
  165. uint16_t gid; // Low 16 bits of Group Id
  166. uint16_t links_count; // Links count
  167. uint32_t blocks; // Blocks count
  168. uint32_t flags; // File flags
  169. uint32_t reserved1;
  170. uint32_t block[15]; // Pointers to blocks
  171. uint32_t generation; // File version (for NFS)
  172. uint32_t file_acl; // File ACL
  173. uint32_t dir_acl; // Directory ACL (or top bits of file length)
  174. uint32_t faddr; // Last block in file
  175. uint8_t frag; // Fragment number
  176. uint8_t fsize; // Fragment size
  177. uint16_t pad1;
  178. uint16_t uid_high; // High bits of uid
  179. uint16_t gid_high; // High bits of gid
  180. uint32_t reserved2;
  181. };
  182. #define EXT2_FEATURE_COMPAT_DIR_PREALLOC 0x0001
  183. #define EXT2_FEATURE_COMPAT_IMAGIC_INODES 0x0002
  184. #define EXT3_FEATURE_COMPAT_HAS_JOURNAL 0x0004
  185. #define EXT2_FEATURE_COMPAT_EXT_ATTR 0x0008
  186. #define EXT2_FEATURE_COMPAT_RESIZE_INO 0x0010
  187. #define EXT2_FEATURE_COMPAT_DIR_INDEX 0x0020
  188. #define EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER 0x0001
  189. #define EXT2_FEATURE_RO_COMPAT_LARGE_FILE 0x0002
  190. #define EXT2_FEATURE_RO_COMPAT_BTREE_DIR 0x0004
  191. #define EXT2_FEATURE_INCOMPAT_COMPRESSION 0x0001
  192. #define EXT2_FEATURE_INCOMPAT_FILETYPE 0x0002
  193. #define EXT3_FEATURE_INCOMPAT_RECOVER 0x0004
  194. #define EXT3_FEATURE_INCOMPAT_JOURNAL_DEV 0x0008
  195. #define EXT2_FEATURE_INCOMPAT_META_BG 0x0010
  196. #define EXT2_NAME_LEN 255
  197. // Ext2 directory file types. Only the low 3 bits are used. The
  198. // other bits are reserved for now.
  199. enum {
  200. EXT2_FT_UNKNOWN,
  201. EXT2_FT_REG_FILE,
  202. EXT2_FT_DIR,
  203. EXT2_FT_CHRDEV,
  204. EXT2_FT_BLKDEV,
  205. EXT2_FT_FIFO,
  206. EXT2_FT_SOCK,
  207. EXT2_FT_SYMLINK,
  208. EXT2_FT_MAX
  209. };
  210. #define INODES_RESERVED 10
  211. static uint32_t div_round_up(uint32_t a, uint32_t b)
  212. {
  213. uint32_t c = a/b;
  214. if (a%b) c++;
  215. return c;
  216. }
  217. // Calculate data blocks plus index blocks needed to hold a file.
  218. static uint32_t file_blocks_used(uint64_t size, uint32_t *blocklist)
  219. {
  220. uint32_t dblocks = (uint32_t)((size+(TT.blocksize-1))/TT.blocksize);
  221. uint32_t idx=TT.blocksize/4, iblocks=0, diblocks=0, tiblocks=0;
  222. // Fill out index blocks in inode.
  223. if (blocklist) {
  224. int i;
  225. // Direct index blocks
  226. for (i=0; i<13 && i<dblocks; i++) blocklist[i] = i;
  227. // Singly indirect index blocks
  228. if (dblocks > 13+idx) blocklist[13] = 13+idx;
  229. // Doubly indirect index blocks
  230. idx = 13 + idx + (idx*idx);
  231. if (dblocks > idx) blocklist[14] = idx;
  232. return 0;
  233. }
  234. // Account for direct, singly, doubly, and triply indirect index blocks
  235. if (dblocks > 12) {
  236. iblocks = ((dblocks-13)/idx)+1;
  237. if (iblocks > 1) {
  238. diblocks = ((iblocks-2)/idx)+1;
  239. if (diblocks > 1)
  240. tiblocks = ((diblocks-2)/idx)+1;
  241. }
  242. }
  243. return dblocks + iblocks + diblocks + tiblocks;
  244. }
  245. // Use the parent pointer to iterate through the tree non-recursively.
  246. static struct dirtree *treenext(struct dirtree *this)
  247. {
  248. while (this && !this->next) this = this->parent;
  249. if (this) this = this->next;
  250. return this;
  251. }
  252. // Recursively calculate the number of blocks used by each inode in the tree.
  253. // Returns blocks used by this directory, assigns bytes used to *size.
  254. // Writes total block count to TT.treeblocks and inode count to TT.treeinodes.
  255. static long check_treesize(struct dirtree *that, off_t *size)
  256. {
  257. long blocks;
  258. while (that) {
  259. *size += sizeof(struct ext2_dentry) + strlen(that->name);
  260. if (that->child)
  261. that->st.st_blocks = check_treesize(that->child, &that->st.st_size);
  262. else if (S_ISREG(that->st.st_mode)) {
  263. that->st.st_blocks = file_blocks_used(that->st.st_size, 0);
  264. TT.treeblocks += that->st.st_blocks;
  265. }
  266. that = that->next;
  267. }
  268. TT.treeblocks += blocks = file_blocks_used(*size, 0);
  269. TT.treeinodes++;
  270. return blocks;
  271. }
  272. // Calculate inode numbers and link counts.
  273. //
  274. // To do this right I need to copy the tree and sort it, but here's a really
  275. // ugly n^2 way of dealing with the problem that doesn't scale well to large
  276. // numbers of files (> 100,000) but can be done in very little code.
  277. // This rewrites inode numbers to their final values, allocating depth first.
  278. static void check_treelinks(struct dirtree *tree)
  279. {
  280. struct dirtree *current=tree, *that;
  281. long inode = INODES_RESERVED;
  282. while (current) {
  283. ++inode;
  284. // Since we can't hardlink to directories, we know their link count.
  285. if (S_ISDIR(current->st.st_mode)) current->st.st_nlink = 2;
  286. else {
  287. dev_t new = current->st.st_dev;
  288. if (!new) continue;
  289. // Look for other copies of current node
  290. current->st.st_nlink = 0;
  291. for (that = tree; that; that = treenext(that)) {
  292. if (current->st.st_ino == that->st.st_ino &&
  293. current->st.st_dev == that->st.st_dev)
  294. {
  295. current->st.st_nlink++;
  296. current->st.st_ino = inode;
  297. }
  298. }
  299. }
  300. current->st.st_ino = inode;
  301. current = treenext(current);
  302. }
  303. }
  304. // Calculate inodes per group from total inodes.
  305. static uint32_t get_inodespg(uint32_t inodes)
  306. {
  307. uint32_t temp;
  308. // Round up to fill complete inode blocks.
  309. temp = (inodes + TT.groups - 1) / TT.groups;
  310. inodes = TT.blocksize/sizeof(struct ext2_inode);
  311. return ((temp + inodes - 1)/inodes)*inodes;
  312. }
  313. // Fill out superblock and TT structures.
  314. static void init_superblock(struct ext2_superblock *sb)
  315. {
  316. uint32_t temp;
  317. // Set log_block_size and log_frag_size.
  318. for (temp = 0; temp < 4; temp++) if (TT.blocksize == 1024<<temp) break;
  319. if (temp==4) error_exit("bad blocksize");
  320. sb->log_block_size = sb->log_frag_size = SWAP_LE32(temp);
  321. // Fill out blocks_count, r_blocks_count, first_data_block
  322. sb->blocks_count = SWAP_LE32(TT.blocks);
  323. sb->free_blocks_count = SWAP_LE32(TT.freeblocks);
  324. temp = (TT.blocks * (uint64_t)TT.reserved_percent) / 100;
  325. sb->r_blocks_count = SWAP_LE32(temp);
  326. sb->first_data_block = SWAP_LE32(TT.blocksize == 1024 ? 1 : 0);
  327. // Set blocks_per_group and frags_per_group, which is the size of an
  328. // allocation bitmap that fits in one block (I.E. how many bits per block)?
  329. sb->blocks_per_group = sb->frags_per_group = SWAP_LE32(TT.blockbits);
  330. // Set inodes_per_group and total inodes_count
  331. sb->inodes_per_group = SWAP_LE32(TT.inodespg);
  332. sb->inodes_count = SWAP_LE32(TT.inodespg * TT.groups);
  333. // Determine free inodes.
  334. temp = TT.inodespg*TT.groups - INODES_RESERVED;
  335. if (temp < TT.treeinodes) error_exit("Not enough inodes.\n");
  336. sb->free_inodes_count = SWAP_LE32(temp - TT.treeinodes);
  337. // Fill out the rest of the superblock.
  338. sb->max_mnt_count=0xFFFF;
  339. sb->wtime = sb->lastcheck = sb->mkfs_time = SWAP_LE32(time(NULL));
  340. sb->magic = SWAP_LE32(0xEF53);
  341. sb->state = sb->errors = SWAP_LE16(1);
  342. sb->rev_level = SWAP_LE32(1);
  343. sb->first_ino = SWAP_LE32(INODES_RESERVED+1);
  344. sb->inode_size = SWAP_LE16(sizeof(struct ext2_inode));
  345. sb->feature_incompat = SWAP_LE32(EXT2_FEATURE_INCOMPAT_FILETYPE);
  346. sb->feature_ro_compat = SWAP_LE32(EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER);
  347. create_uuid(sb->uuid);
  348. // TODO If we're called as mke3fs or mkfs.ext3, do a journal.
  349. //if (strchr(toys.which->name,'3'))
  350. // sb->feature_compat |= SWAP_LE32(EXT3_FEATURE_COMPAT_HAS_JOURNAL);
  351. }
  352. // Does this group contain a superblock backup (and group descriptor table)?
  353. static int is_sb_group(uint32_t group)
  354. {
  355. int i;
  356. // Superblock backups are on groups 0, 1, and powers of 3, 5, and 7.
  357. if(!group || group==1) return 1;
  358. for (i=3; i<9; i+=2) {
  359. int j = i;
  360. while (j<group) j*=i;
  361. if (j==group) return 1;
  362. }
  363. return 0;
  364. }
  365. // Number of blocks used in group by optional superblock/group list backup.
  366. static int group_superblock_overhead(uint32_t group)
  367. {
  368. int used;
  369. if (!is_sb_group(group)) return 0;
  370. // How many blocks does the group descriptor table take up?
  371. used = TT.groups * sizeof(struct ext2_group);
  372. used += TT.blocksize - 1;
  373. used /= TT.blocksize;
  374. // Plus the superblock itself.
  375. used++;
  376. // And a corner case.
  377. if (!group && TT.blocksize == 1024) used++;
  378. return used;
  379. }
  380. // Number of blocks used in group to store superblock/group/inode list
  381. static int group_overhead(uint32_t group)
  382. {
  383. // Return superblock backup overhead (if any), plus block/inode
  384. // allocation bitmaps, plus inode tables.
  385. return group_superblock_overhead(group) + 2 + get_inodespg(TT.inodespg)
  386. / (TT.blocksize/sizeof(struct ext2_inode));
  387. }
  388. // In bitmap "array" set "len" bits starting at position "start" (from 0).
  389. static void bits_set(char *array, int start, int len)
  390. {
  391. while(len) {
  392. if ((start&7) || len<8) {
  393. array[start/8]|=(1<<(start&7));
  394. start++;
  395. len--;
  396. } else {
  397. array[start/8]=255;
  398. start+=8;
  399. len-=8;
  400. }
  401. }
  402. }
  403. // Seek past len bytes (to maintain sparse file), or write zeroes if output
  404. // not seekable
  405. static void put_zeroes(int len)
  406. {
  407. if(-1 == lseek(TT.fsfd, len, SEEK_SET)) {
  408. memset(toybuf, 0, sizeof(toybuf));
  409. while (len) {
  410. int out = len > sizeof(toybuf) ? sizeof(toybuf) : len;
  411. xwrite(TT.fsfd, toybuf, out);
  412. len -= out;
  413. }
  414. }
  415. }
  416. // Fill out an inode structure from struct stat info in dirtree.
  417. static void fill_inode(struct ext2_inode *in, struct dirtree *that)
  418. {
  419. uint32_t fbu[15];
  420. int temp;
  421. file_blocks_used(that->st.st_size, fbu);
  422. // If that inode needs data blocks allocated to it.
  423. if (that->st.st_size) {
  424. int i, group = TT.nextblock/TT.blockbits;
  425. // TODO: teach this about indirect blocks.
  426. for (i=0; i<15; i++) {
  427. // If we just jumped into a new group, skip group overhead blocks.
  428. while (group >= TT.nextgroup)
  429. TT.nextblock += group_overhead(TT.nextgroup++);
  430. }
  431. }
  432. // TODO : S_ISREG/DIR/CHR/BLK/FIFO/LNK/SOCK(m)
  433. in->mode = SWAP_LE32(that->st.st_mode);
  434. in->uid = SWAP_LE16(that->st.st_uid & 0xFFFF);
  435. in->uid_high = SWAP_LE16(that->st.st_uid >> 16);
  436. in->gid = SWAP_LE16(that->st.st_gid & 0xFFFF);
  437. in->gid_high = SWAP_LE16(that->st.st_gid >> 16);
  438. in->size = SWAP_LE32(that->st.st_size & 0xFFFFFFFF);
  439. // Contortions to make the compiler not generate a warning for x>>32
  440. // when x is 32 bits. The optimizer should clean this up.
  441. if (sizeof(that->st.st_size) > 4) temp = 32;
  442. else temp = 0;
  443. if (temp) in->dir_acl = SWAP_LE32(that->st.st_size >> temp);
  444. in->atime = SWAP_LE32(that->st.st_atime);
  445. in->ctime = SWAP_LE32(that->st.st_ctime);
  446. in->mtime = SWAP_LE32(that->st.st_mtime);
  447. in->links_count = SWAP_LE16(that->st.st_nlink);
  448. in->blocks = SWAP_LE32(that->st.st_blocks);
  449. // in->faddr
  450. }
  451. // Works like an archiver.
  452. // The first argument is the name of the file to create. If it already
  453. // exists, that size will be used.
  454. void mke2fs_main(void)
  455. {
  456. int i, temp;
  457. off_t length;
  458. uint32_t usedblocks, usedinodes, dtbblk;
  459. struct dirtree *dti, *dtb;
  460. struct ext2_superblock sb;
  461. // Handle command line arguments.
  462. if (toys.optargs[1]) {
  463. sscanf(toys.optargs[1], "%u", &TT.blocks);
  464. temp = O_RDWR|O_CREAT;
  465. } else temp = O_RDWR;
  466. if (!TT.reserved_percent) TT.reserved_percent = 5;
  467. // TODO: Check if filesystem is mounted here
  468. // For mke?fs, open file. For gene?fs, create file.
  469. TT.fsfd = xcreate(*toys.optargs, temp, 0777);
  470. // Determine appropriate block size and block count from file length.
  471. // (If no length, default to 4k. They can override it on the cmdline.)
  472. length = fdlength(TT.fsfd);
  473. if (!TT.blocksize) TT.blocksize = (length && length < 1<<29) ? 1024 : 4096;
  474. TT.blockbits = 8*TT.blocksize;
  475. if (!TT.blocks) TT.blocks = length/TT.blocksize;
  476. // Collect gene2fs list or lost+found, calculate requirements.
  477. if (TT.gendir) {
  478. strncpy(toybuf, TT.gendir, sizeof(toybuf));
  479. dti = dirtree_read(toybuf, dirtree_notdotdot);
  480. } else {
  481. dti = xzalloc(sizeof(struct dirtree)+11);
  482. strcpy(dti->name, "lost+found");
  483. dti->st.st_mode = S_IFDIR|0755;
  484. dti->st.st_ctime = dti->st.st_mtime = time(NULL);
  485. }
  486. // Add root directory inode. This is iterated through for when finding
  487. // blocks, but not when finding inodes. The tree's parent pointers don't
  488. // point back into this.
  489. dtb = xzalloc(sizeof(struct dirtree)+1);
  490. dtb->st.st_mode = S_IFDIR|0755;
  491. dtb->st.st_ctime = dtb->st.st_mtime = time(NULL);
  492. dtb->child = dti;
  493. // Figure out how much space is used by preset files
  494. length = check_treesize(dtb, &(dtb->st.st_size));
  495. check_treelinks(dtb);
  496. // Figure out how many total inodes we need.
  497. if (!TT.inodes) {
  498. if (!TT.bytes_per_inode) TT.bytes_per_inode = 8192;
  499. TT.inodes = (TT.blocks * (uint64_t)TT.blocksize) / TT.bytes_per_inode;
  500. }
  501. // If we're generating a filesystem and have no idea how many blocks it
  502. // needs, start with a minimal guess, find the overhead of that many
  503. // groups, and loop until this is enough groups to store this many blocks.
  504. if (!TT.blocks) TT.groups = (TT.treeblocks/TT.blockbits)+1;
  505. else TT.groups = div_round_up(TT.blocks, TT.blockbits);
  506. for (;;) {
  507. temp = TT.treeblocks;
  508. for (i = 0; i<TT.groups; i++) temp += group_overhead(i);
  509. if (TT.blocks) {
  510. if (TT.blocks < temp) error_exit("Not enough space.\n");
  511. break;
  512. }
  513. if (temp <= TT.groups * TT.blockbits) {
  514. TT.blocks = temp;
  515. break;
  516. }
  517. TT.groups++;
  518. }
  519. TT.freeblocks = TT.blocks - temp;
  520. // Now we know all the TT data, initialize superblock structure.
  521. init_superblock(&sb);
  522. // Start writing. Skip the first 1k to avoid the boot sector (if any).
  523. put_zeroes(1024);
  524. // Loop through block groups, write out each one.
  525. dtbblk = usedblocks = usedinodes = 0;
  526. for (i=0; i<TT.groups; i++) {
  527. struct ext2_inode *in = (struct ext2_inode *)toybuf;
  528. uint32_t start, itable, used, end;
  529. int j, slot;
  530. // Where does this group end?
  531. end = TT.blockbits;
  532. if ((i+1)*TT.blockbits > TT.blocks) end = TT.blocks & (TT.blockbits-1);
  533. // Blocks used by inode table
  534. itable = (TT.inodespg*sizeof(struct ext2_inode))/TT.blocksize;
  535. // If a superblock goes here, write it out.
  536. start = group_superblock_overhead(i);
  537. if (start) {
  538. struct ext2_group *bg = (struct ext2_group *)toybuf;
  539. int treeblocks = TT.treeblocks, treeinodes = TT.treeinodes;
  540. sb.block_group_nr = SWAP_LE16(i);
  541. // Write superblock and pad it up to block size
  542. xwrite(TT.fsfd, &sb, sizeof(struct ext2_superblock));
  543. temp = TT.blocksize - sizeof(struct ext2_superblock);
  544. if (!i && TT.blocksize > 1024) temp -= 1024;
  545. memset(toybuf, 0, TT.blocksize);
  546. xwrite(TT.fsfd, toybuf, temp);
  547. // Loop through groups to write group descriptor table.
  548. for(j=0; j<TT.groups; j++) {
  549. // Figure out what sector this group starts in.
  550. used = group_superblock_overhead(j);
  551. // Find next array slot in this block (flush block if full).
  552. slot = j % (TT.blocksize/sizeof(struct ext2_group));
  553. if (!slot) {
  554. if (j) xwrite(TT.fsfd, bg, TT.blocksize);
  555. memset(bg, 0, TT.blocksize);
  556. }
  557. // How many free inodes in this group?
  558. temp = TT.inodespg;
  559. if (!i) temp -= INODES_RESERVED;
  560. if (temp > treeinodes) {
  561. treeinodes -= temp;
  562. temp = 0;
  563. } else {
  564. temp -= treeinodes;
  565. treeinodes = 0;
  566. }
  567. bg[slot].free_inodes_count = SWAP_LE16(temp);
  568. // How many free blocks in this group?
  569. temp = TT.inodespg/(TT.blocksize/sizeof(struct ext2_inode)) + 2;
  570. temp = end-used-temp;
  571. if (temp > treeblocks) {
  572. treeblocks -= temp;
  573. temp = 0;
  574. } else {
  575. temp -= treeblocks;
  576. treeblocks = 0;
  577. }
  578. bg[slot].free_blocks_count = SWAP_LE32(temp);
  579. // Fill out rest of group structure
  580. used += j*TT.blockbits;
  581. bg[slot].block_bitmap = SWAP_LE32(used++);
  582. bg[slot].inode_bitmap = SWAP_LE32(used++);
  583. bg[slot].inode_table = SWAP_LE32(used);
  584. bg[slot].used_dirs_count = 0; // (TODO)
  585. }
  586. xwrite(TT.fsfd, bg, TT.blocksize);
  587. }
  588. // Now write out stuff that every block group has.
  589. // Write block usage bitmap
  590. start += 2 + itable;
  591. memset(toybuf, 0, TT.blocksize);
  592. bits_set(toybuf, 0, start);
  593. bits_set(toybuf, end, TT.blockbits-end);
  594. temp = TT.treeblocks - usedblocks;
  595. if (temp) {
  596. if (end-start > temp) temp = end-start;
  597. bits_set(toybuf, start, temp);
  598. }
  599. xwrite(TT.fsfd, toybuf, TT.blocksize);
  600. // Write inode bitmap
  601. memset(toybuf, 0, TT.blocksize);
  602. j = 0;
  603. if (!i) bits_set(toybuf, 0, j = INODES_RESERVED);
  604. bits_set(toybuf, TT.inodespg, slot = TT.blockbits-TT.inodespg);
  605. temp = TT.treeinodes - usedinodes;
  606. if (temp) {
  607. if (slot-j > temp) temp = slot-j;
  608. bits_set(toybuf, j, temp);
  609. }
  610. xwrite(TT.fsfd, toybuf, TT.blocksize);
  611. // Write inode table for this group (TODO)
  612. for (j = 0; j<TT.inodespg; j++) {
  613. slot = j % (TT.blocksize/sizeof(struct ext2_inode));
  614. if (!slot) {
  615. if (j) xwrite(TT.fsfd, in, TT.blocksize);
  616. memset(in, 0, TT.blocksize);
  617. }
  618. if (!i && j<INODES_RESERVED) {
  619. // Write root inode
  620. if (j == 2) fill_inode(in+slot, dtb);
  621. } else if (dti) {
  622. fill_inode(in+slot, dti);
  623. dti = treenext(dti);
  624. }
  625. }
  626. xwrite(TT.fsfd, in, TT.blocksize);
  627. while (dtb) {
  628. // TODO write index data block
  629. // TODO write root directory data block
  630. // TODO write directory data block
  631. // TODO write file data block
  632. put_zeroes(TT.blocksize);
  633. start++;
  634. if (start == end) break;
  635. }
  636. // Write data blocks (TODO)
  637. put_zeroes((end-start) * TT.blocksize);
  638. }
  639. }