forked from apple/containerization
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEXT4.swift
More file actions
336 lines (300 loc) · 11.7 KB
/
EXT4.swift
File metadata and controls
336 lines (300 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
//===----------------------------------------------------------------------===//
// Copyright © 2025 Apple Inc. and the Containerization project authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//===----------------------------------------------------------------------===//
import ContainerizationOS
import Foundation
/**
```
# EXT4 Filesystem Layout
The EXT4 filesystem divides the disk into an upfront metadata section followed by several logical groups known as block groups. The
metadata section looks like this:
+--------------------------+
| Boot Sector (1024) |
+--------------------------+
| Superblock (1024) |
+--------------------------+
| Empty (2048) |
+--------------------------+
| |
| [Block Group Descriptors]|
| |
| - Free/used block bitmap |
| - Free/used inode bitmap |
| - Inode table pointer |
| - Other metadata |
| |
+--------------------------+
## Block Groups
Each block group optionally stores a copy of the superblock and group descriptor table for disaster recovery.
The rest of the block group comprises of data blocks. The size of each block group is dynamically decided
while formatting, based on total amount of space available on the disk.
+--------------------------+
| Block Group 0 |
| +------------------+ |
| | Super Block | |
| +------------------+ |
| | Group Desc. | |
| +------------------+ |
| | Data Blocks | |
| | | |
| +------------------+ |
+--------------------------+
| Block Group 1 |
| +------------------+ |
| | Super Block | |
| +------------------+ |
| | Group Desc. | |
| +------------------+ |
| | Data Blocks | |
| | | |
| +------------------+ |
+--------------------------+
| ... |
+--------------------------+
| Block Group N |
| +------------------+ |
| | Super Block | |
| +------------------+ |
| | Group Desc. | |
| +------------------+ |
| | Data Blocks | |
| | | |
| +------------------+ |
+--------------------------+
The descriptor for each block group contain the following information:
- Block Bitmap
- Inode Bitmap
- Pointer to Inode Table
- other metadata such as used block count, num. dirs etc.
### Block Bitmap
A sequence of bits, where each bit represents a block in the block group.
1: In use block
0: Free block
+---------------+---------------+
| Block |
| Bitmap |
+---------------+---------------+
| 1 0 1 0 1 1 0 0 |
+---------------+---------------+
| | | | | | | |
| | | | | | | |
v v v v v v v v
+---+---+---+---+---+---+---+---+
| B | | B | | B | B | | |
+---+---+---+---+---+---+---+---+
Whenever a file is created, free data blocks are identified by using this table.
When it is deleted, the corresponding data blocks are marked as free.
### Inode Bitmap
A sequence of bits, where each bit represents a inode in the block group. Since
inodes per group is a fixed number, this bitmap is made to be of sufficient length
to accommodate that many inodes
1: In use inode
0: Free inode
+---------------+---------------+
| Inode |
| Bitmap |
+---------------+---------------+
| 1 0 1 0 1 1 0 0 |
+---------------+---------------+
| | | | | | | |
| | | | | | | |
v v v v v v v v
+---+---+---+---+---+---+---+---+
| I | | I | | I | I | | |
+---+---+---+---+---+---+---+---+
## Inode table
Inode table provides a mapping from Inode -> Data blocks. In this implementation, inode size is set to 256 bytes.
Inode table uses extents to efficiently describe the mapping.
+-----------------------+
| Inode Table |
+-----------------------+
| Inode | Metadata |
+-------+---------------+
| 1 | permissions |
| | size |
| | user ID |
| | group ID |
| | timestamps |
| | block |
| | blocks count |
+-------+---------------+
| 2 | ... |
+-------+---------------+
| ... | ... |
+-------+---------------+
The length of `block` field in the inode table is 60 bytes. This field contains an extent tree
that holds information about ranges of blocks used by the file. For smaller files, the entire extent
tree can be stored within this field.
+-----------------------+
| Inode |
+-----------------------+
| Metadata |
+-----------------------+
| Extent Tree |
| +-------------------+ |
| | Extent Leaf Node | |
| +-------------------+ |
| | - Start Block | |
| | - Block Count | |
| | - ... | |
| +-------------------+ |
+-----------------------+
For larger files which span across multiple non-contiguous blocks, extent tree's root points to extent
blocks, which in-turn point to the blocks used by the file
+-----------------------+
| Extent Tree |
| +-------------------+ |
| | Extent Root | |
| +-------------------+ |
| | - Pointers to | |
| | Extent Blocks | |
| +-------------------+ |
+-----------------------+
|
v
+-----------------------+
| Extent Block |
+-----------------------+
| +-------------------+ |
| | Extent Leaf Node | |
| +-------------------+ |
| | - Start Block | |
| | - Block Count | |
| | - ... | |
| +-------------------+ |
| +-------------------+ |
| | Extent Leaf Node | |
| +-------------------+ |
| | - Start Block | |
| | - Block Count | |
| | - ... | |
| +-------------------+ |
+-----------------------+
## Directory entries
The data blocks for directory inodes point to a list of directory entrees. Each entry
consists of only a name and inode number. The name and inode number correspond to the
name and inode number of the children of the directory
+-------------------------+
| Directory Entry |
+-------------------------+
| inode | rec_len | name |
+-------------------------+
| 2 | 1 | "." |
+-------------------------+
| Directory Entry |
+-------------------------+
| inode | rec_len | name |
+-------------------------+
| 1 | 2 | ".." |
+-------------------------+
| Directory Entry |
+-------------------------+
| inode | rec_len | name |
+-------------------------+
| 11 | 10 | lost& |
| | | found |
+-------------------------+
More details can be found here https://ext4.wiki.kernel.org/index.php/Ext4_Disk_Layout
```
*/
/// A type for interacting with ext4 file systems.
///
/// The `Ext4` class provides functionality to read the superblock of an existing ext4 block device
/// and format a new block device with the ext4 file system.
///
/// Usage:
/// - To read the superblock of an existing ext4 block device, create an instance of `Ext4` with the
/// path to the block device
/// - To format a new block device with ext4, create an instance of `Ext4.Formatter` with the path to the block
/// device and call the `close()` method.
///
/// Example 1: Read an existing block device
/// ```swift
/// let blockDevice = URL(filePath: "/dev/sdb")
/// // succeeds if a valid ext4 fs is found at path
/// let ext4 = try Ext4(blockDevice: blockDevice)
/// print("Block size: \(ext4.blockSize)")
/// print("Total size: \(ext4.size)")
///
/// // Reading the superblock
/// let superblock = ext4.superblock
/// print("Superblock information:")
/// print("Total blocks: \(superblock.blocksCountLow)")
/// ```
///
/// Example 2: Format a new block device (Refer [`EXT4.Formatter`](x-source-tag://EXT4.Formatter) for more info)
/// ```swift
/// let devicePath = URL(filePath: "/dev/sdc")
/// let formatter = try EXT4.Formatter(devicePath, blockSize: 4096)
/// try formatter.close()
/// ```
public enum EXT4 {
public static let SuperBlockMagic: UInt16 = 0xef53
static let ExtentHeaderMagic: UInt16 = 0xf30a
static let XAttrHeaderMagic: UInt32 = 0xea02_0000
static let DefectiveBlockInode: InodeNumber = 1
static let RootInode: InodeNumber = 2
static let FirstInode: InodeNumber = 11
static let LostAndFoundInode: InodeNumber = 11
static let InodeActualSize: UInt32 = 160 // 160 bytes used by metadata
static let InodeExtraSize: UInt32 = 96 // 96 bytes for inline xattrs
static let InodeSize: UInt32 = UInt32(MemoryLayout<Inode>.size) // 256 bytes. This is the max size of an inode
static let XattrInodeHeaderSize: UInt32 = 4
static let XattrBlockHeaderSize: UInt32 = 32
static let ExtraIsize: UInt16 = UInt16(InodeActualSize) - 128
static let MaxLinks: UInt32 = 65000
static let MaxBlocksPerExtent: UInt32 = 0x8000
static let MaxFileSize: UInt64 = 128.gib()
static let SuperBlockOffset: UInt64 = 1024
}
extension EXT4 {
// `EXT4` errors.
public enum Error: Swift.Error, CustomStringConvertible, Sendable, Equatable {
case notFound(_ path: String)
case couldNotReadSuperBlock(_ path: String, _ offset: UInt64, _ size: Int)
case invalidSuperBlock
case deepExtentsUnimplemented
case invalidExtents
case invalidXattrEntry
case couldNotReadBlock(_ block: UInt32)
case invalidPathEncoding(_ path: String)
case couldNotReadInode(_ inode: UInt32)
case couldNotReadGroup(_ group: UInt32)
public var description: String {
switch self {
case .notFound(let path):
return "file at path \(path) not found"
case .couldNotReadSuperBlock(let path, let offset, let size):
return "could not read \(size) bytes of superblock from \(path) at offset \(offset)"
case .invalidSuperBlock:
return "not a valid EXT4 superblock"
case .deepExtentsUnimplemented:
return "deep extents are not supported"
case .invalidExtents:
return "extents invalid or corrupted"
case .invalidXattrEntry:
return "invalid extended attribute entry"
case .couldNotReadBlock(let block):
return "could not read block \(block)"
case .invalidPathEncoding(let path):
return "path encoding for '\(path)' is invalid, must be ascii or utf8"
case .couldNotReadInode(let inode):
return "could not read inode \(inode)"
case .couldNotReadGroup(let group):
return "could not read group descriptor \(group)"
}
}
}
}