10f66f451Sopenharmony_ci/* mke2fs.c - Create an ext2 filesystem image. 20f66f451Sopenharmony_ci * 30f66f451Sopenharmony_ci * Copyright 2006, 2007 Rob Landley <rob@landley.net> 40f66f451Sopenharmony_ci 50f66f451Sopenharmony_ci// Still to go: "E:jJ:L:m:O:" 60f66f451Sopenharmony_ciUSE_MKE2FS(NEWTOY(mke2fs, "<1>2g:Fnqm#N#i#b#", TOYFLAG_SBIN)) 70f66f451Sopenharmony_ci 80f66f451Sopenharmony_ciconfig MKE2FS 90f66f451Sopenharmony_ci bool "mke2fs" 100f66f451Sopenharmony_ci default n 110f66f451Sopenharmony_ci help 120f66f451Sopenharmony_ci usage: mke2fs [-Fnq] [-b ###] [-N|i ###] [-m ###] device 130f66f451Sopenharmony_ci 140f66f451Sopenharmony_ci Create an ext2 filesystem on a block device or filesystem image. 150f66f451Sopenharmony_ci 160f66f451Sopenharmony_ci -F Force to run on a mounted device 170f66f451Sopenharmony_ci -n Don't write to device 180f66f451Sopenharmony_ci -q Quiet (no output) 190f66f451Sopenharmony_ci -b size Block size (1024, 2048, or 4096) 200f66f451Sopenharmony_ci -N inodes Allocate this many inodes 210f66f451Sopenharmony_ci -i bytes Allocate one inode for every XXX bytes of device 220f66f451Sopenharmony_ci -m percent Reserve this percent of filesystem space for root user 230f66f451Sopenharmony_ci 240f66f451Sopenharmony_ciconfig MKE2FS_JOURNAL 250f66f451Sopenharmony_ci bool "Journaling support (ext3)" 260f66f451Sopenharmony_ci default n 270f66f451Sopenharmony_ci depends on MKE2FS 280f66f451Sopenharmony_ci help 290f66f451Sopenharmony_ci usage: mke2fs [-j] [-J size=###,device=XXX] 300f66f451Sopenharmony_ci 310f66f451Sopenharmony_ci -j Create journal (ext3) 320f66f451Sopenharmony_ci -J Journal options 330f66f451Sopenharmony_ci size: Number of blocks (1024-102400) 340f66f451Sopenharmony_ci device: Specify an external journal 350f66f451Sopenharmony_ci 360f66f451Sopenharmony_ciconfig MKE2FS_GEN 370f66f451Sopenharmony_ci bool "Generate (gene2fs)" 380f66f451Sopenharmony_ci default n 390f66f451Sopenharmony_ci depends on MKE2FS 400f66f451Sopenharmony_ci help 410f66f451Sopenharmony_ci usage: gene2fs [options] device filename 420f66f451Sopenharmony_ci 430f66f451Sopenharmony_ci The [options] are the same as mke2fs. 440f66f451Sopenharmony_ci 450f66f451Sopenharmony_ciconfig MKE2FS_LABEL 460f66f451Sopenharmony_ci bool "Label support" 470f66f451Sopenharmony_ci default n 480f66f451Sopenharmony_ci depends on MKE2FS 490f66f451Sopenharmony_ci help 500f66f451Sopenharmony_ci usage: mke2fs [-L label] [-M path] [-o string] 510f66f451Sopenharmony_ci 520f66f451Sopenharmony_ci -L Volume label 530f66f451Sopenharmony_ci -M Path to mount point 540f66f451Sopenharmony_ci -o Created by 550f66f451Sopenharmony_ci 560f66f451Sopenharmony_ciconfig MKE2FS_EXTENDED 570f66f451Sopenharmony_ci bool "Extended options" 580f66f451Sopenharmony_ci default n 590f66f451Sopenharmony_ci depends on MKE2FS 600f66f451Sopenharmony_ci help 610f66f451Sopenharmony_ci usage: mke2fs [-E stride=###] [-O option[,option]] 620f66f451Sopenharmony_ci 630f66f451Sopenharmony_ci -E stride= Set RAID stripe size (in blocks) 640f66f451Sopenharmony_ci -O [opts] Specify fewer ext2 option flags (for old kernels) 650f66f451Sopenharmony_ci All of these are on by default (as appropriate) 660f66f451Sopenharmony_ci none Clear default options (all but journaling) 670f66f451Sopenharmony_ci dir_index Use htree indexes for large directories 680f66f451Sopenharmony_ci filetype Store file type info in directory entry 690f66f451Sopenharmony_ci has_journal Set by -j 700f66f451Sopenharmony_ci journal_dev Set by -J device=XXX 710f66f451Sopenharmony_ci sparse_super Don't allocate huge numbers of redundant superblocks 720f66f451Sopenharmony_ci*/ 730f66f451Sopenharmony_ci 740f66f451Sopenharmony_ci#define FOR_mke2fs 750f66f451Sopenharmony_ci#include "toys.h" 760f66f451Sopenharmony_ci 770f66f451Sopenharmony_ciGLOBALS( 780f66f451Sopenharmony_ci // Command line arguments. 790f66f451Sopenharmony_ci long blocksize; 800f66f451Sopenharmony_ci long bytes_per_inode; 810f66f451Sopenharmony_ci long inodes; // Total inodes in filesystem. 820f66f451Sopenharmony_ci long reserved_percent; // Integer precent of space to reserve for root. 830f66f451Sopenharmony_ci char *gendir; // Where to read dirtree from. 840f66f451Sopenharmony_ci 850f66f451Sopenharmony_ci // Internal data. 860f66f451Sopenharmony_ci struct dirtree *dt; // Tree of files to copy into the new filesystem. 870f66f451Sopenharmony_ci unsigned treeblocks; // Blocks used by dt 880f66f451Sopenharmony_ci unsigned treeinodes; // Inodes used by dt 890f66f451Sopenharmony_ci 900f66f451Sopenharmony_ci unsigned blocks; // Total blocks in the filesystem. 910f66f451Sopenharmony_ci unsigned freeblocks; // Free blocks in the filesystem. 920f66f451Sopenharmony_ci unsigned inodespg; // Inodes per group 930f66f451Sopenharmony_ci unsigned groups; // Total number of block groups. 940f66f451Sopenharmony_ci unsigned blockbits; // Bits per block. (Also blocks per group.) 950f66f451Sopenharmony_ci 960f66f451Sopenharmony_ci // For gene2fs 970f66f451Sopenharmony_ci unsigned nextblock; // Next data block to allocate 980f66f451Sopenharmony_ci unsigned nextgroup; // Next group we'll be allocating from 990f66f451Sopenharmony_ci int fsfd; // File descriptor of filesystem (to output to). 1000f66f451Sopenharmony_ci) 1010f66f451Sopenharmony_ci 1020f66f451Sopenharmony_ci// Stuff defined in linux/ext2_fs.h 1030f66f451Sopenharmony_ci 1040f66f451Sopenharmony_ci#define EXT2_SUPER_MAGIC 0xEF53 1050f66f451Sopenharmony_ci 1060f66f451Sopenharmony_cistruct ext2_superblock { 1070f66f451Sopenharmony_ci uint32_t inodes_count; // Inodes count 1080f66f451Sopenharmony_ci uint32_t blocks_count; // Blocks count 1090f66f451Sopenharmony_ci uint32_t r_blocks_count; // Reserved blocks count 1100f66f451Sopenharmony_ci uint32_t free_blocks_count; // Free blocks count 1110f66f451Sopenharmony_ci uint32_t free_inodes_count; // Free inodes count 1120f66f451Sopenharmony_ci uint32_t first_data_block; // First Data Block 1130f66f451Sopenharmony_ci uint32_t log_block_size; // Block size 1140f66f451Sopenharmony_ci uint32_t log_frag_size; // Fragment size 1150f66f451Sopenharmony_ci uint32_t blocks_per_group; // Blocks per group 1160f66f451Sopenharmony_ci uint32_t frags_per_group; // Fragments per group 1170f66f451Sopenharmony_ci uint32_t inodes_per_group; // Inodes per group 1180f66f451Sopenharmony_ci uint32_t mtime; // Mount time 1190f66f451Sopenharmony_ci uint32_t wtime; // Write time 1200f66f451Sopenharmony_ci uint16_t mnt_count; // Mount count 1210f66f451Sopenharmony_ci uint16_t max_mnt_count; // Maximal mount count 1220f66f451Sopenharmony_ci uint16_t magic; // Magic signature 1230f66f451Sopenharmony_ci uint16_t state; // File system state 1240f66f451Sopenharmony_ci uint16_t errors; // Behaviour when detecting errors 1250f66f451Sopenharmony_ci uint16_t minor_rev_level; // minor revision level 1260f66f451Sopenharmony_ci uint32_t lastcheck; // time of last check 1270f66f451Sopenharmony_ci uint32_t checkinterval; // max. time between checks 1280f66f451Sopenharmony_ci uint32_t creator_os; // OS 1290f66f451Sopenharmony_ci uint32_t rev_level; // Revision level 1300f66f451Sopenharmony_ci uint16_t def_resuid; // Default uid for reserved blocks 1310f66f451Sopenharmony_ci uint16_t def_resgid; // Default gid for reserved blocks 1320f66f451Sopenharmony_ci uint32_t first_ino; // First non-reserved inode 1330f66f451Sopenharmony_ci uint16_t inode_size; // size of inode structure 1340f66f451Sopenharmony_ci uint16_t block_group_nr; // block group # of this superblock 1350f66f451Sopenharmony_ci uint32_t feature_compat; // compatible feature set 1360f66f451Sopenharmony_ci uint32_t feature_incompat; // incompatible feature set 1370f66f451Sopenharmony_ci uint32_t feature_ro_compat; // readonly-compatible feature set 1380f66f451Sopenharmony_ci char uuid[16]; // 128-bit uuid for volume 1390f66f451Sopenharmony_ci char volume_name[16]; // volume name 1400f66f451Sopenharmony_ci char last_mounted[64]; // directory where last mounted 1410f66f451Sopenharmony_ci uint32_t alg_usage_bitmap; // For compression 1420f66f451Sopenharmony_ci // For EXT2_COMPAT_PREALLOC 1430f66f451Sopenharmony_ci uint8_t prealloc_blocks; // Nr of blocks to try to preallocate 1440f66f451Sopenharmony_ci uint8_t prealloc_dir_blocks; //Nr to preallocate for dirs 1450f66f451Sopenharmony_ci uint16_t padding1; 1460f66f451Sopenharmony_ci // For EXT3_FEATURE_COMPAT_HAS_JOURNAL 1470f66f451Sopenharmony_ci uint8_t journal_uuid[16]; // uuid of journal superblock 1480f66f451Sopenharmony_ci uint32_t journal_inum; // inode number of journal file 1490f66f451Sopenharmony_ci uint32_t journal_dev; // device number of journal file 1500f66f451Sopenharmony_ci uint32_t last_orphan; // start of list of inodes to delete 1510f66f451Sopenharmony_ci uint32_t hash_seed[4]; // HTREE hash seed 1520f66f451Sopenharmony_ci uint8_t def_hash_version; // Default hash version to use 1530f66f451Sopenharmony_ci uint8_t padding2[3]; 1540f66f451Sopenharmony_ci uint32_t default_mount_opts; 1550f66f451Sopenharmony_ci uint32_t first_meta_bg; // First metablock block group 1560f66f451Sopenharmony_ci uint32_t mkfs_time; // Creation timestamp 1570f66f451Sopenharmony_ci uint32_t jnl_blocks[17]; // Backup of journal inode 1580f66f451Sopenharmony_ci // uint32_t reserved[172]; // Padding to the end of the block 1590f66f451Sopenharmony_ci}; 1600f66f451Sopenharmony_ci 1610f66f451Sopenharmony_cistruct ext2_group 1620f66f451Sopenharmony_ci{ 1630f66f451Sopenharmony_ci uint32_t block_bitmap; // Block number of block bitmap 1640f66f451Sopenharmony_ci uint32_t inode_bitmap; // Block number of inode bitmap 1650f66f451Sopenharmony_ci uint32_t inode_table; // Block number of inode table 1660f66f451Sopenharmony_ci uint16_t free_blocks_count; // How many free blocks in this group? 1670f66f451Sopenharmony_ci uint16_t free_inodes_count; // How many free inodes in this group? 1680f66f451Sopenharmony_ci uint16_t used_dirs_count; // How many directories? 1690f66f451Sopenharmony_ci uint16_t reserved[7]; // pad to 32 bytes 1700f66f451Sopenharmony_ci}; 1710f66f451Sopenharmony_ci 1720f66f451Sopenharmony_cistruct ext2_dentry { 1730f66f451Sopenharmony_ci uint32_t inode; // Inode number 1740f66f451Sopenharmony_ci uint16_t rec_len; // Directory entry length 1750f66f451Sopenharmony_ci uint8_t name_len; // Name length 1760f66f451Sopenharmony_ci uint8_t file_type; 1770f66f451Sopenharmony_ci char name[0]; // File name 1780f66f451Sopenharmony_ci}; 1790f66f451Sopenharmony_ci 1800f66f451Sopenharmony_cistruct ext2_inode { 1810f66f451Sopenharmony_ci uint16_t mode; // File mode 1820f66f451Sopenharmony_ci uint16_t uid; // Low 16 bits of Owner Uid 1830f66f451Sopenharmony_ci uint32_t size; // Size in bytes 1840f66f451Sopenharmony_ci uint32_t atime; // Access time 1850f66f451Sopenharmony_ci uint32_t ctime; // Creation time 1860f66f451Sopenharmony_ci uint32_t mtime; // Modification time 1870f66f451Sopenharmony_ci uint32_t dtime; // Deletion Time 1880f66f451Sopenharmony_ci uint16_t gid; // Low 16 bits of Group Id 1890f66f451Sopenharmony_ci uint16_t links_count; // Links count 1900f66f451Sopenharmony_ci uint32_t blocks; // Blocks count 1910f66f451Sopenharmony_ci uint32_t flags; // File flags 1920f66f451Sopenharmony_ci uint32_t reserved1; 1930f66f451Sopenharmony_ci uint32_t block[15]; // Pointers to blocks 1940f66f451Sopenharmony_ci uint32_t generation; // File version (for NFS) 1950f66f451Sopenharmony_ci uint32_t file_acl; // File ACL 1960f66f451Sopenharmony_ci uint32_t dir_acl; // Directory ACL (or top bits of file length) 1970f66f451Sopenharmony_ci uint32_t faddr; // Last block in file 1980f66f451Sopenharmony_ci uint8_t frag; // Fragment number 1990f66f451Sopenharmony_ci uint8_t fsize; // Fragment size 2000f66f451Sopenharmony_ci uint16_t pad1; 2010f66f451Sopenharmony_ci uint16_t uid_high; // High bits of uid 2020f66f451Sopenharmony_ci uint16_t gid_high; // High bits of gid 2030f66f451Sopenharmony_ci uint32_t reserved2; 2040f66f451Sopenharmony_ci}; 2050f66f451Sopenharmony_ci 2060f66f451Sopenharmony_ci#define EXT2_FEATURE_COMPAT_DIR_PREALLOC 0x0001 2070f66f451Sopenharmony_ci#define EXT2_FEATURE_COMPAT_IMAGIC_INODES 0x0002 2080f66f451Sopenharmony_ci#define EXT3_FEATURE_COMPAT_HAS_JOURNAL 0x0004 2090f66f451Sopenharmony_ci#define EXT2_FEATURE_COMPAT_EXT_ATTR 0x0008 2100f66f451Sopenharmony_ci#define EXT2_FEATURE_COMPAT_RESIZE_INO 0x0010 2110f66f451Sopenharmony_ci#define EXT2_FEATURE_COMPAT_DIR_INDEX 0x0020 2120f66f451Sopenharmony_ci 2130f66f451Sopenharmony_ci#define EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER 0x0001 2140f66f451Sopenharmony_ci#define EXT2_FEATURE_RO_COMPAT_LARGE_FILE 0x0002 2150f66f451Sopenharmony_ci#define EXT2_FEATURE_RO_COMPAT_BTREE_DIR 0x0004 2160f66f451Sopenharmony_ci 2170f66f451Sopenharmony_ci#define EXT2_FEATURE_INCOMPAT_COMPRESSION 0x0001 2180f66f451Sopenharmony_ci#define EXT2_FEATURE_INCOMPAT_FILETYPE 0x0002 2190f66f451Sopenharmony_ci#define EXT3_FEATURE_INCOMPAT_RECOVER 0x0004 2200f66f451Sopenharmony_ci#define EXT3_FEATURE_INCOMPAT_JOURNAL_DEV 0x0008 2210f66f451Sopenharmony_ci#define EXT2_FEATURE_INCOMPAT_META_BG 0x0010 2220f66f451Sopenharmony_ci 2230f66f451Sopenharmony_ci#define EXT2_NAME_LEN 255 2240f66f451Sopenharmony_ci 2250f66f451Sopenharmony_ci// Ext2 directory file types. Only the low 3 bits are used. The 2260f66f451Sopenharmony_ci// other bits are reserved for now. 2270f66f451Sopenharmony_ci 2280f66f451Sopenharmony_cienum { 2290f66f451Sopenharmony_ci EXT2_FT_UNKNOWN, 2300f66f451Sopenharmony_ci EXT2_FT_REG_FILE, 2310f66f451Sopenharmony_ci EXT2_FT_DIR, 2320f66f451Sopenharmony_ci EXT2_FT_CHRDEV, 2330f66f451Sopenharmony_ci EXT2_FT_BLKDEV, 2340f66f451Sopenharmony_ci EXT2_FT_FIFO, 2350f66f451Sopenharmony_ci EXT2_FT_SOCK, 2360f66f451Sopenharmony_ci EXT2_FT_SYMLINK, 2370f66f451Sopenharmony_ci EXT2_FT_MAX 2380f66f451Sopenharmony_ci}; 2390f66f451Sopenharmony_ci 2400f66f451Sopenharmony_ci#define INODES_RESERVED 10 2410f66f451Sopenharmony_ci 2420f66f451Sopenharmony_cistatic uint32_t div_round_up(uint32_t a, uint32_t b) 2430f66f451Sopenharmony_ci{ 2440f66f451Sopenharmony_ci uint32_t c = a/b; 2450f66f451Sopenharmony_ci 2460f66f451Sopenharmony_ci if (a%b) c++; 2470f66f451Sopenharmony_ci return c; 2480f66f451Sopenharmony_ci} 2490f66f451Sopenharmony_ci 2500f66f451Sopenharmony_ci// Calculate data blocks plus index blocks needed to hold a file. 2510f66f451Sopenharmony_ci 2520f66f451Sopenharmony_cistatic uint32_t file_blocks_used(uint64_t size, uint32_t *blocklist) 2530f66f451Sopenharmony_ci{ 2540f66f451Sopenharmony_ci uint32_t dblocks = (uint32_t)((size+(TT.blocksize-1))/TT.blocksize); 2550f66f451Sopenharmony_ci uint32_t idx=TT.blocksize/4, iblocks=0, diblocks=0, tiblocks=0; 2560f66f451Sopenharmony_ci 2570f66f451Sopenharmony_ci // Fill out index blocks in inode. 2580f66f451Sopenharmony_ci 2590f66f451Sopenharmony_ci if (blocklist) { 2600f66f451Sopenharmony_ci int i; 2610f66f451Sopenharmony_ci 2620f66f451Sopenharmony_ci // Direct index blocks 2630f66f451Sopenharmony_ci for (i=0; i<13 && i<dblocks; i++) blocklist[i] = i; 2640f66f451Sopenharmony_ci // Singly indirect index blocks 2650f66f451Sopenharmony_ci if (dblocks > 13+idx) blocklist[13] = 13+idx; 2660f66f451Sopenharmony_ci // Doubly indirect index blocks 2670f66f451Sopenharmony_ci idx = 13 + idx + (idx*idx); 2680f66f451Sopenharmony_ci if (dblocks > idx) blocklist[14] = idx; 2690f66f451Sopenharmony_ci 2700f66f451Sopenharmony_ci return 0; 2710f66f451Sopenharmony_ci } 2720f66f451Sopenharmony_ci 2730f66f451Sopenharmony_ci // Account for direct, singly, doubly, and triply indirect index blocks 2740f66f451Sopenharmony_ci 2750f66f451Sopenharmony_ci if (dblocks > 12) { 2760f66f451Sopenharmony_ci iblocks = ((dblocks-13)/idx)+1; 2770f66f451Sopenharmony_ci if (iblocks > 1) { 2780f66f451Sopenharmony_ci diblocks = ((iblocks-2)/idx)+1; 2790f66f451Sopenharmony_ci if (diblocks > 1) 2800f66f451Sopenharmony_ci tiblocks = ((diblocks-2)/idx)+1; 2810f66f451Sopenharmony_ci } 2820f66f451Sopenharmony_ci } 2830f66f451Sopenharmony_ci 2840f66f451Sopenharmony_ci return dblocks + iblocks + diblocks + tiblocks; 2850f66f451Sopenharmony_ci} 2860f66f451Sopenharmony_ci 2870f66f451Sopenharmony_ci// Use the parent pointer to iterate through the tree non-recursively. 2880f66f451Sopenharmony_cistatic struct dirtree *treenext(struct dirtree *this) 2890f66f451Sopenharmony_ci{ 2900f66f451Sopenharmony_ci while (this && !this->next) this = this->parent; 2910f66f451Sopenharmony_ci if (this) this = this->next; 2920f66f451Sopenharmony_ci 2930f66f451Sopenharmony_ci return this; 2940f66f451Sopenharmony_ci} 2950f66f451Sopenharmony_ci 2960f66f451Sopenharmony_ci// Recursively calculate the number of blocks used by each inode in the tree. 2970f66f451Sopenharmony_ci// Returns blocks used by this directory, assigns bytes used to *size. 2980f66f451Sopenharmony_ci// Writes total block count to TT.treeblocks and inode count to TT.treeinodes. 2990f66f451Sopenharmony_ci 3000f66f451Sopenharmony_cistatic long check_treesize(struct dirtree *that, off_t *size) 3010f66f451Sopenharmony_ci{ 3020f66f451Sopenharmony_ci long blocks; 3030f66f451Sopenharmony_ci 3040f66f451Sopenharmony_ci while (that) { 3050f66f451Sopenharmony_ci *size += sizeof(struct ext2_dentry) + strlen(that->name); 3060f66f451Sopenharmony_ci 3070f66f451Sopenharmony_ci if (that->child) 3080f66f451Sopenharmony_ci that->st.st_blocks = check_treesize(that->child, &that->st.st_size); 3090f66f451Sopenharmony_ci else if (S_ISREG(that->st.st_mode)) { 3100f66f451Sopenharmony_ci that->st.st_blocks = file_blocks_used(that->st.st_size, 0); 3110f66f451Sopenharmony_ci TT.treeblocks += that->st.st_blocks; 3120f66f451Sopenharmony_ci } 3130f66f451Sopenharmony_ci that = that->next; 3140f66f451Sopenharmony_ci } 3150f66f451Sopenharmony_ci TT.treeblocks += blocks = file_blocks_used(*size, 0); 3160f66f451Sopenharmony_ci TT.treeinodes++; 3170f66f451Sopenharmony_ci 3180f66f451Sopenharmony_ci return blocks; 3190f66f451Sopenharmony_ci} 3200f66f451Sopenharmony_ci 3210f66f451Sopenharmony_ci// Calculate inode numbers and link counts. 3220f66f451Sopenharmony_ci// 3230f66f451Sopenharmony_ci// To do this right I need to copy the tree and sort it, but here's a really 3240f66f451Sopenharmony_ci// ugly n^2 way of dealing with the problem that doesn't scale well to large 3250f66f451Sopenharmony_ci// numbers of files (> 100,000) but can be done in very little code. 3260f66f451Sopenharmony_ci// This rewrites inode numbers to their final values, allocating depth first. 3270f66f451Sopenharmony_ci 3280f66f451Sopenharmony_cistatic void check_treelinks(struct dirtree *tree) 3290f66f451Sopenharmony_ci{ 3300f66f451Sopenharmony_ci struct dirtree *current=tree, *that; 3310f66f451Sopenharmony_ci long inode = INODES_RESERVED; 3320f66f451Sopenharmony_ci 3330f66f451Sopenharmony_ci while (current) { 3340f66f451Sopenharmony_ci ++inode; 3350f66f451Sopenharmony_ci // Since we can't hardlink to directories, we know their link count. 3360f66f451Sopenharmony_ci if (S_ISDIR(current->st.st_mode)) current->st.st_nlink = 2; 3370f66f451Sopenharmony_ci else { 3380f66f451Sopenharmony_ci dev_t new = current->st.st_dev; 3390f66f451Sopenharmony_ci 3400f66f451Sopenharmony_ci if (!new) continue; 3410f66f451Sopenharmony_ci 3420f66f451Sopenharmony_ci // Look for other copies of current node 3430f66f451Sopenharmony_ci current->st.st_nlink = 0; 3440f66f451Sopenharmony_ci for (that = tree; that; that = treenext(that)) { 3450f66f451Sopenharmony_ci if (current->st.st_ino == that->st.st_ino && 3460f66f451Sopenharmony_ci current->st.st_dev == that->st.st_dev) 3470f66f451Sopenharmony_ci { 3480f66f451Sopenharmony_ci current->st.st_nlink++; 3490f66f451Sopenharmony_ci current->st.st_ino = inode; 3500f66f451Sopenharmony_ci } 3510f66f451Sopenharmony_ci } 3520f66f451Sopenharmony_ci } 3530f66f451Sopenharmony_ci current->st.st_ino = inode; 3540f66f451Sopenharmony_ci current = treenext(current); 3550f66f451Sopenharmony_ci } 3560f66f451Sopenharmony_ci} 3570f66f451Sopenharmony_ci 3580f66f451Sopenharmony_ci// Calculate inodes per group from total inodes. 3590f66f451Sopenharmony_cistatic uint32_t get_inodespg(uint32_t inodes) 3600f66f451Sopenharmony_ci{ 3610f66f451Sopenharmony_ci uint32_t temp; 3620f66f451Sopenharmony_ci 3630f66f451Sopenharmony_ci // Round up to fill complete inode blocks. 3640f66f451Sopenharmony_ci temp = (inodes + TT.groups - 1) / TT.groups; 3650f66f451Sopenharmony_ci inodes = TT.blocksize/sizeof(struct ext2_inode); 3660f66f451Sopenharmony_ci return ((temp + inodes - 1)/inodes)*inodes; 3670f66f451Sopenharmony_ci} 3680f66f451Sopenharmony_ci 3690f66f451Sopenharmony_ci// Fill out superblock and TT structures. 3700f66f451Sopenharmony_ci 3710f66f451Sopenharmony_cistatic void init_superblock(struct ext2_superblock *sb) 3720f66f451Sopenharmony_ci{ 3730f66f451Sopenharmony_ci uint32_t temp; 3740f66f451Sopenharmony_ci 3750f66f451Sopenharmony_ci // Set log_block_size and log_frag_size. 3760f66f451Sopenharmony_ci 3770f66f451Sopenharmony_ci for (temp = 0; temp < 4; temp++) if (TT.blocksize == 1024<<temp) break; 3780f66f451Sopenharmony_ci if (temp==4) error_exit("bad blocksize"); 3790f66f451Sopenharmony_ci sb->log_block_size = sb->log_frag_size = SWAP_LE32(temp); 3800f66f451Sopenharmony_ci 3810f66f451Sopenharmony_ci // Fill out blocks_count, r_blocks_count, first_data_block 3820f66f451Sopenharmony_ci 3830f66f451Sopenharmony_ci sb->blocks_count = SWAP_LE32(TT.blocks); 3840f66f451Sopenharmony_ci sb->free_blocks_count = SWAP_LE32(TT.freeblocks); 3850f66f451Sopenharmony_ci temp = (TT.blocks * (uint64_t)TT.reserved_percent) / 100; 3860f66f451Sopenharmony_ci sb->r_blocks_count = SWAP_LE32(temp); 3870f66f451Sopenharmony_ci 3880f66f451Sopenharmony_ci sb->first_data_block = SWAP_LE32(TT.blocksize == 1024 ? 1 : 0); 3890f66f451Sopenharmony_ci 3900f66f451Sopenharmony_ci // Set blocks_per_group and frags_per_group, which is the size of an 3910f66f451Sopenharmony_ci // allocation bitmap that fits in one block (I.E. how many bits per block)? 3920f66f451Sopenharmony_ci 3930f66f451Sopenharmony_ci sb->blocks_per_group = sb->frags_per_group = SWAP_LE32(TT.blockbits); 3940f66f451Sopenharmony_ci 3950f66f451Sopenharmony_ci // Set inodes_per_group and total inodes_count 3960f66f451Sopenharmony_ci sb->inodes_per_group = SWAP_LE32(TT.inodespg); 3970f66f451Sopenharmony_ci sb->inodes_count = SWAP_LE32(TT.inodespg * TT.groups); 3980f66f451Sopenharmony_ci 3990f66f451Sopenharmony_ci // Determine free inodes. 4000f66f451Sopenharmony_ci temp = TT.inodespg*TT.groups - INODES_RESERVED; 4010f66f451Sopenharmony_ci if (temp < TT.treeinodes) error_exit("Not enough inodes.\n"); 4020f66f451Sopenharmony_ci sb->free_inodes_count = SWAP_LE32(temp - TT.treeinodes); 4030f66f451Sopenharmony_ci 4040f66f451Sopenharmony_ci // Fill out the rest of the superblock. 4050f66f451Sopenharmony_ci sb->max_mnt_count=0xFFFF; 4060f66f451Sopenharmony_ci sb->wtime = sb->lastcheck = sb->mkfs_time = SWAP_LE32(time(NULL)); 4070f66f451Sopenharmony_ci sb->magic = SWAP_LE32(0xEF53); 4080f66f451Sopenharmony_ci sb->state = sb->errors = SWAP_LE16(1); 4090f66f451Sopenharmony_ci 4100f66f451Sopenharmony_ci sb->rev_level = SWAP_LE32(1); 4110f66f451Sopenharmony_ci sb->first_ino = SWAP_LE32(INODES_RESERVED+1); 4120f66f451Sopenharmony_ci sb->inode_size = SWAP_LE16(sizeof(struct ext2_inode)); 4130f66f451Sopenharmony_ci sb->feature_incompat = SWAP_LE32(EXT2_FEATURE_INCOMPAT_FILETYPE); 4140f66f451Sopenharmony_ci sb->feature_ro_compat = SWAP_LE32(EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER); 4150f66f451Sopenharmony_ci 4160f66f451Sopenharmony_ci create_uuid(sb->uuid); 4170f66f451Sopenharmony_ci 4180f66f451Sopenharmony_ci // TODO If we're called as mke3fs or mkfs.ext3, do a journal. 4190f66f451Sopenharmony_ci 4200f66f451Sopenharmony_ci //if (strchr(toys.which->name,'3')) 4210f66f451Sopenharmony_ci // sb->feature_compat |= SWAP_LE32(EXT3_FEATURE_COMPAT_HAS_JOURNAL); 4220f66f451Sopenharmony_ci} 4230f66f451Sopenharmony_ci 4240f66f451Sopenharmony_ci// Does this group contain a superblock backup (and group descriptor table)? 4250f66f451Sopenharmony_cistatic int is_sb_group(uint32_t group) 4260f66f451Sopenharmony_ci{ 4270f66f451Sopenharmony_ci int i; 4280f66f451Sopenharmony_ci 4290f66f451Sopenharmony_ci // Superblock backups are on groups 0, 1, and powers of 3, 5, and 7. 4300f66f451Sopenharmony_ci if(!group || group==1) return 1; 4310f66f451Sopenharmony_ci for (i=3; i<9; i+=2) { 4320f66f451Sopenharmony_ci int j = i; 4330f66f451Sopenharmony_ci while (j<group) j*=i; 4340f66f451Sopenharmony_ci if (j==group) return 1; 4350f66f451Sopenharmony_ci } 4360f66f451Sopenharmony_ci return 0; 4370f66f451Sopenharmony_ci} 4380f66f451Sopenharmony_ci 4390f66f451Sopenharmony_ci 4400f66f451Sopenharmony_ci// Number of blocks used in group by optional superblock/group list backup. 4410f66f451Sopenharmony_cistatic int group_superblock_overhead(uint32_t group) 4420f66f451Sopenharmony_ci{ 4430f66f451Sopenharmony_ci int used; 4440f66f451Sopenharmony_ci 4450f66f451Sopenharmony_ci if (!is_sb_group(group)) return 0; 4460f66f451Sopenharmony_ci 4470f66f451Sopenharmony_ci // How many blocks does the group descriptor table take up? 4480f66f451Sopenharmony_ci used = TT.groups * sizeof(struct ext2_group); 4490f66f451Sopenharmony_ci used += TT.blocksize - 1; 4500f66f451Sopenharmony_ci used /= TT.blocksize; 4510f66f451Sopenharmony_ci // Plus the superblock itself. 4520f66f451Sopenharmony_ci used++; 4530f66f451Sopenharmony_ci // And a corner case. 4540f66f451Sopenharmony_ci if (!group && TT.blocksize == 1024) used++; 4550f66f451Sopenharmony_ci 4560f66f451Sopenharmony_ci return used; 4570f66f451Sopenharmony_ci} 4580f66f451Sopenharmony_ci 4590f66f451Sopenharmony_ci// Number of blocks used in group to store superblock/group/inode list 4600f66f451Sopenharmony_cistatic int group_overhead(uint32_t group) 4610f66f451Sopenharmony_ci{ 4620f66f451Sopenharmony_ci // Return superblock backup overhead (if any), plus block/inode 4630f66f451Sopenharmony_ci // allocation bitmaps, plus inode tables. 4640f66f451Sopenharmony_ci return group_superblock_overhead(group) + 2 + get_inodespg(TT.inodespg) 4650f66f451Sopenharmony_ci / (TT.blocksize/sizeof(struct ext2_inode)); 4660f66f451Sopenharmony_ci} 4670f66f451Sopenharmony_ci 4680f66f451Sopenharmony_ci// In bitmap "array" set "len" bits starting at position "start" (from 0). 4690f66f451Sopenharmony_cistatic void bits_set(char *array, int start, int len) 4700f66f451Sopenharmony_ci{ 4710f66f451Sopenharmony_ci while(len) { 4720f66f451Sopenharmony_ci if ((start&7) || len<8) { 4730f66f451Sopenharmony_ci array[start/8]|=(1<<(start&7)); 4740f66f451Sopenharmony_ci start++; 4750f66f451Sopenharmony_ci len--; 4760f66f451Sopenharmony_ci } else { 4770f66f451Sopenharmony_ci array[start/8]=255; 4780f66f451Sopenharmony_ci start+=8; 4790f66f451Sopenharmony_ci len-=8; 4800f66f451Sopenharmony_ci } 4810f66f451Sopenharmony_ci } 4820f66f451Sopenharmony_ci} 4830f66f451Sopenharmony_ci 4840f66f451Sopenharmony_ci// Seek past len bytes (to maintain sparse file), or write zeroes if output 4850f66f451Sopenharmony_ci// not seekable 4860f66f451Sopenharmony_cistatic void put_zeroes(int len) 4870f66f451Sopenharmony_ci{ 4880f66f451Sopenharmony_ci if(-1 == lseek(TT.fsfd, len, SEEK_SET)) { 4890f66f451Sopenharmony_ci memset(toybuf, 0, sizeof(toybuf)); 4900f66f451Sopenharmony_ci while (len) { 4910f66f451Sopenharmony_ci int out = len > sizeof(toybuf) ? sizeof(toybuf) : len; 4920f66f451Sopenharmony_ci xwrite(TT.fsfd, toybuf, out); 4930f66f451Sopenharmony_ci len -= out; 4940f66f451Sopenharmony_ci } 4950f66f451Sopenharmony_ci } 4960f66f451Sopenharmony_ci} 4970f66f451Sopenharmony_ci 4980f66f451Sopenharmony_ci// Fill out an inode structure from struct stat info in dirtree. 4990f66f451Sopenharmony_cistatic void fill_inode(struct ext2_inode *in, struct dirtree *that) 5000f66f451Sopenharmony_ci{ 5010f66f451Sopenharmony_ci uint32_t fbu[15]; 5020f66f451Sopenharmony_ci int temp; 5030f66f451Sopenharmony_ci 5040f66f451Sopenharmony_ci file_blocks_used(that->st.st_size, fbu); 5050f66f451Sopenharmony_ci 5060f66f451Sopenharmony_ci // If that inode needs data blocks allocated to it. 5070f66f451Sopenharmony_ci if (that->st.st_size) { 5080f66f451Sopenharmony_ci int i, group = TT.nextblock/TT.blockbits; 5090f66f451Sopenharmony_ci 5100f66f451Sopenharmony_ci // TODO: teach this about indirect blocks. 5110f66f451Sopenharmony_ci for (i=0; i<15; i++) { 5120f66f451Sopenharmony_ci // If we just jumped into a new group, skip group overhead blocks. 5130f66f451Sopenharmony_ci while (group >= TT.nextgroup) 5140f66f451Sopenharmony_ci TT.nextblock += group_overhead(TT.nextgroup++); 5150f66f451Sopenharmony_ci } 5160f66f451Sopenharmony_ci } 5170f66f451Sopenharmony_ci // TODO : S_ISREG/DIR/CHR/BLK/FIFO/LNK/SOCK(m) 5180f66f451Sopenharmony_ci in->mode = SWAP_LE32(that->st.st_mode); 5190f66f451Sopenharmony_ci 5200f66f451Sopenharmony_ci in->uid = SWAP_LE16(that->st.st_uid & 0xFFFF); 5210f66f451Sopenharmony_ci in->uid_high = SWAP_LE16(that->st.st_uid >> 16); 5220f66f451Sopenharmony_ci in->gid = SWAP_LE16(that->st.st_gid & 0xFFFF); 5230f66f451Sopenharmony_ci in->gid_high = SWAP_LE16(that->st.st_gid >> 16); 5240f66f451Sopenharmony_ci in->size = SWAP_LE32(that->st.st_size & 0xFFFFFFFF); 5250f66f451Sopenharmony_ci 5260f66f451Sopenharmony_ci // Contortions to make the compiler not generate a warning for x>>32 5270f66f451Sopenharmony_ci // when x is 32 bits. The optimizer should clean this up. 5280f66f451Sopenharmony_ci if (sizeof(that->st.st_size) > 4) temp = 32; 5290f66f451Sopenharmony_ci else temp = 0; 5300f66f451Sopenharmony_ci if (temp) in->dir_acl = SWAP_LE32(that->st.st_size >> temp); 5310f66f451Sopenharmony_ci 5320f66f451Sopenharmony_ci in->atime = SWAP_LE32(that->st.st_atime); 5330f66f451Sopenharmony_ci in->ctime = SWAP_LE32(that->st.st_ctime); 5340f66f451Sopenharmony_ci in->mtime = SWAP_LE32(that->st.st_mtime); 5350f66f451Sopenharmony_ci 5360f66f451Sopenharmony_ci in->links_count = SWAP_LE16(that->st.st_nlink); 5370f66f451Sopenharmony_ci in->blocks = SWAP_LE32(that->st.st_blocks); 5380f66f451Sopenharmony_ci // in->faddr 5390f66f451Sopenharmony_ci} 5400f66f451Sopenharmony_ci 5410f66f451Sopenharmony_ci// Works like an archiver. 5420f66f451Sopenharmony_ci// The first argument is the name of the file to create. If it already 5430f66f451Sopenharmony_ci// exists, that size will be used. 5440f66f451Sopenharmony_ci 5450f66f451Sopenharmony_civoid mke2fs_main(void) 5460f66f451Sopenharmony_ci{ 5470f66f451Sopenharmony_ci int i, temp; 5480f66f451Sopenharmony_ci off_t length; 5490f66f451Sopenharmony_ci uint32_t usedblocks, usedinodes, dtbblk; 5500f66f451Sopenharmony_ci struct dirtree *dti, *dtb; 5510f66f451Sopenharmony_ci struct ext2_superblock sb; 5520f66f451Sopenharmony_ci 5530f66f451Sopenharmony_ci // Handle command line arguments. 5540f66f451Sopenharmony_ci 5550f66f451Sopenharmony_ci if (toys.optargs[1]) { 5560f66f451Sopenharmony_ci sscanf(toys.optargs[1], "%u", &TT.blocks); 5570f66f451Sopenharmony_ci temp = O_RDWR|O_CREAT; 5580f66f451Sopenharmony_ci } else temp = O_RDWR; 5590f66f451Sopenharmony_ci if (!TT.reserved_percent) TT.reserved_percent = 5; 5600f66f451Sopenharmony_ci 5610f66f451Sopenharmony_ci // TODO: Check if filesystem is mounted here 5620f66f451Sopenharmony_ci 5630f66f451Sopenharmony_ci // For mke?fs, open file. For gene?fs, create file. 5640f66f451Sopenharmony_ci TT.fsfd = xcreate(*toys.optargs, temp, 0777); 5650f66f451Sopenharmony_ci 5660f66f451Sopenharmony_ci // Determine appropriate block size and block count from file length. 5670f66f451Sopenharmony_ci // (If no length, default to 4k. They can override it on the cmdline.) 5680f66f451Sopenharmony_ci 5690f66f451Sopenharmony_ci length = fdlength(TT.fsfd); 5700f66f451Sopenharmony_ci if (!TT.blocksize) TT.blocksize = (length && length < 1<<29) ? 1024 : 4096; 5710f66f451Sopenharmony_ci TT.blockbits = 8*TT.blocksize; 5720f66f451Sopenharmony_ci if (!TT.blocks) TT.blocks = length/TT.blocksize; 5730f66f451Sopenharmony_ci 5740f66f451Sopenharmony_ci // Collect gene2fs list or lost+found, calculate requirements. 5750f66f451Sopenharmony_ci 5760f66f451Sopenharmony_ci if (TT.gendir) { 5770f66f451Sopenharmony_ci strncpy(toybuf, TT.gendir, sizeof(toybuf)); 5780f66f451Sopenharmony_ci dti = dirtree_read(toybuf, dirtree_notdotdot); 5790f66f451Sopenharmony_ci } else { 5800f66f451Sopenharmony_ci dti = xzalloc(sizeof(struct dirtree)+11); 5810f66f451Sopenharmony_ci strcpy(dti->name, "lost+found"); 5820f66f451Sopenharmony_ci dti->st.st_mode = S_IFDIR|0755; 5830f66f451Sopenharmony_ci dti->st.st_ctime = dti->st.st_mtime = time(NULL); 5840f66f451Sopenharmony_ci } 5850f66f451Sopenharmony_ci 5860f66f451Sopenharmony_ci // Add root directory inode. This is iterated through for when finding 5870f66f451Sopenharmony_ci // blocks, but not when finding inodes. The tree's parent pointers don't 5880f66f451Sopenharmony_ci // point back into this. 5890f66f451Sopenharmony_ci 5900f66f451Sopenharmony_ci dtb = xzalloc(sizeof(struct dirtree)+1); 5910f66f451Sopenharmony_ci dtb->st.st_mode = S_IFDIR|0755; 5920f66f451Sopenharmony_ci dtb->st.st_ctime = dtb->st.st_mtime = time(NULL); 5930f66f451Sopenharmony_ci dtb->child = dti; 5940f66f451Sopenharmony_ci 5950f66f451Sopenharmony_ci // Figure out how much space is used by preset files 5960f66f451Sopenharmony_ci length = check_treesize(dtb, &(dtb->st.st_size)); 5970f66f451Sopenharmony_ci check_treelinks(dtb); 5980f66f451Sopenharmony_ci 5990f66f451Sopenharmony_ci // Figure out how many total inodes we need. 6000f66f451Sopenharmony_ci 6010f66f451Sopenharmony_ci if (!TT.inodes) { 6020f66f451Sopenharmony_ci if (!TT.bytes_per_inode) TT.bytes_per_inode = 8192; 6030f66f451Sopenharmony_ci TT.inodes = (TT.blocks * (uint64_t)TT.blocksize) / TT.bytes_per_inode; 6040f66f451Sopenharmony_ci } 6050f66f451Sopenharmony_ci 6060f66f451Sopenharmony_ci // If we're generating a filesystem and have no idea how many blocks it 6070f66f451Sopenharmony_ci // needs, start with a minimal guess, find the overhead of that many 6080f66f451Sopenharmony_ci // groups, and loop until this is enough groups to store this many blocks. 6090f66f451Sopenharmony_ci if (!TT.blocks) TT.groups = (TT.treeblocks/TT.blockbits)+1; 6100f66f451Sopenharmony_ci else TT.groups = div_round_up(TT.blocks, TT.blockbits); 6110f66f451Sopenharmony_ci 6120f66f451Sopenharmony_ci for (;;) { 6130f66f451Sopenharmony_ci temp = TT.treeblocks; 6140f66f451Sopenharmony_ci 6150f66f451Sopenharmony_ci for (i = 0; i<TT.groups; i++) temp += group_overhead(i); 6160f66f451Sopenharmony_ci 6170f66f451Sopenharmony_ci if (TT.blocks) { 6180f66f451Sopenharmony_ci if (TT.blocks < temp) error_exit("Not enough space.\n"); 6190f66f451Sopenharmony_ci break; 6200f66f451Sopenharmony_ci } 6210f66f451Sopenharmony_ci if (temp <= TT.groups * TT.blockbits) { 6220f66f451Sopenharmony_ci TT.blocks = temp; 6230f66f451Sopenharmony_ci break; 6240f66f451Sopenharmony_ci } 6250f66f451Sopenharmony_ci TT.groups++; 6260f66f451Sopenharmony_ci } 6270f66f451Sopenharmony_ci TT.freeblocks = TT.blocks - temp; 6280f66f451Sopenharmony_ci 6290f66f451Sopenharmony_ci // Now we know all the TT data, initialize superblock structure. 6300f66f451Sopenharmony_ci 6310f66f451Sopenharmony_ci init_superblock(&sb); 6320f66f451Sopenharmony_ci 6330f66f451Sopenharmony_ci // Start writing. Skip the first 1k to avoid the boot sector (if any). 6340f66f451Sopenharmony_ci put_zeroes(1024); 6350f66f451Sopenharmony_ci 6360f66f451Sopenharmony_ci // Loop through block groups, write out each one. 6370f66f451Sopenharmony_ci dtbblk = usedblocks = usedinodes = 0; 6380f66f451Sopenharmony_ci for (i=0; i<TT.groups; i++) { 6390f66f451Sopenharmony_ci struct ext2_inode *in = (struct ext2_inode *)toybuf; 6400f66f451Sopenharmony_ci uint32_t start, itable, used, end; 6410f66f451Sopenharmony_ci int j, slot; 6420f66f451Sopenharmony_ci 6430f66f451Sopenharmony_ci // Where does this group end? 6440f66f451Sopenharmony_ci end = TT.blockbits; 6450f66f451Sopenharmony_ci if ((i+1)*TT.blockbits > TT.blocks) end = TT.blocks & (TT.blockbits-1); 6460f66f451Sopenharmony_ci 6470f66f451Sopenharmony_ci // Blocks used by inode table 6480f66f451Sopenharmony_ci itable = (TT.inodespg*sizeof(struct ext2_inode))/TT.blocksize; 6490f66f451Sopenharmony_ci 6500f66f451Sopenharmony_ci // If a superblock goes here, write it out. 6510f66f451Sopenharmony_ci start = group_superblock_overhead(i); 6520f66f451Sopenharmony_ci if (start) { 6530f66f451Sopenharmony_ci struct ext2_group *bg = (struct ext2_group *)toybuf; 6540f66f451Sopenharmony_ci int treeblocks = TT.treeblocks, treeinodes = TT.treeinodes; 6550f66f451Sopenharmony_ci 6560f66f451Sopenharmony_ci sb.block_group_nr = SWAP_LE16(i); 6570f66f451Sopenharmony_ci 6580f66f451Sopenharmony_ci // Write superblock and pad it up to block size 6590f66f451Sopenharmony_ci xwrite(TT.fsfd, &sb, sizeof(struct ext2_superblock)); 6600f66f451Sopenharmony_ci temp = TT.blocksize - sizeof(struct ext2_superblock); 6610f66f451Sopenharmony_ci if (!i && TT.blocksize > 1024) temp -= 1024; 6620f66f451Sopenharmony_ci memset(toybuf, 0, TT.blocksize); 6630f66f451Sopenharmony_ci xwrite(TT.fsfd, toybuf, temp); 6640f66f451Sopenharmony_ci 6650f66f451Sopenharmony_ci // Loop through groups to write group descriptor table. 6660f66f451Sopenharmony_ci for(j=0; j<TT.groups; j++) { 6670f66f451Sopenharmony_ci 6680f66f451Sopenharmony_ci // Figure out what sector this group starts in. 6690f66f451Sopenharmony_ci used = group_superblock_overhead(j); 6700f66f451Sopenharmony_ci 6710f66f451Sopenharmony_ci // Find next array slot in this block (flush block if full). 6720f66f451Sopenharmony_ci slot = j % (TT.blocksize/sizeof(struct ext2_group)); 6730f66f451Sopenharmony_ci if (!slot) { 6740f66f451Sopenharmony_ci if (j) xwrite(TT.fsfd, bg, TT.blocksize); 6750f66f451Sopenharmony_ci memset(bg, 0, TT.blocksize); 6760f66f451Sopenharmony_ci } 6770f66f451Sopenharmony_ci 6780f66f451Sopenharmony_ci // How many free inodes in this group? 6790f66f451Sopenharmony_ci temp = TT.inodespg; 6800f66f451Sopenharmony_ci if (!i) temp -= INODES_RESERVED; 6810f66f451Sopenharmony_ci if (temp > treeinodes) { 6820f66f451Sopenharmony_ci treeinodes -= temp; 6830f66f451Sopenharmony_ci temp = 0; 6840f66f451Sopenharmony_ci } else { 6850f66f451Sopenharmony_ci temp -= treeinodes; 6860f66f451Sopenharmony_ci treeinodes = 0; 6870f66f451Sopenharmony_ci } 6880f66f451Sopenharmony_ci bg[slot].free_inodes_count = SWAP_LE16(temp); 6890f66f451Sopenharmony_ci 6900f66f451Sopenharmony_ci // How many free blocks in this group? 6910f66f451Sopenharmony_ci temp = TT.inodespg/(TT.blocksize/sizeof(struct ext2_inode)) + 2; 6920f66f451Sopenharmony_ci temp = end-used-temp; 6930f66f451Sopenharmony_ci if (temp > treeblocks) { 6940f66f451Sopenharmony_ci treeblocks -= temp; 6950f66f451Sopenharmony_ci temp = 0; 6960f66f451Sopenharmony_ci } else { 6970f66f451Sopenharmony_ci temp -= treeblocks; 6980f66f451Sopenharmony_ci treeblocks = 0; 6990f66f451Sopenharmony_ci } 7000f66f451Sopenharmony_ci bg[slot].free_blocks_count = SWAP_LE32(temp); 7010f66f451Sopenharmony_ci 7020f66f451Sopenharmony_ci // Fill out rest of group structure 7030f66f451Sopenharmony_ci used += j*TT.blockbits; 7040f66f451Sopenharmony_ci bg[slot].block_bitmap = SWAP_LE32(used++); 7050f66f451Sopenharmony_ci bg[slot].inode_bitmap = SWAP_LE32(used++); 7060f66f451Sopenharmony_ci bg[slot].inode_table = SWAP_LE32(used); 7070f66f451Sopenharmony_ci bg[slot].used_dirs_count = 0; // (TODO) 7080f66f451Sopenharmony_ci } 7090f66f451Sopenharmony_ci xwrite(TT.fsfd, bg, TT.blocksize); 7100f66f451Sopenharmony_ci } 7110f66f451Sopenharmony_ci 7120f66f451Sopenharmony_ci // Now write out stuff that every block group has. 7130f66f451Sopenharmony_ci 7140f66f451Sopenharmony_ci // Write block usage bitmap 7150f66f451Sopenharmony_ci 7160f66f451Sopenharmony_ci start += 2 + itable; 7170f66f451Sopenharmony_ci memset(toybuf, 0, TT.blocksize); 7180f66f451Sopenharmony_ci bits_set(toybuf, 0, start); 7190f66f451Sopenharmony_ci bits_set(toybuf, end, TT.blockbits-end); 7200f66f451Sopenharmony_ci temp = TT.treeblocks - usedblocks; 7210f66f451Sopenharmony_ci if (temp) { 7220f66f451Sopenharmony_ci if (end-start > temp) temp = end-start; 7230f66f451Sopenharmony_ci bits_set(toybuf, start, temp); 7240f66f451Sopenharmony_ci } 7250f66f451Sopenharmony_ci xwrite(TT.fsfd, toybuf, TT.blocksize); 7260f66f451Sopenharmony_ci 7270f66f451Sopenharmony_ci // Write inode bitmap 7280f66f451Sopenharmony_ci memset(toybuf, 0, TT.blocksize); 7290f66f451Sopenharmony_ci j = 0; 7300f66f451Sopenharmony_ci if (!i) bits_set(toybuf, 0, j = INODES_RESERVED); 7310f66f451Sopenharmony_ci bits_set(toybuf, TT.inodespg, slot = TT.blockbits-TT.inodespg); 7320f66f451Sopenharmony_ci temp = TT.treeinodes - usedinodes; 7330f66f451Sopenharmony_ci if (temp) { 7340f66f451Sopenharmony_ci if (slot-j > temp) temp = slot-j; 7350f66f451Sopenharmony_ci bits_set(toybuf, j, temp); 7360f66f451Sopenharmony_ci } 7370f66f451Sopenharmony_ci xwrite(TT.fsfd, toybuf, TT.blocksize); 7380f66f451Sopenharmony_ci 7390f66f451Sopenharmony_ci // Write inode table for this group (TODO) 7400f66f451Sopenharmony_ci for (j = 0; j<TT.inodespg; j++) { 7410f66f451Sopenharmony_ci slot = j % (TT.blocksize/sizeof(struct ext2_inode)); 7420f66f451Sopenharmony_ci if (!slot) { 7430f66f451Sopenharmony_ci if (j) xwrite(TT.fsfd, in, TT.blocksize); 7440f66f451Sopenharmony_ci memset(in, 0, TT.blocksize); 7450f66f451Sopenharmony_ci } 7460f66f451Sopenharmony_ci if (!i && j<INODES_RESERVED) { 7470f66f451Sopenharmony_ci // Write root inode 7480f66f451Sopenharmony_ci if (j == 2) fill_inode(in+slot, dtb); 7490f66f451Sopenharmony_ci } else if (dti) { 7500f66f451Sopenharmony_ci fill_inode(in+slot, dti); 7510f66f451Sopenharmony_ci dti = treenext(dti); 7520f66f451Sopenharmony_ci } 7530f66f451Sopenharmony_ci } 7540f66f451Sopenharmony_ci xwrite(TT.fsfd, in, TT.blocksize); 7550f66f451Sopenharmony_ci 7560f66f451Sopenharmony_ci while (dtb) { 7570f66f451Sopenharmony_ci // TODO write index data block 7580f66f451Sopenharmony_ci // TODO write root directory data block 7590f66f451Sopenharmony_ci // TODO write directory data block 7600f66f451Sopenharmony_ci // TODO write file data block 7610f66f451Sopenharmony_ci put_zeroes(TT.blocksize); 7620f66f451Sopenharmony_ci start++; 7630f66f451Sopenharmony_ci if (start == end) break; 7640f66f451Sopenharmony_ci } 7650f66f451Sopenharmony_ci // Write data blocks (TODO) 7660f66f451Sopenharmony_ci put_zeroes((end-start) * TT.blocksize); 7670f66f451Sopenharmony_ci } 7680f66f451Sopenharmony_ci} 769