annotate toys/other/mke2fs.c @ 674:7e846e281e38

New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
author Rob Landley <rob@landley.net>
date Mon, 08 Oct 2012 00:02:30 -0500
parents 6df4ccc0acbe
children 786841fdb1e0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
53
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
1 /* vi: set ts=4:
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
2 *
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
3 * mke2fs.c - Create an ext2 filesystem image.
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
4 *
194
30a6db5a95c2 Add comments about SUSv3 specs (or lack thereof).
Rob Landley <rob@landley.net>
parents: 186
diff changeset
5 * Copyright 2006, 2007 Rob Landley <rob@landley.net>
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
6
236
6b292ee780ab Minor cleanup: move mke2fs options into mke2fs.c.
Rob Landley <rob@landley.net>
parents: 234
diff changeset
7 // Still to go: "E:jJ:L:m:O:"
6b292ee780ab Minor cleanup: move mke2fs options into mke2fs.c.
Rob Landley <rob@landley.net>
parents: 234
diff changeset
8 USE_MKE2FS(NEWTOY(mke2fs, "<1>2g:Fnqm#N#i#b#", TOYFLAG_SBIN))
234
163498bf547b Move NEWTOY() list from end of toylist.h to generated/newtoys.h.
Rob Landley <rob@landley.net>
parents: 233
diff changeset
9
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
10 config MKE2FS
565
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 237
diff changeset
11 bool "mke2fs (unfinished and broken by dirtree changes)"
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
12 default n
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
13 help
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
14 usage: mke2fs [-Fnq] [-b ###] [-N|i ###] [-m ###] device
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
15
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
16 Create an ext2 filesystem on a block device or filesystem image.
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
17
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
18 -F Force to run on a mounted device
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
19 -n Don't write to device
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
20 -q Quiet (no output)
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
21 -b size Block size (1024, 2048, or 4096)
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
22 -N inodes Allocate this many inodes
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
23 -i bytes Allocate one inode for every XXX bytes of device
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
24 -m percent Reserve this percent of filesystem space for root user
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
25
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
26 config MKE2FS_JOURNAL
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
27 bool "Journaling support (ext3)"
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
28 default n
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
29 depends on MKE2FS
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
30 help
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
31 usage: [-j] [-J size=###,device=XXX]
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
32
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
33 -j Create journal (ext3)
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
34 -J Journal options
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
35 size: Number of blocks (1024-102400)
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
36 device: Specify an external journal
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
37
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
38 config MKE2FS_GEN
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
39 bool "Generate (gene2fs)"
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
40 default n
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
41 depends on MKE2FS
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
42 help
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
43 usage: gene2fs [options] device filename
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
44
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
45 The [options] are the same as mke2fs.
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
46
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
47 config MKE2FS_LABEL
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
48 bool "Label support"
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
49 default n
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
50 depends on MKE2FS
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
51 help
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
52 usage: mke2fs [-L label] [-M path] [-o string]
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
53
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
54 -L Volume label
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
55 -M Path to mount point
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
56 -o Created by
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
57
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
58 config MKE2FS_EXTENDED
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
59 bool "Extended options"
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
60 default n
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
61 depends on MKE2FS
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
62 help
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
63 usage: mke2fs [-E stride=###] [-O option[,option]]
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
64
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
65 -E stride= Set RAID stripe size (in blocks)
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
66 -O [opts] Specify fewer ext2 option flags (for old kernels)
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
67 All of these are on by default (as appropriate)
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
68 none Clear default options (all but journaling)
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
69 dir_index Use htree indexes for large directories
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
70 filetype Store file type info in directory entry
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
71 has_journal Set by -j
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
72 journal_dev Set by -J device=XXX
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
73 sparse_super Don't allocate huge numbers of redundant superblocks
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
74 */
53
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
75
674
7e846e281e38 New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
76 #define FOR_mke2fs
53
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
77 #include "toys.h"
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
78
674
7e846e281e38 New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
79 GLOBALS(
237
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
80 // Command line arguments.
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
81 long blocksize;
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
82 long bytes_per_inode;
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
83 long inodes; // Total inodes in filesystem.
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
84 long reserved_percent; // Integer precent of space to reserve for root.
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
85 char *gendir; // Where to read dirtree from.
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
86
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
87 // Internal data.
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
88 struct dirtree *dt; // Tree of files to copy into the new filesystem.
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
89 unsigned treeblocks; // Blocks used by dt
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
90 unsigned treeinodes; // Inodes used by dt
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
91
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
92 unsigned blocks; // Total blocks in the filesystem.
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
93 unsigned freeblocks; // Free blocks in the filesystem.
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
94 unsigned inodespg; // Inodes per group
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
95 unsigned groups; // Total number of block groups.
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
96 unsigned blockbits; // Bits per block. (Also blocks per group.)
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
97
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
98 // For gene2fs
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
99 unsigned nextblock; // Next data block to allocate
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
100 unsigned nextgroup; // Next group we'll be allocating from
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
101 int fsfd; // File descriptor of filesystem (to output to).
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
102
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
103 struct ext2_superblock sb;
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
104 )
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
105
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
106 #define INODES_RESERVED 10
53
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
107
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
108 static uint32_t div_round_up(uint32_t a, uint32_t b)
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
109 {
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
110 uint32_t c = a/b;
53
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
111
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
112 if (a%b) c++;
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
113 return c;
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
114 }
53
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
115
97
4c81e6375719 Add parent pointer to dirtree, more work on mke2fs (populate dirtree, count
Rob Landley <rob@landley.net>
parents: 84
diff changeset
116 // Calculate data blocks plus index blocks needed to hold a file.
99
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
117
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
118 static uint32_t file_blocks_used(uint64_t size, uint32_t *blocklist)
97
4c81e6375719 Add parent pointer to dirtree, more work on mke2fs (populate dirtree, count
Rob Landley <rob@landley.net>
parents: 84
diff changeset
119 {
4c81e6375719 Add parent pointer to dirtree, more work on mke2fs (populate dirtree, count
Rob Landley <rob@landley.net>
parents: 84
diff changeset
120 uint32_t dblocks = (uint32_t)((size+(TT.blocksize-1))/TT.blocksize);
4c81e6375719 Add parent pointer to dirtree, more work on mke2fs (populate dirtree, count
Rob Landley <rob@landley.net>
parents: 84
diff changeset
121 uint32_t idx=TT.blocksize/4, iblocks=0, diblocks=0, tiblocks=0;
4c81e6375719 Add parent pointer to dirtree, more work on mke2fs (populate dirtree, count
Rob Landley <rob@landley.net>
parents: 84
diff changeset
122
143
9cbb323f297f Break out dirtree.c and let it call a function instead of returning the data.
Rob Landley <rob@landley.net>
parents: 112
diff changeset
123 // Fill out index blocks in inode.
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
124
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
125 if (blocklist) {
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
126 int i;
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
127
143
9cbb323f297f Break out dirtree.c and let it call a function instead of returning the data.
Rob Landley <rob@landley.net>
parents: 112
diff changeset
128 // Direct index blocks
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
129 for (i=0; i<13 && i<dblocks; i++) blocklist[i] = i;
143
9cbb323f297f Break out dirtree.c and let it call a function instead of returning the data.
Rob Landley <rob@landley.net>
parents: 112
diff changeset
130 // Singly indirect index blocks
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
131 if (dblocks > 13+idx) blocklist[13] = 13+idx;
143
9cbb323f297f Break out dirtree.c and let it call a function instead of returning the data.
Rob Landley <rob@landley.net>
parents: 112
diff changeset
132 // Doubly indirect index blocks
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
133 idx = 13 + idx + (idx*idx);
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
134 if (dblocks > idx) blocklist[14] = idx;
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
135
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
136 return 0;
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
137 }
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
138
99
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
139 // Account for direct, singly, doubly, and triply indirect index blocks
97
4c81e6375719 Add parent pointer to dirtree, more work on mke2fs (populate dirtree, count
Rob Landley <rob@landley.net>
parents: 84
diff changeset
140
4c81e6375719 Add parent pointer to dirtree, more work on mke2fs (populate dirtree, count
Rob Landley <rob@landley.net>
parents: 84
diff changeset
141 if (dblocks > 12) {
4c81e6375719 Add parent pointer to dirtree, more work on mke2fs (populate dirtree, count
Rob Landley <rob@landley.net>
parents: 84
diff changeset
142 iblocks = ((dblocks-13)/idx)+1;
4c81e6375719 Add parent pointer to dirtree, more work on mke2fs (populate dirtree, count
Rob Landley <rob@landley.net>
parents: 84
diff changeset
143 if (iblocks > 1) {
4c81e6375719 Add parent pointer to dirtree, more work on mke2fs (populate dirtree, count
Rob Landley <rob@landley.net>
parents: 84
diff changeset
144 diblocks = ((iblocks-2)/idx)+1;
4c81e6375719 Add parent pointer to dirtree, more work on mke2fs (populate dirtree, count
Rob Landley <rob@landley.net>
parents: 84
diff changeset
145 if (diblocks > 1)
4c81e6375719 Add parent pointer to dirtree, more work on mke2fs (populate dirtree, count
Rob Landley <rob@landley.net>
parents: 84
diff changeset
146 tiblocks = ((diblocks-2)/idx)+1;
4c81e6375719 Add parent pointer to dirtree, more work on mke2fs (populate dirtree, count
Rob Landley <rob@landley.net>
parents: 84
diff changeset
147 }
4c81e6375719 Add parent pointer to dirtree, more work on mke2fs (populate dirtree, count
Rob Landley <rob@landley.net>
parents: 84
diff changeset
148 }
99
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
149
97
4c81e6375719 Add parent pointer to dirtree, more work on mke2fs (populate dirtree, count
Rob Landley <rob@landley.net>
parents: 84
diff changeset
150 return dblocks + iblocks + diblocks + tiblocks;
4c81e6375719 Add parent pointer to dirtree, more work on mke2fs (populate dirtree, count
Rob Landley <rob@landley.net>
parents: 84
diff changeset
151 }
4c81e6375719 Add parent pointer to dirtree, more work on mke2fs (populate dirtree, count
Rob Landley <rob@landley.net>
parents: 84
diff changeset
152
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
153 // Use the parent pointer to iterate through the tree non-recursively.
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
154 static struct dirtree *treenext(struct dirtree *this)
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
155 {
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
156 while (this && !this->next) this = this->parent;
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
157 if (this) this = this->next;
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
158
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
159 return this;
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
160 }
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
161
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
162 // Recursively calculate the number of blocks used by each inode in the tree.
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
163 // Returns blocks used by this directory, assigns bytes used to *size.
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
164 // Writes total block count to TT.treeblocks and inode count to TT.treeinodes.
99
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
165
237
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
166 static long check_treesize(struct dirtree *that, off_t *size)
99
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
167 {
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
168 long blocks;
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
169
237
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
170 while (that) {
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
171 *size += sizeof(struct ext2_dentry) + strlen(that->name);
99
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
172
237
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
173 if (that->child)
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
174 that->st.st_blocks = check_treesize(that->child, &that->st.st_size);
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
175 else if (S_ISREG(that->st.st_mode)) {
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
176 that->st.st_blocks = file_blocks_used(that->st.st_size, 0);
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
177 TT.treeblocks += that->st.st_blocks;
99
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
178 }
237
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
179 that = that->next;
99
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
180 }
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
181 TT.treeblocks += blocks = file_blocks_used(*size, 0);
99
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
182 TT.treeinodes++;
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
183
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
184 return blocks;
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
185 }
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
186
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
187 // Calculate inode numbers and link counts.
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
188 //
101
ff85a83e7d7e Calculate st_nlink for each node, via a small but n^2 algorithm.
Rob Landley <rob@landley.net>
parents: 99
diff changeset
189 // To do this right I need to copy the tree and sort it, but here's a really
ff85a83e7d7e Calculate st_nlink for each node, via a small but n^2 algorithm.
Rob Landley <rob@landley.net>
parents: 99
diff changeset
190 // ugly n^2 way of dealing with the problem that doesn't scale well to large
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
191 // numbers of files (> 100,000) but can be done in very little code.
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
192 // This rewrites inode numbers to their final values, allocating depth first.
101
ff85a83e7d7e Calculate st_nlink for each node, via a small but n^2 algorithm.
Rob Landley <rob@landley.net>
parents: 99
diff changeset
193
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
194 static void check_treelinks(struct dirtree *tree)
101
ff85a83e7d7e Calculate st_nlink for each node, via a small but n^2 algorithm.
Rob Landley <rob@landley.net>
parents: 99
diff changeset
195 {
237
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
196 struct dirtree *current=tree, *that;
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
197 long inode = INODES_RESERVED;
101
ff85a83e7d7e Calculate st_nlink for each node, via a small but n^2 algorithm.
Rob Landley <rob@landley.net>
parents: 99
diff changeset
198
237
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
199 while (current) {
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
200 ++inode;
104
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
201 // Since we can't hardlink to directories, we know their link count.
237
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
202 if (S_ISDIR(current->st.st_mode)) current->st.st_nlink = 2;
104
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
203 else {
237
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
204 dev_t new = current->st.st_dev;
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
205
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
206 if (!new) continue;
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
207
237
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
208 // Look for other copies of current node
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
209 current->st.st_nlink = 0;
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
210 for (that = tree; that; that = treenext(that)) {
237
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
211 if (current->st.st_ino == that->st.st_ino &&
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
212 current->st.st_dev == that->st.st_dev)
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
213 {
237
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
214 current->st.st_nlink++;
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
215 current->st.st_ino = inode;
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
216 }
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
217 }
104
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
218 }
237
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
219 current->st.st_ino = inode;
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
220 current = treenext(current);
101
ff85a83e7d7e Calculate st_nlink for each node, via a small but n^2 algorithm.
Rob Landley <rob@landley.net>
parents: 99
diff changeset
221 }
ff85a83e7d7e Calculate st_nlink for each node, via a small but n^2 algorithm.
Rob Landley <rob@landley.net>
parents: 99
diff changeset
222 }
ff85a83e7d7e Calculate st_nlink for each node, via a small but n^2 algorithm.
Rob Landley <rob@landley.net>
parents: 99
diff changeset
223
53
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
224 // According to http://www.opengroup.org/onlinepubs/9629399/apdxa.htm
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
225 // we should generate a uuid structure by reading a clock with 100 nanosecond
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
226 // precision, normalizing it to the start of the gregorian calendar in 1582,
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
227 // and looking up our eth0 mac address.
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
228 //
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
229 // On the other hand, we have 128 bits to come up with a unique identifier, of
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
230 // which 6 have a defined value. /dev/urandom it is.
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
231
143
9cbb323f297f Break out dirtree.c and let it call a function instead of returning the data.
Rob Landley <rob@landley.net>
parents: 112
diff changeset
232 static void create_uuid(char *uuid)
53
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
233 {
99
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
234 // Read 128 random bits
53
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
235 int fd = xopen("/dev/urandom", O_RDONLY);
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
236 xreadall(fd, uuid, 16);
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
237 close(fd);
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
238
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
239 // Claim to be a DCE format UUID.
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
240 uuid[6] = (uuid[6] & 0x0F) | 0x40;
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
241 uuid[8] = (uuid[8] & 0x3F) | 0x80;
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
242
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
243 // rfc2518 section 6.4.1 suggests if we're not using a macaddr, we should
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
244 // set bit 1 of the node ID, which is the mac multicast bit. This means we
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
245 // should never collide with anybody actually using a macaddr.
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
246 uuid[11] = uuid[11] | 128;
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
247 }
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
248
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
249 // Calculate inodes per group from total inodes.
104
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
250 static uint32_t get_inodespg(uint32_t inodes)
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
251 {
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
252 uint32_t temp;
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
253
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
254 // Round up to fill complete inode blocks.
104
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
255 temp = (inodes + TT.groups - 1) / TT.groups;
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
256 inodes = TT.blocksize/sizeof(struct ext2_inode);
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
257 return ((temp + inodes - 1)/inodes)*inodes;
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
258 }
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
259
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
260 // Fill out superblock and TT structures.
53
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
261
82
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
262 static void init_superblock(struct ext2_superblock *sb)
81
f95f6fd94eed Move superblock initialization to a function. Trim unused padding from
Rob Landley <rob@landley.net>
parents: 80
diff changeset
263 {
f95f6fd94eed Move superblock initialization to a function. Trim unused padding from
Rob Landley <rob@landley.net>
parents: 80
diff changeset
264 uint32_t temp;
53
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
265
82
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
266 // Set log_block_size and log_frag_size.
76
e6332139adae More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 75
diff changeset
267
82
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
268 for (temp = 0; temp < 4; temp++) if (TT.blocksize == 1024<<temp) break;
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
269 if (temp==4) error_exit("bad blocksize");
76
e6332139adae More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 75
diff changeset
270 sb->log_block_size = sb->log_frag_size = SWAP_LE32(temp);
e6332139adae More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 75
diff changeset
271
80
7a058d60faf1 One more field, minor shrinking, and start on block groups.
Rob Landley <rob@landley.net>
parents: 79
diff changeset
272 // Fill out blocks_count, r_blocks_count, first_data_block
76
e6332139adae More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 75
diff changeset
273
e6332139adae More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 75
diff changeset
274 sb->blocks_count = SWAP_LE32(TT.blocks);
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
275 sb->free_blocks_count = SWAP_LE32(TT.freeblocks);
104
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
276 temp = (TT.blocks * (uint64_t)TT.reserved_percent) / 100;
76
e6332139adae More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 75
diff changeset
277 sb->r_blocks_count = SWAP_LE32(temp);
e6332139adae More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 75
diff changeset
278
84
bdbef4ab4ac6 This creates an almost valid empty ext2 filesystem, except for a few superblock
Rob Landley <rob@landley.net>
parents: 83
diff changeset
279 sb->first_data_block = SWAP_LE32(TT.blocksize == 1024 ? 1 : 0);
80
7a058d60faf1 One more field, minor shrinking, and start on block groups.
Rob Landley <rob@landley.net>
parents: 79
diff changeset
280
76
e6332139adae More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 75
diff changeset
281 // Set blocks_per_group and frags_per_group, which is the size of an
e6332139adae More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 75
diff changeset
282 // allocation bitmap that fits in one block (I.E. how many bits per block)?
e6332139adae More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 75
diff changeset
283
104
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
284 sb->blocks_per_group = sb->frags_per_group = SWAP_LE32(TT.blockbits);
76
e6332139adae More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 75
diff changeset
285
79
1aff4181427b Filling out most of the ext2 superblock correctly now.
Rob Landley <rob@landley.net>
parents: 76
diff changeset
286 // Set inodes_per_group and total inodes_count
99
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
287 sb->inodes_per_group = SWAP_LE32(TT.inodespg);
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
288 sb->inodes_count = SWAP_LE32(TT.inodespg * TT.groups);
79
1aff4181427b Filling out most of the ext2 superblock correctly now.
Rob Landley <rob@landley.net>
parents: 76
diff changeset
289
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
290 // Determine free inodes.
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
291 temp = TT.inodespg*TT.groups - INODES_RESERVED;
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
292 if (temp < TT.treeinodes) error_exit("Not enough inodes.\n");
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
293 sb->free_inodes_count = SWAP_LE32(temp - TT.treeinodes);
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
294
79
1aff4181427b Filling out most of the ext2 superblock correctly now.
Rob Landley <rob@landley.net>
parents: 76
diff changeset
295 // Fill out the rest of the superblock.
76
e6332139adae More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 75
diff changeset
296 sb->max_mnt_count=0xFFFF;
e6332139adae More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 75
diff changeset
297 sb->wtime = sb->lastcheck = sb->mkfs_time = SWAP_LE32(time(NULL));
e6332139adae More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 75
diff changeset
298 sb->magic = SWAP_LE32(0xEF53);
e6332139adae More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 75
diff changeset
299 sb->state = sb->errors = SWAP_LE16(1);
e6332139adae More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 75
diff changeset
300
53
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
301 sb->rev_level = SWAP_LE32(1);
84
bdbef4ab4ac6 This creates an almost valid empty ext2 filesystem, except for a few superblock
Rob Landley <rob@landley.net>
parents: 83
diff changeset
302 sb->first_ino = SWAP_LE32(INODES_RESERVED+1);
81
f95f6fd94eed Move superblock initialization to a function. Trim unused padding from
Rob Landley <rob@landley.net>
parents: 80
diff changeset
303 sb->inode_size = SWAP_LE16(sizeof(struct ext2_inode));
53
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
304 sb->feature_incompat = SWAP_LE32(EXT2_FEATURE_INCOMPAT_FILETYPE);
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
305 sb->feature_ro_compat = SWAP_LE32(EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER);
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
306
143
9cbb323f297f Break out dirtree.c and let it call a function instead of returning the data.
Rob Landley <rob@landley.net>
parents: 112
diff changeset
307 create_uuid(sb->uuid);
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
308
83
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
309 // TODO If we're called as mke3fs or mkfs.ext3, do a journal.
53
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
310
75
89ca591a9236 More random progress on mke2fs. Nothing to see yet.
Rob Landley <rob@landley.net>
parents: 53
diff changeset
311 //if (strchr(toys.which->name,'3'))
101
ff85a83e7d7e Calculate st_nlink for each node, via a small but n^2 algorithm.
Rob Landley <rob@landley.net>
parents: 99
diff changeset
312 // sb->feature_compat |= SWAP_LE32(EXT3_FEATURE_COMPAT_HAS_JOURNAL);
81
f95f6fd94eed Move superblock initialization to a function. Trim unused padding from
Rob Landley <rob@landley.net>
parents: 80
diff changeset
313 }
f95f6fd94eed Move superblock initialization to a function. Trim unused padding from
Rob Landley <rob@landley.net>
parents: 80
diff changeset
314
104
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
315 // Does this group contain a superblock backup (and group descriptor table)?
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
316 static int is_sb_group(uint32_t group)
82
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
317 {
104
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
318 int i;
83
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
319
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
320 // Superblock backups are on groups 0, 1, and powers of 3, 5, and 7.
104
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
321 if(!group || group==1) return 1;
83
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
322 for (i=3; i<9; i+=2) {
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
323 int j = i;
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
324 while (j<group) j*=i;
104
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
325 if (j==group) return 1;
83
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
326 }
104
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
327 return 0;
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
328 }
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
329
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
330
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
331 // Number of blocks used in group by optional superblock/group list backup.
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
332 static int group_superblock_overhead(uint32_t group)
104
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
333 {
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
334 int used;
82
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
335
104
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
336 if (!is_sb_group(group)) return 0;
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
337
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
338 // How many blocks does the group descriptor table take up?
104
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
339 used = TT.groups * sizeof(struct ext2_group);
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
340 used += TT.blocksize - 1;
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
341 used /= TT.blocksize;
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
342 // Plus the superblock itself.
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
343 used++;
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
344 // And a corner case.
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
345 if (!group && TT.blocksize == 1024) used++;
82
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
346
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
347 return used;
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
348 }
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
349
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
350 // Number of blocks used in group to store superblock/group/inode list
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
351 static int group_overhead(uint32_t group)
104
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
352 {
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
353 // Return superblock backup overhead (if any), plus block/inode
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
354 // allocation bitmaps, plus inode tables.
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
355 return group_superblock_overhead(group) + 2 + get_inodespg(TT.inodespg)
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
356 / (TT.blocksize/sizeof(struct ext2_inode));
104
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
357 }
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
358
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
359 // In bitmap "array" set "len" bits starting at position "start" (from 0).
83
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
360 static void bits_set(char *array, int start, int len)
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
361 {
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
362 while(len) {
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
363 if ((start&7) || len<8) {
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
364 array[start/8]|=(1<<(start&7));
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
365 start++;
84
bdbef4ab4ac6 This creates an almost valid empty ext2 filesystem, except for a few superblock
Rob Landley <rob@landley.net>
parents: 83
diff changeset
366 len--;
83
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
367 } else {
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
368 array[start/8]=255;
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
369 start+=8;
84
bdbef4ab4ac6 This creates an almost valid empty ext2 filesystem, except for a few superblock
Rob Landley <rob@landley.net>
parents: 83
diff changeset
370 len-=8;
83
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
371 }
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
372 }
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
373 }
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
374
99
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
375 // Seek past len bytes (to maintain sparse file), or write zeroes if output
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
376 // not seekable
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
377 static void put_zeroes(int len)
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
378 {
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
379 if(-1 == lseek(TT.fsfd, len, SEEK_SET)) {
99
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
380 memset(toybuf, 0, sizeof(toybuf));
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
381 while (len) {
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
382 int out = len > sizeof(toybuf) ? sizeof(toybuf) : len;
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
383 xwrite(TT.fsfd, toybuf, out);
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
384 len -= out;
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
385 }
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
386 }
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
387 }
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
388
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
389 // Fill out an inode structure from struct stat info in dirtree.
237
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
390 static void fill_inode(struct ext2_inode *in, struct dirtree *that)
99
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
391 {
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
392 uint32_t fbu[15];
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
393 int temp;
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
394
237
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
395 file_blocks_used(that->st.st_size, fbu);
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
396
237
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
397 // If that inode needs data blocks allocated to it.
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
398 if (that->st.st_size) {
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
399 int i, group = TT.nextblock/TT.blockbits;
99
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
400
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
401 // TODO: teach this about indirect blocks.
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
402 for (i=0; i<15; i++) {
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
403 // If we just jumped into a new group, skip group overhead blocks.
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
404 while (group >= TT.nextgroup)
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
405 TT.nextblock += group_overhead(TT.nextgroup++);
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
406 }
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
407 }
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
408 // TODO : S_ISREG/DIR/CHR/BLK/FIFO/LNK/SOCK(m)
237
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
409 in->mode = SWAP_LE32(that->st.st_mode);
99
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
410
237
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
411 in->uid = SWAP_LE16(that->st.st_uid & 0xFFFF);
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
412 in->uid_high = SWAP_LE16(that->st.st_uid >> 16);
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
413 in->gid = SWAP_LE16(that->st.st_gid & 0xFFFF);
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
414 in->gid_high = SWAP_LE16(that->st.st_gid >> 16);
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
415 in->size = SWAP_LE32(that->st.st_size & 0xFFFFFFFF);
99
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
416
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
417 // Contortions to make the compiler not generate a warning for x>>32
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
418 // when x is 32 bits. The optimizer should clean this up.
237
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
419 if (sizeof(that->st.st_size) > 4) temp = 32;
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
420 else temp = 0;
237
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
421 if (temp) in->dir_acl = SWAP_LE32(that->st.st_size >> temp);
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
422
237
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
423 in->atime = SWAP_LE32(that->st.st_atime);
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
424 in->ctime = SWAP_LE32(that->st.st_ctime);
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
425 in->mtime = SWAP_LE32(that->st.st_mtime);
99
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
426
237
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
427 in->links_count = SWAP_LE16(that->st.st_nlink);
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
428 in->blocks = SWAP_LE32(that->st.st_blocks);
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
429 // in->faddr
99
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
430 }
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
431
143
9cbb323f297f Break out dirtree.c and let it call a function instead of returning the data.
Rob Landley <rob@landley.net>
parents: 112
diff changeset
432 // Works like an archiver.
9cbb323f297f Break out dirtree.c and let it call a function instead of returning the data.
Rob Landley <rob@landley.net>
parents: 112
diff changeset
433 // The first argument is the name of the file to create. If it already
9cbb323f297f Break out dirtree.c and let it call a function instead of returning the data.
Rob Landley <rob@landley.net>
parents: 112
diff changeset
434 // exists, that size will be used.
9cbb323f297f Break out dirtree.c and let it call a function instead of returning the data.
Rob Landley <rob@landley.net>
parents: 112
diff changeset
435
186
25447caf1b4b Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents: 143
diff changeset
436 void mke2fs_main(void)
81
f95f6fd94eed Move superblock initialization to a function. Trim unused padding from
Rob Landley <rob@landley.net>
parents: 80
diff changeset
437 {
104
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
438 int i, temp;
82
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
439 off_t length;
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
440 uint32_t usedblocks, usedinodes, dtiblk, dtbblk;
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
441 struct dirtree *dti, *dtb;
143
9cbb323f297f Break out dirtree.c and let it call a function instead of returning the data.
Rob Landley <rob@landley.net>
parents: 112
diff changeset
442
81
f95f6fd94eed Move superblock initialization to a function. Trim unused padding from
Rob Landley <rob@landley.net>
parents: 80
diff changeset
443 // Handle command line arguments.
f95f6fd94eed Move superblock initialization to a function. Trim unused padding from
Rob Landley <rob@landley.net>
parents: 80
diff changeset
444
f95f6fd94eed Move superblock initialization to a function. Trim unused padding from
Rob Landley <rob@landley.net>
parents: 80
diff changeset
445 if (toys.optargs[1]) {
f95f6fd94eed Move superblock initialization to a function. Trim unused padding from
Rob Landley <rob@landley.net>
parents: 80
diff changeset
446 sscanf(toys.optargs[1], "%u", &TT.blocks);
f95f6fd94eed Move superblock initialization to a function. Trim unused padding from
Rob Landley <rob@landley.net>
parents: 80
diff changeset
447 temp = O_RDWR|O_CREAT;
f95f6fd94eed Move superblock initialization to a function. Trim unused padding from
Rob Landley <rob@landley.net>
parents: 80
diff changeset
448 } else temp = O_RDWR;
104
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
449 if (!TT.reserved_percent) TT.reserved_percent = 5;
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
450
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
451 // TODO: Check if filesystem is mounted here
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
452
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
453 // For mke?fs, open file. For gene?fs, create file.
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
454 TT.fsfd = xcreate(*toys.optargs, temp, 0777);
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
455
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
456 // Determine appropriate block size and block count from file length.
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
457 // (If no length, default to 4k. They can override it on the cmdline.)
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
458
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
459 length = fdlength(TT.fsfd);
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
460 if (!TT.blocksize) TT.blocksize = (length && length < 1<<29) ? 1024 : 4096;
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
461 TT.blockbits = 8*TT.blocksize;
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
462 if (!TT.blocks) TT.blocks = length/TT.blocksize;
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
463
97
4c81e6375719 Add parent pointer to dirtree, more work on mke2fs (populate dirtree, count
Rob Landley <rob@landley.net>
parents: 84
diff changeset
464 // Collect gene2fs list or lost+found, calculate requirements.
4c81e6375719 Add parent pointer to dirtree, more work on mke2fs (populate dirtree, count
Rob Landley <rob@landley.net>
parents: 84
diff changeset
465
4c81e6375719 Add parent pointer to dirtree, more work on mke2fs (populate dirtree, count
Rob Landley <rob@landley.net>
parents: 84
diff changeset
466 if (TT.gendir) {
4c81e6375719 Add parent pointer to dirtree, more work on mke2fs (populate dirtree, count
Rob Landley <rob@landley.net>
parents: 84
diff changeset
467 strncpy(toybuf, TT.gendir, sizeof(toybuf));
143
9cbb323f297f Break out dirtree.c and let it call a function instead of returning the data.
Rob Landley <rob@landley.net>
parents: 112
diff changeset
468 dti = dirtree_read(toybuf, NULL, NULL);
97
4c81e6375719 Add parent pointer to dirtree, more work on mke2fs (populate dirtree, count
Rob Landley <rob@landley.net>
parents: 84
diff changeset
469 } else {
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
470 dti = xzalloc(sizeof(struct dirtree)+11);
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
471 strcpy(dti->name, "lost+found");
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
472 dti->st.st_mode = S_IFDIR|0755;
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
473 dti->st.st_ctime = dti->st.st_mtime = time(NULL);
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
474 }
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
475
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
476 // Add root directory inode. This is iterated through for when finding
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
477 // blocks, but not when finding inodes. The tree's parent pointers don't
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
478 // point back into this.
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
479
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
480 dtb = xzalloc(sizeof(struct dirtree)+1);
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
481 dtb->st.st_mode = S_IFDIR|0755;
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
482 dtb->st.st_ctime = dtb->st.st_mtime = time(NULL);
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
483 dtb->child = dti;
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
484
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
485 // Figure out how much space is used by preset files
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
486 length = check_treesize(dtb, &(dtb->st.st_size));
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
487 check_treelinks(dtb);
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
488
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
489 // Figure out how many total inodes we need.
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
490
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
491 if (!TT.inodes) {
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
492 if (!TT.bytes_per_inode) TT.bytes_per_inode = 8192;
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
493 TT.inodes = (TT.blocks * (uint64_t)TT.blocksize) / TT.bytes_per_inode;
97
4c81e6375719 Add parent pointer to dirtree, more work on mke2fs (populate dirtree, count
Rob Landley <rob@landley.net>
parents: 84
diff changeset
494 }
4c81e6375719 Add parent pointer to dirtree, more work on mke2fs (populate dirtree, count
Rob Landley <rob@landley.net>
parents: 84
diff changeset
495
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
496 // If we're generating a filesystem and have no idea how many blocks it
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
497 // needs, start with a minimal guess, find the overhead of that many
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
498 // groups, and loop until this is enough groups to store this many blocks.
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
499 if (!TT.blocks) TT.groups = (TT.treeblocks/TT.blockbits)+1;
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
500 else TT.groups = div_round_up(TT.blocks, TT.blockbits);
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
501
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
502 for (;;) {
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
503 temp = TT.treeblocks;
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
504
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
505 for (i = 0; i<TT.groups; i++) temp += group_overhead(i);
104
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
506
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
507 if (TT.blocks) {
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
508 if (TT.blocks < temp) error_exit("Not enough space.\n");
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
509 break;
104
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
510 }
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
511 if (temp <= TT.groups * TT.blockbits) {
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
512 TT.blocks = temp;
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
513 break;
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
514 }
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
515 TT.groups++;
99
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
516 }
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
517 TT.freeblocks = TT.blocks - temp;
104
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
518
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
519 // Now we know all the TT data, initialize superblock structure.
104
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
520
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
521 init_superblock(&TT.sb);
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
522
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
523 // Start writing. Skip the first 1k to avoid the boot sector (if any).
104
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
524 put_zeroes(1024);
101
ff85a83e7d7e Calculate st_nlink for each node, via a small but n^2 algorithm.
Rob Landley <rob@landley.net>
parents: 99
diff changeset
525
104
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
526 // Loop through block groups, write out each one.
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
527 dtiblk = dtbblk = usedblocks = usedinodes = 0;
82
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
528 for (i=0; i<TT.groups; i++) {
83
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
529 struct ext2_inode *in = (struct ext2_inode *)toybuf;
84
bdbef4ab4ac6 This creates an almost valid empty ext2 filesystem, except for a few superblock
Rob Landley <rob@landley.net>
parents: 83
diff changeset
530 uint32_t start, itable, used, end;
83
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
531 int j, slot;
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
532
84
bdbef4ab4ac6 This creates an almost valid empty ext2 filesystem, except for a few superblock
Rob Landley <rob@landley.net>
parents: 83
diff changeset
533 // Where does this group end?
104
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
534 end = TT.blockbits;
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
535 if ((i+1)*TT.blockbits > TT.blocks) end = TT.blocks & (TT.blockbits-1);
84
bdbef4ab4ac6 This creates an almost valid empty ext2 filesystem, except for a few superblock
Rob Landley <rob@landley.net>
parents: 83
diff changeset
536
bdbef4ab4ac6 This creates an almost valid empty ext2 filesystem, except for a few superblock
Rob Landley <rob@landley.net>
parents: 83
diff changeset
537 // Blocks used by inode table
99
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
538 itable = (TT.inodespg*sizeof(struct ext2_inode))/TT.blocksize;
84
bdbef4ab4ac6 This creates an almost valid empty ext2 filesystem, except for a few superblock
Rob Landley <rob@landley.net>
parents: 83
diff changeset
539
82
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
540 // If a superblock goes here, write it out.
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
541 start = group_superblock_overhead(i);
83
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
542 if (start) {
82
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
543 struct ext2_group *bg = (struct ext2_group *)toybuf;
105
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
544 int treeblocks = TT.treeblocks, treeinodes = TT.treeinodes;
82
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
545
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
546 TT.sb.block_group_nr = SWAP_LE16(i);
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
547
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
548 // Write superblock and pad it up to block size
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
549 xwrite(TT.fsfd, &TT.sb, sizeof(struct ext2_superblock));
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
550 temp = TT.blocksize - sizeof(struct ext2_superblock);
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
551 if (!i && TT.blocksize > 1024) temp -= 1024;
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
552 memset(toybuf, 0, TT.blocksize);
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
553 xwrite(TT.fsfd, toybuf, temp);
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
554
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
555 // Loop through groups to write group descriptor table.
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
556 for(j=0; j<TT.groups; j++) {
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
557
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
558 // Figure out what sector this group starts in.
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
559 used = group_superblock_overhead(j);
53
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
560
82
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
561 // Find next array slot in this block (flush block if full).
83
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
562 slot = j % (TT.blocksize/sizeof(struct ext2_group));
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
563 if (!slot) {
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
564 if (j) xwrite(TT.fsfd, bg, TT.blocksize);
82
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
565 memset(bg, 0, TT.blocksize);
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
566 }
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
567
105
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
568 // How many free inodes in this group?
99
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
569 temp = TT.inodespg;
84
bdbef4ab4ac6 This creates an almost valid empty ext2 filesystem, except for a few superblock
Rob Landley <rob@landley.net>
parents: 83
diff changeset
570 if (!i) temp -= INODES_RESERVED;
105
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
571 if (temp > treeinodes) {
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
572 treeinodes -= temp;
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
573 temp = 0;
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
574 } else {
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
575 temp -= treeinodes;
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
576 treeinodes = 0;
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
577 }
83
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
578 bg[slot].free_inodes_count = SWAP_LE16(temp);
82
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
579
105
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
580 // How many free blocks in this group?
99
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
581 temp = TT.inodespg/(TT.blocksize/sizeof(struct ext2_inode)) + 2;
84
bdbef4ab4ac6 This creates an almost valid empty ext2 filesystem, except for a few superblock
Rob Landley <rob@landley.net>
parents: 83
diff changeset
582 temp = end-used-temp;
105
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
583 if (temp > treeblocks) {
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
584 treeblocks -= temp;
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
585 temp = 0;
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
586 } else {
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
587 temp -= treeblocks;
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
588 treeblocks = 0;
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
589 }
83
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
590 bg[slot].free_blocks_count = SWAP_LE32(temp);
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
591
105
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
592 // Fill out rest of group structure
104
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
593 used += j*TT.blockbits;
83
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
594 bg[slot].block_bitmap = SWAP_LE32(used++);
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
595 bg[slot].inode_bitmap = SWAP_LE32(used++);
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
596 bg[slot].inode_table = SWAP_LE32(used);
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
597 bg[slot].used_dirs_count = 0; // (TODO)
82
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
598 }
83
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
599 xwrite(TT.fsfd, bg, TT.blocksize);
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
600 }
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
601
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
602 // Now write out stuff that every block group has.
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
603
105
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
604 // Write block usage bitmap
83
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
605
105
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
606 start += 2 + itable;
83
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
607 memset(toybuf, 0, TT.blocksize);
105
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
608 bits_set(toybuf, 0, start);
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
609 bits_set(toybuf, end, TT.blockbits-end);
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
610 temp = TT.treeblocks - usedblocks;
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
611 if (temp) {
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
612 if (end-start > temp) temp = end-start;
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
613 bits_set(toybuf, start, temp);
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
614 }
83
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
615 xwrite(TT.fsfd, toybuf, TT.blocksize);
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
616
105
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
617 // Write inode bitmap
83
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
618 memset(toybuf, 0, TT.blocksize);
105
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
619 j = 0;
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
620 if (!i) bits_set(toybuf, 0, j = INODES_RESERVED);
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
621 bits_set(toybuf, TT.inodespg, slot = TT.blockbits-TT.inodespg);
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
622 temp = TT.treeinodes - usedinodes;
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
623 if (temp) {
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
624 if (slot-j > temp) temp = slot-j;
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
625 bits_set(toybuf, j, temp);
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
626 }
83
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
627 xwrite(TT.fsfd, toybuf, TT.blocksize);
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
628
105
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
629 // Write inode table for this group (TODO)
99
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
630 for (j = 0; j<TT.inodespg; j++) {
83
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
631 slot = j % (TT.blocksize/sizeof(struct ext2_inode));
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
632 if (!slot) {
105
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
633 if (j) xwrite(TT.fsfd, in, TT.blocksize);
83
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
634 memset(in, 0, TT.blocksize);
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
635 }
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
636 if (!i && j<INODES_RESERVED) {
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
637 // Write root inode
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
638 if (j == 2) fill_inode(in+slot, dtb);
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
639 } else if (dti) {
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
640 fill_inode(in+slot, dti);
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
641 dti = treenext(dti);
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
642 }
83
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
643 }
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
644 xwrite(TT.fsfd, in, TT.blocksize);
84
bdbef4ab4ac6 This creates an almost valid empty ext2 filesystem, except for a few superblock
Rob Landley <rob@landley.net>
parents: 83
diff changeset
645
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
646 while (dtb) {
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
647 // TODO write index data block
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
648 // TODO write root directory data block
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
649 // TODO write directory data block
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
650 // TODO write file data block
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
651 put_zeroes(TT.blocksize);
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
652 start++;
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
653 if (start == end) break;
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
654 }
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
655 // Write data blocks (TODO)
99
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
656 put_zeroes((end-start) * TT.blocksize);
82
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
657 }
53
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
658 }