summaryrefslogtreecommitdiffhomepage
path: root/pkg/sentry/fs/gofer/inode_state.go
blob: 82d1dd4da97b438806b3a8d406b0f58e7cad5f25 (plain)
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
// 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 (
	"errors"
	"fmt"
	"strings"

	"gvisor.googlesource.com/gvisor/pkg/p9"
	"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/kernel/time"
)

// Some fs implementations may not support atime, ctime, or mtime in getattr.
// The unstable() logic would try to use clock time for them. However, we do not
// want to use such time during S/R as that would cause restore timestamp
// checking failure. Hence a dummy stable-time clock is needed.
//
// Note that application-visible UnstableAttrs either come from CachingInodeOps
// (in which case they are saved), or they are requested from the gofer on each
// stat (for non-caching), so the dummy time only affects the modification
// timestamp check.
type dummyClock struct {
	time.Clock
}

// Now returns a stable dummy time.
func (d *dummyClock) Now() time.Time {
	return time.Time{}
}

type dummyClockContext struct {
	context.Context
}

// Value implements context.Context
func (d *dummyClockContext) Value(key interface{}) interface{} {
	switch key {
	case time.CtxRealtimeClock:
		return &dummyClock{}
	default:
		return d.Context.Value(key)
	}
}

// beforeSave is invoked by stateify.
func (i *inodeFileState) beforeSave() {
	if _, ok := i.s.inodeMappings[i.sattr.InodeID]; !ok {
		panic(fmt.Sprintf("failed to find path for inode number %d. Device %s contains %s", i.sattr.InodeID, i.s.connID, fs.InodeMappings(i.s.inodeMappings)))
	}
	if i.sattr.Type == fs.RegularFile {
		uattr, err := i.unstableAttr(&dummyClockContext{context.Background()})
		if err != nil {
			panic(fmt.Sprintf("failed to get unstable atttribute of %s: %v", i.s.inodeMappings[i.sattr.InodeID], err))
		}
		i.savedUAttr = &uattr
	}
}

// saveLoading is invoked by stateify.
func (i *inodeFileState) saveLoading() struct{} {
	return struct{}{}
}

// loadLoading is invoked by stateify.
func (i *inodeFileState) loadLoading(_ struct{}) {
	i.loading.Lock()
}

// afterLoad is invoked by stateify.
func (i *inodeFileState) afterLoad() {
	load := func() error {
		// See comment on i.loading().
		defer i.loading.Unlock()

		// Manually restore the p9.File.
		name, ok := i.s.inodeMappings[i.sattr.InodeID]
		if !ok {
			// This should be impossible, see assertion in
			// beforeSave.
			return fmt.Errorf("failed to find path for inode number %d. Device %s contains %s", i.sattr.InodeID, i.s.connID, fs.InodeMappings(i.s.inodeMappings))
		}
		// TODO: Context is not plumbed to save/restore.
		ctx := &dummyClockContext{context.Background()}
		var err error
		_, i.file, err = i.s.attach.walk(ctx, strings.Split(name, "/"))
		if err != nil {
			return fmt.Errorf("failed to walk to %q: %v", name, err)
		}

		// Remap the saved inode number into the gofer device using the
		// actual device and actual inode that exists in our new
		// environment.
		qid, mask, attrs, err := i.file.getAttr(ctx, p9.AttrMaskAll())
		if err != nil {
			return fmt.Errorf("failed to get file attributes of %s: %v", name, err)
		}
		if !mask.RDev {
			return fs.ErrCorruption{fmt.Errorf("file %s lacks device", name)}
		}
		i.key = device.MultiDeviceKey{
			Device:          attrs.RDev,
			SecondaryDevice: i.s.connID,
			Inode:           qid.Path,
		}
		if !goferDevice.Load(i.key, i.sattr.InodeID) {
			return fs.ErrCorruption{fmt.Errorf("gofer device %s -> %d conflict in gofer device mappings: %s", i.key, i.sattr.InodeID, goferDevice)}
		}

		if i.sattr.Type == fs.RegularFile {
			env, ok := fs.CurrentRestoreEnvironment()
			if !ok {
				return errors.New("missing restore environment")
			}
			uattr := unstable(ctx, mask, attrs, i.s.mounter, i.s.client)
			if env.ValidateFileSize && uattr.Size != i.savedUAttr.Size {
				return fs.ErrCorruption{fmt.Errorf("file size has changed for %s: previously %d, now %d", i.s.inodeMappings[i.sattr.InodeID], i.savedUAttr.Size, uattr.Size)}
			}
			if env.ValidateFileTimestamp && uattr.ModificationTime != i.savedUAttr.ModificationTime {
				return fs.ErrCorruption{fmt.Errorf("file modification time has changed for %s: previously %v, now %v", i.s.inodeMappings[i.sattr.InodeID], i.savedUAttr.ModificationTime, uattr.ModificationTime)}
			}
			i.savedUAttr = nil
		}

		return nil
	}

	fs.Async(fs.CatchError(load))
}