annotate toys/mke2fs.c @ 562:4d802d438983

Match uint64_t with PRIu64 to avoid warnings on 64 bit builds.
author Rob Landley <rob@landley.net>
date Sat, 14 Apr 2012 21:27:00 -0500
parents 7cb15eae1664
children 44abf4d901f3
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>
30a6db5a95c2 Add comments about SUSv3 specs (or lack thereof).
Rob Landley <rob@landley.net>
parents: 186
diff changeset
6 *
30a6db5a95c2 Add comments about SUSv3 specs (or lack thereof).
Rob Landley <rob@landley.net>
parents: 186
diff changeset
7 * Not in SUSv3.
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
8
236
6b292ee780ab Minor cleanup: move mke2fs options into mke2fs.c.
Rob Landley <rob@landley.net>
parents: 234
diff changeset
9 // 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
10 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
11
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 config MKE2FS
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
13 bool "mke2fs"
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
14 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
15 help
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
16 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
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 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
19
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
20 -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
21 -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
22 -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
23 -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
24 -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
25 -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
26 -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
27
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
28 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
29 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
30 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
31 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
32 help
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
33 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
34
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
35 -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
36 -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
37 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
38 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
39
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
40 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
41 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
42 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
43 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
44 help
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
45 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
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 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
48
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
49 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
50 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
51 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
52 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
53 help
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
54 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
55
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
56 -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
57 -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
58 -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
59
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
60 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
61 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
62 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
63 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
64 help
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
65 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
66
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
67 -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
68 -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
69 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
70 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
71 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
72 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
73 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
74 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
75 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
76 */
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
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
78 #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
79
237
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
80 DEFINE_GLOBALS(
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
81 // Command line arguments.
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
82 long blocksize;
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
83 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
84 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
85 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
86 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
87
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
88 // Internal data.
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
89 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
90 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
91 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
92
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
93 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
94 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
95 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
96 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
97 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
98
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
99 // For gene2fs
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
100 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
101 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
102 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
103
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
104 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
105 )
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
106
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
107 // Shortcut to our global data structure, since we use it so much.
237
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
108 #define TT this.mke2fs
76
e6332139adae More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 75
diff changeset
109
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
110 #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
111
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
112 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
113 {
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
114 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
115
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
116 if (a%b) c++;
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
117 return c;
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
118 }
53
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
119
97
4c81e6375719 Add parent pointer to dirtree, more work on mke2fs (populate dirtree, count
Rob Landley <rob@landley.net>
parents: 84
diff changeset
120 // 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
121
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
122 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
123 {
4c81e6375719 Add parent pointer to dirtree, more work on mke2fs (populate dirtree, count
Rob Landley <rob@landley.net>
parents: 84
diff changeset
124 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
125 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
126
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
127 // Fill out index blocks in inode.
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
128
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
129 if (blocklist) {
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
130 int i;
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
131
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 // Direct index blocks
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
133 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
134 // Singly indirect index blocks
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
135 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
136 // Doubly indirect index blocks
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
137 idx = 13 + idx + (idx*idx);
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
138 if (dblocks > idx) blocklist[14] = idx;
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
139
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
140 return 0;
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
141 }
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
142
99
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
143 // 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
144
4c81e6375719 Add parent pointer to dirtree, more work on mke2fs (populate dirtree, count
Rob Landley <rob@landley.net>
parents: 84
diff changeset
145 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
146 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
147 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
148 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
149 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
150 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
151 }
4c81e6375719 Add parent pointer to dirtree, more work on mke2fs (populate dirtree, count
Rob Landley <rob@landley.net>
parents: 84
diff changeset
152 }
99
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
153
97
4c81e6375719 Add parent pointer to dirtree, more work on mke2fs (populate dirtree, count
Rob Landley <rob@landley.net>
parents: 84
diff changeset
154 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
155 }
4c81e6375719 Add parent pointer to dirtree, more work on mke2fs (populate dirtree, count
Rob Landley <rob@landley.net>
parents: 84
diff changeset
156
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
157 // 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
158 static struct dirtree *treenext(struct dirtree *this)
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
159 {
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
160 while (this && !this->next) this = this->parent;
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
161 if (this) this = this->next;
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
162
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
163 return this;
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
164 }
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
165
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
166 // 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
167 // 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
168 // 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
169
237
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
170 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
171 {
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
172 long blocks;
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
173
237
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
174 while (that) {
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
175 *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
176
237
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
177 if (that->child)
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
178 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
179 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
180 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
181 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
182 }
237
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
183 that = that->next;
99
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
184 }
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
185 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
186 TT.treeinodes++;
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
187
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
188 return blocks;
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
189 }
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
190
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
191 // Calculate inode numbers and link counts.
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
192 //
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 // 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
194 // 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
195 // 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
196 // 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
197
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
198 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
199 {
237
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
200 struct dirtree *current=tree, *that;
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
201 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
202
237
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
203 while (current) {
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
204 ++inode;
104
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
205 // 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
206 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
207 else {
237
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
208 dev_t new = current->st.st_dev;
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
209
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
210 if (!new) continue;
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
211
237
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
212 // 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
213 current->st.st_nlink = 0;
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
214 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
215 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
216 current->st.st_dev == that->st.st_dev)
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
217 {
237
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
218 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
219 current->st.st_ino = inode;
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
220 }
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
221 }
104
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
222 }
237
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
223 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
224 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
225 }
ff85a83e7d7e Calculate st_nlink for each node, via a small but n^2 algorithm.
Rob Landley <rob@landley.net>
parents: 99
diff changeset
226 }
ff85a83e7d7e Calculate st_nlink for each node, via a small but n^2 algorithm.
Rob Landley <rob@landley.net>
parents: 99
diff changeset
227
53
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
228 // 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
229 // 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
230 // 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
231 // 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
232 //
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
233 // 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
234 // 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
235
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
236 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
237 {
99
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
238 // 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
239 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
240 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
241 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
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 // 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
244 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
245 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
246
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
247 // 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
248 // 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
249 // 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
250 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
251 }
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
252
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
253 // 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
254 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
255 {
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
256 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
257
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
258 // 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
259 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
260 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
261 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
262 }
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
263
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
264 // 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
265
82
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
266 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
267 {
f95f6fd94eed Move superblock initialization to a function. Trim unused padding from
Rob Landley <rob@landley.net>
parents: 80
diff changeset
268 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
269
82
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
270 // Set log_block_size and log_frag_size.
76
e6332139adae More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 75
diff changeset
271
82
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
272 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
273 if (temp==4) error_exit("bad blocksize");
76
e6332139adae More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 75
diff changeset
274 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
275
80
7a058d60faf1 One more field, minor shrinking, and start on block groups.
Rob Landley <rob@landley.net>
parents: 79
diff changeset
276 // 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
277
e6332139adae More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 75
diff changeset
278 sb->blocks_count = SWAP_LE32(TT.blocks);
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
279 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
280 temp = (TT.blocks * (uint64_t)TT.reserved_percent) / 100;
76
e6332139adae More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 75
diff changeset
281 sb->r_blocks_count = SWAP_LE32(temp);
e6332139adae More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 75
diff changeset
282
84
bdbef4ab4ac6 This creates an almost valid empty ext2 filesystem, except for a few superblock
Rob Landley <rob@landley.net>
parents: 83
diff changeset
283 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
284
76
e6332139adae More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 75
diff changeset
285 // 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
286 // 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
287
104
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
288 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
289
79
1aff4181427b Filling out most of the ext2 superblock correctly now.
Rob Landley <rob@landley.net>
parents: 76
diff changeset
290 // 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
291 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
292 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
293
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
294 // Determine free inodes.
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
295 temp = TT.inodespg*TT.groups - INODES_RESERVED;
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
296 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
297 sb->free_inodes_count = SWAP_LE32(temp - TT.treeinodes);
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
298
79
1aff4181427b Filling out most of the ext2 superblock correctly now.
Rob Landley <rob@landley.net>
parents: 76
diff changeset
299 // Fill out the rest of the superblock.
76
e6332139adae More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 75
diff changeset
300 sb->max_mnt_count=0xFFFF;
e6332139adae More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 75
diff changeset
301 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
302 sb->magic = SWAP_LE32(0xEF53);
e6332139adae More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 75
diff changeset
303 sb->state = sb->errors = SWAP_LE16(1);
e6332139adae More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 75
diff changeset
304
53
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->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
306 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
307 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
308 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
309 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
310
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
311 create_uuid(sb->uuid);
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
312
83
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
313 // 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
314
75
89ca591a9236 More random progress on mke2fs. Nothing to see yet.
Rob Landley <rob@landley.net>
parents: 53
diff changeset
315 //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
316 // 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
317 }
f95f6fd94eed Move superblock initialization to a function. Trim unused padding from
Rob Landley <rob@landley.net>
parents: 80
diff changeset
318
104
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
319 // 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
320 static int is_sb_group(uint32_t group)
82
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
321 {
104
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
322 int i;
83
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
323
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
324 // 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
325 if(!group || group==1) return 1;
83
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
326 for (i=3; i<9; i+=2) {
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
327 int j = i;
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
328 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
329 if (j==group) return 1;
83
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
330 }
104
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
331 return 0;
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
332 }
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
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
335 // 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
336 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
337 {
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
338 int used;
82
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
339
104
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
340 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
341
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
342 // 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
343 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
344 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
345 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
346 // 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
347 used++;
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
348 // 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
349 if (!group && TT.blocksize == 1024) used++;
82
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
350
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
351 return used;
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
352 }
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
353
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
354 // 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
355 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
356 {
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
357 // Return superblock backup overhead (if any), plus block/inode
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
358 // allocation bitmaps, plus inode tables.
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
359 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
360 / (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
361 }
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
362
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
363 // 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
364 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
365 {
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
366 while(len) {
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
367 if ((start&7) || len<8) {
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
368 array[start/8]|=(1<<(start&7));
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
369 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
370 len--;
83
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
371 } else {
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
372 array[start/8]=255;
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
373 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
374 len-=8;
83
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
375 }
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
376 }
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
377 }
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
378
99
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
379 // 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
380 // not seekable
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
381 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
382 {
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
383 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
384 memset(toybuf, 0, sizeof(toybuf));
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
385 while (len) {
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
386 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
387 xwrite(TT.fsfd, toybuf, out);
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
388 len -= out;
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
389 }
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
390 }
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
391 }
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
392
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
393 // 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
394 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
395 {
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
396 uint32_t fbu[15];
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
397 int temp;
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
398
237
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
399 file_blocks_used(that->st.st_size, fbu);
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
400
237
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
401 // 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
402 if (that->st.st_size) {
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
403 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
404
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
405 // TODO: teach this about indirect blocks.
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
406 for (i=0; i<15; i++) {
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
407 // 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
408 while (group >= TT.nextgroup)
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
409 TT.nextblock += group_overhead(TT.nextgroup++);
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
410 }
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
411 }
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
412 // 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
413 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
414
237
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
415 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
416 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
417 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
418 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
419 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
420
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
421 // 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
422 // 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
423 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
424 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
425 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
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->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
428 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
429 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
430
237
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 236
diff changeset
431 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
432 in->blocks = SWAP_LE32(that->st.st_blocks);
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
433 // in->faddr
99
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
434 }
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
435
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
436 // 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
437 // 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
438 // 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
439
186
25447caf1b4b Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents: 143
diff changeset
440 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
441 {
104
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
442 int i, temp;
82
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
443 off_t length;
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
444 uint32_t usedblocks, usedinodes, dtiblk, dtbblk;
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
445 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
446
81
f95f6fd94eed Move superblock initialization to a function. Trim unused padding from
Rob Landley <rob@landley.net>
parents: 80
diff changeset
447 // Handle command line arguments.
f95f6fd94eed Move superblock initialization to a function. Trim unused padding from
Rob Landley <rob@landley.net>
parents: 80
diff changeset
448
f95f6fd94eed Move superblock initialization to a function. Trim unused padding from
Rob Landley <rob@landley.net>
parents: 80
diff changeset
449 if (toys.optargs[1]) {
f95f6fd94eed Move superblock initialization to a function. Trim unused padding from
Rob Landley <rob@landley.net>
parents: 80
diff changeset
450 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
451 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
452 } 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
453 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
454
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
455 // 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
456
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
457 // 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
458 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
459
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
460 // 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
461 // (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
462
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
463 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
464 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
465 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
466 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
467
97
4c81e6375719 Add parent pointer to dirtree, more work on mke2fs (populate dirtree, count
Rob Landley <rob@landley.net>
parents: 84
diff changeset
468 // 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
469
4c81e6375719 Add parent pointer to dirtree, more work on mke2fs (populate dirtree, count
Rob Landley <rob@landley.net>
parents: 84
diff changeset
470 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
471 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
472 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
473 } else {
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
474 dti = xzalloc(sizeof(struct dirtree)+11);
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
475 strcpy(dti->name, "lost+found");
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
476 dti->st.st_mode = S_IFDIR|0755;
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
477 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
478 }
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 // 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
481 // 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
482 // point back into this.
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
483
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
484 dtb = xzalloc(sizeof(struct dirtree)+1);
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
485 dtb->st.st_mode = S_IFDIR|0755;
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
486 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
487 dtb->child = dti;
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 much space is used by preset files
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
490 length = check_treesize(dtb, &(dtb->st.st_size));
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
491 check_treelinks(dtb);
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
492
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
493 // Figure out how many total inodes we need.
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
494
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
495 if (!TT.inodes) {
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
496 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
497 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
498 }
4c81e6375719 Add parent pointer to dirtree, more work on mke2fs (populate dirtree, count
Rob Landley <rob@landley.net>
parents: 84
diff changeset
499
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
500 // 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
501 // 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
502 // 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
503 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
504 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
505
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
506 for (;;) {
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
507 temp = TT.treeblocks;
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
508
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
509 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
510
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
511 if (TT.blocks) {
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
512 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
513 break;
104
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
514 }
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
515 if (temp <= TT.groups * TT.blockbits) {
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
516 TT.blocks = temp;
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
517 break;
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
518 }
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
519 TT.groups++;
99
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
520 }
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
521 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
522
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
523 // 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
524
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
525 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
526
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
527 // 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
528 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
529
104
1c2f6d2e25de Precalculate block and inode usage given a starting file tree. This compiles,
Rob Landley <rob@landley.net>
parents: 101
diff changeset
530 // Loop through block groups, write out each one.
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
531 dtiblk = dtbblk = usedblocks = usedinodes = 0;
82
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
532 for (i=0; i<TT.groups; i++) {
83
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
533 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
534 uint32_t start, itable, used, end;
83
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
535 int j, slot;
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
536
84
bdbef4ab4ac6 This creates an almost valid empty ext2 filesystem, except for a few superblock
Rob Landley <rob@landley.net>
parents: 83
diff changeset
537 // 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
538 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
539 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
540
bdbef4ab4ac6 This creates an almost valid empty ext2 filesystem, except for a few superblock
Rob Landley <rob@landley.net>
parents: 83
diff changeset
541 // 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
542 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
543
82
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
544 // If a superblock goes here, write it out.
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
545 start = group_superblock_overhead(i);
83
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
546 if (start) {
82
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
547 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
548 int treeblocks = TT.treeblocks, treeinodes = TT.treeinodes;
82
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
549
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
550 TT.sb.block_group_nr = SWAP_LE16(i);
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
551
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
552 // Write superblock and pad it up to block size
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
553 xwrite(TT.fsfd, &TT.sb, sizeof(struct ext2_superblock));
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
554 temp = TT.blocksize - sizeof(struct ext2_superblock);
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
555 if (!i && TT.blocksize > 1024) temp -= 1024;
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
556 memset(toybuf, 0, TT.blocksize);
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
557 xwrite(TT.fsfd, toybuf, temp);
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
558
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
559 // Loop through groups to write group descriptor table.
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
560 for(j=0; j<TT.groups; j++) {
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
561
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
562 // Figure out what sector this group starts in.
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
563 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
564
82
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
565 // 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
566 slot = j % (TT.blocksize/sizeof(struct ext2_group));
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
567 if (!slot) {
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
568 if (j) xwrite(TT.fsfd, bg, TT.blocksize);
82
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
569 memset(bg, 0, TT.blocksize);
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
570 }
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
571
105
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
572 // 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
573 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
574 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
575 if (temp > treeinodes) {
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
576 treeinodes -= temp;
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
577 temp = 0;
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
578 } else {
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
579 temp -= treeinodes;
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
580 treeinodes = 0;
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
581 }
83
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
582 bg[slot].free_inodes_count = SWAP_LE16(temp);
82
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
583
105
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
584 // 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
585 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
586 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
587 if (temp > treeblocks) {
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
588 treeblocks -= temp;
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
589 temp = 0;
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
590 } else {
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
591 temp -= treeblocks;
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
592 treeblocks = 0;
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
593 }
83
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
594 bg[slot].free_blocks_count = SWAP_LE32(temp);
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
595
105
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
596 // 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
597 used += j*TT.blockbits;
83
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
598 bg[slot].block_bitmap = SWAP_LE32(used++);
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
599 bg[slot].inode_bitmap = SWAP_LE32(used++);
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
600 bg[slot].inode_table = SWAP_LE32(used);
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
601 bg[slot].used_dirs_count = 0; // (TODO)
82
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
602 }
83
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
603 xwrite(TT.fsfd, bg, TT.blocksize);
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
604 }
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
605
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
606 // 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
607
105
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
608 // Write block usage bitmap
83
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
609
105
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
610 start += 2 + itable;
83
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
611 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
612 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
613 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
614 temp = TT.treeblocks - usedblocks;
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
615 if (temp) {
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
616 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
617 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
618 }
83
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
619 xwrite(TT.fsfd, toybuf, TT.blocksize);
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
620
105
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
621 // Write inode bitmap
83
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
622 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
623 j = 0;
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
624 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
625 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
626 temp = TT.treeinodes - usedinodes;
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
627 if (temp) {
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
628 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
629 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
630 }
83
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
631 xwrite(TT.fsfd, toybuf, TT.blocksize);
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
632
105
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
633 // 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
634 for (j = 0; j<TT.inodespg; j++) {
83
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
635 slot = j % (TT.blocksize/sizeof(struct ext2_inode));
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
636 if (!slot) {
105
c5b6fb1fe3a4 Calculate block and inode usage in group table and bitmaps.
Rob Landley <rob@landley.net>
parents: 104
diff changeset
637 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
638 memset(in, 0, TT.blocksize);
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
639 }
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
640 if (!i && j<INODES_RESERVED) {
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
641 // Write root inode
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
642 if (j == 2) fill_inode(in+slot, dtb);
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
643 } else if (dti) {
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
644 fill_inode(in+slot, dti);
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
645 dti = treenext(dti);
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
646 }
83
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
647 }
4b25264da01b mke2fs: Stopping for dinner. It compiles.
Rob Landley <rob@landley.net>
parents: 82
diff changeset
648 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
649
112
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
650 while (dtb) {
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
651 // TODO write index data block
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
652 // TODO write root directory data block
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
653 // TODO write directory data block
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
654 // TODO write file data block
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
655 put_zeroes(TT.blocksize);
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
656 start++;
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
657 if (start == end) break;
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
658 }
aadd28817955 Next iteration of mke2fs development.
Rob Landley <rob@landley.net>
parents: 105
diff changeset
659 // Write data blocks (TODO)
99
97d717829ff0 More work on mke2fs.c. Random in-progress snapshot...
Rob Landley <rob@landley.net>
parents: 97
diff changeset
660 put_zeroes((end-start) * TT.blocksize);
82
612c8d6c2829 More work on mke2fs.
Rob Landley <rob@landley.net>
parents: 81
diff changeset
661 }
53
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents:
diff changeset
662 }