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
|
// Copyright 2020 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 overlay
import (
"sync/atomic"
"gvisor.dev/gvisor/pkg/abi/linux"
"gvisor.dev/gvisor/pkg/context"
"gvisor.dev/gvisor/pkg/fspath"
"gvisor.dev/gvisor/pkg/sentry/vfs"
"gvisor.dev/gvisor/pkg/sync"
"gvisor.dev/gvisor/pkg/syserror"
)
func (d *dentry) isDir() bool {
return atomic.LoadUint32(&d.mode)&linux.S_IFMT == linux.S_IFDIR
}
// Preconditions:
// * d.dirMu must be locked.
// * d.isDir().
func (d *dentry) collectWhiteoutsForRmdirLocked(ctx context.Context) (map[string]bool, error) {
vfsObj := d.fs.vfsfs.VirtualFilesystem()
var readdirErr error
whiteouts := make(map[string]bool)
var maybeWhiteouts []string
d.iterLayers(func(layerVD vfs.VirtualDentry, isUpper bool) bool {
layerFD, err := vfsObj.OpenAt(ctx, d.fs.creds, &vfs.PathOperation{
Root: layerVD,
Start: layerVD,
}, &vfs.OpenOptions{
Flags: linux.O_RDONLY | linux.O_DIRECTORY,
})
if err != nil {
readdirErr = err
return false
}
defer layerFD.DecRef(ctx)
// Reuse slice allocated for maybeWhiteouts from a previous layer to
// reduce allocations.
maybeWhiteouts = maybeWhiteouts[:0]
err = layerFD.IterDirents(ctx, vfs.IterDirentsCallbackFunc(func(dirent vfs.Dirent) error {
if dirent.Name == "." || dirent.Name == ".." {
return nil
}
if _, ok := whiteouts[dirent.Name]; ok {
// This file has been whited-out in a previous layer.
return nil
}
if dirent.Type == linux.DT_CHR {
// We have to determine if this is a whiteout, which doesn't
// count against the directory's emptiness. However, we can't
// do so while holding locks held by layerFD.IterDirents().
maybeWhiteouts = append(maybeWhiteouts, dirent.Name)
return nil
}
// Non-whiteout file in the directory prevents rmdir.
return syserror.ENOTEMPTY
}))
if err != nil {
readdirErr = err
return false
}
for _, maybeWhiteoutName := range maybeWhiteouts {
stat, err := vfsObj.StatAt(ctx, d.fs.creds, &vfs.PathOperation{
Root: layerVD,
Start: layerVD,
Path: fspath.Parse(maybeWhiteoutName),
}, &vfs.StatOptions{})
if err != nil {
readdirErr = err
return false
}
if stat.RdevMajor != 0 || stat.RdevMinor != 0 {
// This file is a real character device, not a whiteout.
readdirErr = syserror.ENOTEMPTY
return false
}
whiteouts[maybeWhiteoutName] = isUpper
}
// Continue iteration since we haven't found any non-whiteout files in
// this directory yet.
return true
})
return whiteouts, readdirErr
}
type directoryFD struct {
fileDescription
vfs.DirectoryFileDescriptionDefaultImpl
vfs.DentryMetadataFileDescriptionImpl
mu sync.Mutex
off int64
dirents []vfs.Dirent
}
// Release implements vfs.FileDescriptionImpl.Release.
func (fd *directoryFD) Release(ctx context.Context) {
}
// IterDirents implements vfs.FileDescriptionImpl.IterDirents.
func (fd *directoryFD) IterDirents(ctx context.Context, cb vfs.IterDirentsCallback) error {
fd.mu.Lock()
defer fd.mu.Unlock()
d := fd.dentry()
if fd.dirents == nil {
ds, err := d.getDirents(ctx)
if err != nil {
return err
}
fd.dirents = ds
}
for fd.off < int64(len(fd.dirents)) {
if err := cb.Handle(fd.dirents[fd.off]); err != nil {
return err
}
fd.off++
}
return nil
}
// Preconditions: d.isDir().
func (d *dentry) getDirents(ctx context.Context) ([]vfs.Dirent, error) {
d.fs.renameMu.RLock()
defer d.fs.renameMu.RUnlock()
d.dirMu.Lock()
defer d.dirMu.Unlock()
if d.dirents != nil {
return d.dirents, nil
}
parent := genericParentOrSelf(d)
dirents := []vfs.Dirent{
{
Name: ".",
Type: linux.DT_DIR,
Ino: d.ino,
NextOff: 1,
},
{
Name: "..",
Type: uint8(atomic.LoadUint32(&parent.mode) >> 12),
Ino: parent.ino,
NextOff: 2,
},
}
// Merge dirents from all layers comprising this directory.
vfsObj := d.fs.vfsfs.VirtualFilesystem()
var readdirErr error
prevDirents := make(map[string]struct{})
var maybeWhiteouts []vfs.Dirent
d.iterLayers(func(layerVD vfs.VirtualDentry, isUpper bool) bool {
layerFD, err := vfsObj.OpenAt(ctx, d.fs.creds, &vfs.PathOperation{
Root: layerVD,
Start: layerVD,
}, &vfs.OpenOptions{
Flags: linux.O_RDONLY | linux.O_DIRECTORY,
})
if err != nil {
readdirErr = err
return false
}
defer layerFD.DecRef(ctx)
// Reuse slice allocated for maybeWhiteouts from a previous layer to
// reduce allocations.
maybeWhiteouts = maybeWhiteouts[:0]
err = layerFD.IterDirents(ctx, vfs.IterDirentsCallbackFunc(func(dirent vfs.Dirent) error {
if dirent.Name == "." || dirent.Name == ".." {
return nil
}
if _, ok := prevDirents[dirent.Name]; ok {
// This file is hidden by, or merged with, another file with
// the same name in a previous layer.
return nil
}
prevDirents[dirent.Name] = struct{}{}
if dirent.Type == linux.DT_CHR {
// We can't determine if this file is a whiteout while holding
// locks held by layerFD.IterDirents().
maybeWhiteouts = append(maybeWhiteouts, dirent)
return nil
}
dirent.NextOff = int64(len(dirents) + 1)
dirents = append(dirents, dirent)
return nil
}))
if err != nil {
readdirErr = err
return false
}
for _, dirent := range maybeWhiteouts {
stat, err := vfsObj.StatAt(ctx, d.fs.creds, &vfs.PathOperation{
Root: layerVD,
Start: layerVD,
Path: fspath.Parse(dirent.Name),
}, &vfs.StatOptions{})
if err != nil {
readdirErr = err
return false
}
if stat.RdevMajor == 0 && stat.RdevMinor == 0 {
// This file is a whiteout; don't emit a dirent for it.
continue
}
dirent.NextOff = int64(len(dirents) + 1)
dirents = append(dirents, dirent)
}
return true
})
if readdirErr != nil {
return nil, readdirErr
}
// Cache dirents for future directoryFDs.
d.dirents = dirents
return dirents, nil
}
// Seek implements vfs.FileDescriptionImpl.Seek.
func (fd *directoryFD) Seek(ctx context.Context, offset int64, whence int32) (int64, error) {
fd.mu.Lock()
defer fd.mu.Unlock()
switch whence {
case linux.SEEK_SET:
if offset < 0 {
return 0, syserror.EINVAL
}
if offset == 0 {
// Ensure that the next call to fd.IterDirents() calls
// fd.dentry().getDirents().
fd.dirents = nil
}
fd.off = offset
return fd.off, nil
case linux.SEEK_CUR:
offset += fd.off
if offset < 0 {
return 0, syserror.EINVAL
}
// Don't clear fd.dirents in this case, even if offset == 0.
fd.off = offset
return fd.off, nil
default:
return 0, syserror.EINVAL
}
}
// Sync implements vfs.FileDescriptionImpl.Sync. Forwards sync to the upper
// layer, if there is one. The lower layer doesn't need to sync because it
// never changes.
func (fd *directoryFD) Sync(ctx context.Context) error {
d := fd.dentry()
if !d.isCopiedUp() {
return nil
}
vfsObj := d.fs.vfsfs.VirtualFilesystem()
pop := vfs.PathOperation{
Root: d.upperVD,
Start: d.upperVD,
}
upperFD, err := vfsObj.OpenAt(ctx, d.fs.creds, &pop, &vfs.OpenOptions{Flags: linux.O_RDONLY | linux.O_DIRECTORY})
if err != nil {
return err
}
err = upperFD.Sync(ctx)
upperFD.DecRef(ctx)
return err
}
|