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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
// Copyright 2018 Google Inc.
//
// 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 gofer
import (
"fmt"
"sync"
"gvisor.googlesource.com/gvisor/pkg/p9"
"gvisor.googlesource.com/gvisor/pkg/refs"
"gvisor.googlesource.com/gvisor/pkg/sentry/context"
"gvisor.googlesource.com/gvisor/pkg/sentry/device"
"gvisor.googlesource.com/gvisor/pkg/sentry/fs"
"gvisor.googlesource.com/gvisor/pkg/sentry/fs/fsutil"
"gvisor.googlesource.com/gvisor/pkg/tcpip/transport/unix"
"gvisor.googlesource.com/gvisor/pkg/unet"
)
// +stateify savable
type endpointMaps struct {
// mu protexts the direntMap, the keyMap, and the pathMap below.
mu sync.RWMutex `state:"nosave"`
// direntMap links sockets to their dirents.
// It is filled concurrently with the keyMap and is stored upon save.
// Before saving, this map is used to populate the pathMap.
direntMap map[unix.BoundEndpoint]*fs.Dirent
// keyMap links MultiDeviceKeys (containing inode IDs) to their sockets.
// It is not stored during save because the inode ID may change upon restore.
keyMap map[device.MultiDeviceKey]unix.BoundEndpoint `state:"nosave"`
// pathMap links the sockets to their paths.
// It is filled before saving from the direntMap and is stored upon save.
// Upon restore, this map is used to re-populate the keyMap.
pathMap map[unix.BoundEndpoint]string
}
// add adds the endpoint to the maps.
// A reference is taken on the dirent argument.
//
// Precondition: maps must have been locked with 'lock'.
func (e *endpointMaps) add(key device.MultiDeviceKey, d *fs.Dirent, ep unix.BoundEndpoint) {
e.keyMap[key] = ep
d.IncRef()
e.direntMap[ep] = d
}
// remove deletes the key from the maps.
//
// Precondition: maps must have been locked with 'lock'.
func (e *endpointMaps) remove(key device.MultiDeviceKey) {
endpoint := e.get(key)
delete(e.keyMap, key)
d := e.direntMap[endpoint]
d.DecRef()
delete(e.direntMap, endpoint)
}
// lock blocks other addition and removal operations from happening while
// the backing file is being created or deleted. Returns a function that unlocks
// the endpoint map.
func (e *endpointMaps) lock() func() {
e.mu.Lock()
return func() { e.mu.Unlock() }
}
// get returns the endpoint mapped to the given key.
//
// Precondition: maps must have been locked for reading.
func (e *endpointMaps) get(key device.MultiDeviceKey) unix.BoundEndpoint {
return e.keyMap[key]
}
// session holds state for each 9p session established during sys_mount.
//
// +stateify savable
type session struct {
refs.AtomicRefCount
// conn is a unet.Socket that wraps the readFD/writeFD mount option,
// see fs/gofer/fs.go.
conn *unet.Socket `state:"nosave"`
// msize is the value of the msize mount option, see fs/gofer/fs.go.
msize uint32 `state:"wait"`
// version is the value of the version mount option, see fs/gofer/fs.go.
version string `state:"wait"`
// cachePolicy is the cache policy. It may be either cacheAll or cacheNone.
cachePolicy cachePolicy `state:"wait"`
// aname is the value of the aname mount option, see fs/gofer/fs.go.
aname string `state:"wait"`
// The client associated with this session. This will be initialized lazily.
client *p9.Client `state:"nosave"`
// The p9.File pointing to attachName via the client. This will be initialized
// lazily.
attach contextFile `state:"nosave"`
// Flags provided to the mount.
superBlockFlags fs.MountSourceFlags `state:"wait"`
// connID is a unique identifier for the session connection.
connID string `state:"wait"`
// inodeMappings contains mappings of fs.Inodes associated with this session
// to paths relative to the attach point, where inodeMappings is keyed by
// Inode.StableAttr.InodeID.
inodeMappings map[uint64]string `state:"wait"`
// mounter is the EUID/EGID that mounted this file system.
mounter fs.FileOwner `state:"wait"`
// endpoints is used to map inodes that represent socket files to their
// corresponding endpoint. Socket files are created as regular files in the
// gofer and their presence in this map indicate that they should indeed be
// socket files. This allows unix domain sockets to be used with paths that
// belong to a gofer.
//
// TODO: there are few possible races with someone stat'ing the
// file and another deleting it concurrently, where the file will not be
// reported as socket file.
endpoints *endpointMaps `state:"wait"`
}
// Destroy tears down the session.
func (s *session) Destroy() {
s.conn.Close()
}
// Revalidate implements MountSource.Revalidate.
func (s *session) Revalidate(ctx context.Context, name string, parent, child *fs.Inode) bool {
return s.cachePolicy.revalidate(ctx, name, parent, child)
}
// Keep implements MountSource.Keep.
func (s *session) Keep(d *fs.Dirent) bool {
return s.cachePolicy.keep(d)
}
// ResetInodeMappings implements fs.MountSourceOperations.ResetInodeMappings.
func (s *session) ResetInodeMappings() {
s.inodeMappings = make(map[uint64]string)
}
// SaveInodeMapping implements fs.MountSourceOperations.SaveInodeMapping.
func (s *session) SaveInodeMapping(inode *fs.Inode, path string) {
// This is very unintuitive. We *CANNOT* trust the inode's StableAttrs,
// because overlay copyUp may have changed them out from under us.
// So much for "immutable".
sattr := inode.InodeOperations.(*inodeOperations).fileState.sattr
s.inodeMappings[sattr.InodeID] = path
}
// newInodeOperations creates a new 9p fs.InodeOperations backed by a p9.File and attributes
// (p9.QID, p9.AttrMask, p9.Attr).
//
// Endpoints lock must not be held if socket == false.
func newInodeOperations(ctx context.Context, s *session, file contextFile, qid p9.QID, valid p9.AttrMask, attr p9.Attr, socket bool) (fs.StableAttr, *inodeOperations) {
deviceKey := device.MultiDeviceKey{
Device: attr.RDev,
SecondaryDevice: s.connID,
Inode: qid.Path,
}
sattr := fs.StableAttr{
Type: ntype(attr),
DeviceID: goferDevice.DeviceID(),
InodeID: goferDevice.Map(deviceKey),
BlockSize: bsize(attr),
}
if s.endpoints != nil {
if socket {
sattr.Type = fs.Socket
} else {
// If unix sockets are allowed on this filesystem, check if this file is
// supposed to be a socket file.
unlock := s.endpoints.lock()
if s.endpoints.get(deviceKey) != nil {
sattr.Type = fs.Socket
}
unlock()
}
}
fileState := &inodeFileState{
s: s,
file: file,
sattr: sattr,
key: deviceKey,
}
uattr := unstable(ctx, valid, attr, s.mounter, s.client)
return sattr, &inodeOperations{
fileState: fileState,
cachingInodeOps: fsutil.NewCachingInodeOperations(ctx, fileState, uattr, s.superBlockFlags.ForcePageCache),
}
}
// Root returns the root of a 9p mount. This mount is bound to a 9p server
// based on conn. Otherwise configuration parameters are:
//
// * dev: connection id
// * filesystem: the filesystem backing the mount
// * superBlockFlags: the mount flags describing general mount options
// * opts: parsed 9p mount options
func Root(ctx context.Context, dev string, filesystem fs.Filesystem, superBlockFlags fs.MountSourceFlags, o opts) (*fs.Inode, error) {
// The mounting EUID/EGID will be cached by this file system. This will
// be used to assign ownership to files that the Gofer owns.
mounter := fs.FileOwnerFromContext(ctx)
conn, err := unet.NewSocket(o.fd)
if err != nil {
return nil, err
}
// Construct the session.
s := &session{
connID: dev,
conn: conn,
msize: o.msize,
version: o.version,
cachePolicy: o.policy,
aname: o.aname,
superBlockFlags: superBlockFlags,
mounter: mounter,
}
if o.privateunixsocket {
s.endpoints = newEndpointMaps()
}
// Construct the MountSource with the session and superBlockFlags.
m := fs.NewMountSource(s, filesystem, superBlockFlags)
// Send the Tversion request.
s.client, err = p9.NewClient(s.conn, s.msize, s.version)
if err != nil {
// Drop our reference on the session, it needs to be torn down.
s.DecRef()
return nil, err
}
// Notify that we're about to call the Gofer and block.
ctx.UninterruptibleSleepStart(false)
// Send the Tattach request.
s.attach.file, err = s.client.Attach(s.aname)
ctx.UninterruptibleSleepFinish(false)
if err != nil {
// Same as above.
s.DecRef()
return nil, err
}
qid, valid, attr, err := s.attach.getAttr(ctx, p9.AttrMaskAll())
if err != nil {
s.attach.close(ctx)
// Same as above, but after we execute the Close request.
s.DecRef()
return nil, err
}
sattr, iops := newInodeOperations(ctx, s, s.attach, qid, valid, attr, false)
return fs.NewInode(iops, m, sattr), nil
}
// newEndpointMaps creates a new endpointMaps.
func newEndpointMaps() *endpointMaps {
return &endpointMaps{
direntMap: make(map[unix.BoundEndpoint]*fs.Dirent),
keyMap: make(map[device.MultiDeviceKey]unix.BoundEndpoint),
pathMap: make(map[unix.BoundEndpoint]string),
}
}
// fillKeyMap populates key and dirent maps upon restore from saved
// pathmap.
func (s *session) fillKeyMap(ctx context.Context) error {
unlock := s.endpoints.lock()
defer unlock()
for ep, dirPath := range s.endpoints.pathMap {
_, file, err := s.attach.walk(ctx, splitAbsolutePath(dirPath))
if err != nil {
return fmt.Errorf("error filling endpointmaps, failed to walk to %q: %v", dirPath, err)
}
qid, _, attr, err := file.getAttr(ctx, p9.AttrMaskAll())
if err != nil {
return fmt.Errorf("failed to get file attributes of %s: %v", dirPath, err)
}
key := device.MultiDeviceKey{
Device: attr.RDev,
SecondaryDevice: s.connID,
Inode: qid.Path,
}
s.endpoints.keyMap[key] = ep
}
return nil
}
// fillPathMap populates paths for endpoints from dirents in direntMap
// before save.
func (s *session) fillPathMap() error {
unlock := s.endpoints.lock()
defer unlock()
for ep, dir := range s.endpoints.direntMap {
mountRoot := dir.MountRoot()
defer mountRoot.DecRef()
dirPath, _ := dir.FullName(mountRoot)
if dirPath == "" {
return fmt.Errorf("error getting path from dirent")
}
s.endpoints.pathMap[ep] = dirPath
}
return nil
}
// restoreEndpointMaps recreates and fills the key and dirent maps.
func (s *session) restoreEndpointMaps(ctx context.Context) error {
// When restoring, only need to create the keyMap because the dirent and path
// maps got stored through the save.
s.endpoints.keyMap = make(map[device.MultiDeviceKey]unix.BoundEndpoint)
if err := s.fillKeyMap(ctx); err != nil {
return fmt.Errorf("failed to insert sockets into endpoint map: %v", err)
}
// Re-create pathMap because it can no longer be trusted as socket paths can
// change while process continues to run. Empty pathMap will be re-filled upon
// next save.
s.endpoints.pathMap = make(map[unix.BoundEndpoint]string)
return nil
}
|