annotate toys/other/mke2fs.c @ 656:6df4ccc0acbe

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