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
|
// Copyright 2019 The gVisor Authors.
//
// 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
//
// http://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.
package ext
import (
"bytes"
"io"
"math/rand"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"gvisor.dev/gvisor/pkg/binary"
"gvisor.dev/gvisor/pkg/sentry/fs/ext/disklayout"
)
const (
// mockExtentBlkSize is the mock block size used for testing.
// No block has more than 1 header + 4 entries.
mockExtentBlkSize = uint64(64)
)
// The tree described below looks like:
//
// 0.{Head}[Idx][Idx]
// / \
// / \
// 1.{Head}[Ext][Ext] 2.{Head}[Idx]
// / | \
// [Phy] [Phy, Phy] 3.{Head}[Ext]
// |
// [Phy, Phy, Phy]
//
// Legend:
// - Head = ExtentHeader
// - Idx = ExtentIdx
// - Ext = Extent
// - Phy = Physical Block
//
// Please note that ext4 might not construct extent trees looking like this.
// This is purely for testing the tree traversal logic.
var (
node3 = &disklayout.ExtentNode{
Header: disklayout.ExtentHeader{
Magic: disklayout.ExtentMagic,
NumEntries: 1,
MaxEntries: 4,
Height: 0,
},
Entries: []disklayout.ExtentEntryPair{
{
Entry: &disklayout.Extent{
FirstFileBlock: 3,
Length: 3,
StartBlockLo: 6,
},
Node: nil,
},
},
}
node2 = &disklayout.ExtentNode{
Header: disklayout.ExtentHeader{
Magic: disklayout.ExtentMagic,
NumEntries: 1,
MaxEntries: 4,
Height: 1,
},
Entries: []disklayout.ExtentEntryPair{
{
Entry: &disklayout.ExtentIdx{
FirstFileBlock: 3,
ChildBlockLo: 2,
},
Node: node3,
},
},
}
node1 = &disklayout.ExtentNode{
Header: disklayout.ExtentHeader{
Magic: disklayout.ExtentMagic,
NumEntries: 2,
MaxEntries: 4,
Height: 0,
},
Entries: []disklayout.ExtentEntryPair{
{
Entry: &disklayout.Extent{
FirstFileBlock: 0,
Length: 1,
StartBlockLo: 3,
},
Node: nil,
},
{
Entry: &disklayout.Extent{
FirstFileBlock: 1,
Length: 2,
StartBlockLo: 4,
},
Node: nil,
},
},
}
node0 = &disklayout.ExtentNode{
Header: disklayout.ExtentHeader{
Magic: disklayout.ExtentMagic,
NumEntries: 2,
MaxEntries: 4,
Height: 2,
},
Entries: []disklayout.ExtentEntryPair{
{
Entry: &disklayout.ExtentIdx{
FirstFileBlock: 0,
ChildBlockLo: 0,
},
Node: node1,
},
{
Entry: &disklayout.ExtentIdx{
FirstFileBlock: 3,
ChildBlockLo: 1,
},
Node: node2,
},
},
}
)
// TestExtentReader tests extentReader functionality. We should be able to use
// the file reader like any other io.Reader.
func TestExtentReader(t *testing.T) {
type extentReaderTest struct {
name string
from func(uint64) uint64
to func(uint64) uint64
}
tests := []extentReaderTest{
{
name: "read first half",
from: beginning,
to: middle,
},
{
name: "read entire file",
from: beginning,
to: end,
},
{
name: "read second half",
from: middle,
to: end,
},
}
dev, mockExtentFile, want := extentTreeSetUp(t, node0)
size := mockExtentFile.regFile.inode.diskInode.Size()
for _, test := range tests {
from := test.from(size)
to := test.to(size)
fileReader := mockExtentFile.getFileReader(dev, mockExtentBlkSize, from)
got := make([]byte, to-from)
if _, err := io.ReadFull(fileReader, got); err != nil {
t.Errorf("file read failed: %v", err)
}
if diff := cmp.Diff(got, want[from:to]); diff != "" {
t.Errorf("file data mismatch (-want +got):\n%s", diff)
}
}
}
// TestBuildExtentTree tests the extent tree building logic.
func TestBuildExtentTree(t *testing.T) {
_, mockExtentFile, _ := extentTreeSetUp(t, node0)
opt := cmpopts.IgnoreUnexported(disklayout.ExtentIdx{}, disklayout.ExtentHeader{})
if diff := cmp.Diff(&mockExtentFile.root, node0, opt); diff != "" {
t.Errorf("extent tree mismatch (-want +got):\n%s", diff)
}
}
// extentTreeSetUp writes the passed extent tree to a mock disk as an extent
// tree. It also constucts a mock extent file with the same tree built in it.
// It also writes random data file data and returns it.
func extentTreeSetUp(t *testing.T, root *disklayout.ExtentNode) (io.ReadSeeker, *extentFile, []byte) {
t.Helper()
mockDisk := make([]byte, mockExtentBlkSize*10)
mockExtentFile := &extentFile{
regFile: regularFile{
inode: inode{
diskInode: &disklayout.InodeNew{
InodeOld: disklayout.InodeOld{
SizeLo: uint32(mockExtentBlkSize) * getNumPhyBlks(root),
},
},
},
},
}
fileData := writeTree(&mockExtentFile.regFile.inode, mockDisk, node0, mockExtentBlkSize)
r := bytes.NewReader(mockDisk)
if err := mockExtentFile.buildExtTree(r, mockExtentBlkSize); err != nil {
t.Fatalf("inode.buildExtTree failed: %v", err)
}
return r, mockExtentFile, fileData
}
// writeTree writes the tree represented by `root` to the inode and disk. It
// also writes random file data on disk.
func writeTree(in *inode, disk []byte, root *disklayout.ExtentNode, mockExtentBlkSize uint64) []byte {
rootData := binary.Marshal(nil, binary.LittleEndian, root.Header)
for _, ep := range root.Entries {
rootData = binary.Marshal(rootData, binary.LittleEndian, ep.Entry)
}
copy(in.diskInode.Data(), rootData)
var fileData []byte
for _, ep := range root.Entries {
if root.Header.Height == 0 {
fileData = append(fileData, writeRandomFileData(disk, ep.Entry.(*disklayout.Extent))...)
} else {
fileData = append(fileData, writeTreeToDisk(disk, ep)...)
}
}
return fileData
}
// writeTreeToDisk is the recursive step for writeTree which writes the tree
// on the disk only. Also writes random file data on disk.
func writeTreeToDisk(disk []byte, curNode disklayout.ExtentEntryPair) []byte {
nodeData := binary.Marshal(nil, binary.LittleEndian, curNode.Node.Header)
for _, ep := range curNode.Node.Entries {
nodeData = binary.Marshal(nodeData, binary.LittleEndian, ep.Entry)
}
copy(disk[curNode.Entry.PhysicalBlock()*mockExtentBlkSize:], nodeData)
var fileData []byte
for _, ep := range curNode.Node.Entries {
if curNode.Node.Header.Height == 0 {
fileData = append(fileData, writeRandomFileData(disk, ep.Entry.(*disklayout.Extent))...)
} else {
fileData = append(fileData, writeTreeToDisk(disk, ep)...)
}
}
return fileData
}
// writeRandomFileData writes random bytes to the blocks on disk that the
// passed extent points to.
func writeRandomFileData(disk []byte, ex *disklayout.Extent) []byte {
phyExStartBlk := ex.PhysicalBlock()
phyExStartOff := phyExStartBlk * mockExtentBlkSize
phyExEndOff := phyExStartOff + uint64(ex.Length)*mockExtentBlkSize
rand.Read(disk[phyExStartOff:phyExEndOff])
return disk[phyExStartOff:phyExEndOff]
}
// getNumPhyBlks returns the number of physical blocks covered under the node.
func getNumPhyBlks(node *disklayout.ExtentNode) uint32 {
var res uint32
for _, ep := range node.Entries {
if node.Header.Height == 0 {
res += uint32(ep.Entry.(*disklayout.Extent).Length)
} else {
res += getNumPhyBlks(ep.Node)
}
}
return res
}
|