summaryrefslogtreecommitdiffhomepage
path: root/pkg/sentry/fs/gofer/handles.go
blob: 0b33e80c309a4e391037ee2e75c98a938aab5408 (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
// Copyright 2018 Google LLC
//
// 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 (
	"io"

	"gvisor.googlesource.com/gvisor/pkg/fd"
	"gvisor.googlesource.com/gvisor/pkg/log"
	"gvisor.googlesource.com/gvisor/pkg/p9"
	"gvisor.googlesource.com/gvisor/pkg/refs"
	"gvisor.googlesource.com/gvisor/pkg/secio"
	"gvisor.googlesource.com/gvisor/pkg/sentry/context"
	"gvisor.googlesource.com/gvisor/pkg/sentry/fs"
	"gvisor.googlesource.com/gvisor/pkg/sentry/safemem"
)

// handles are the open handles of a gofer file. They are reference counted to
// support open handle sharing between files for read only filesystems.
//
// If Host != nil then it will be used exclusively over File.
type handles struct {
	refs.AtomicRefCount

	// File is a p9.File handle. Must not be nil.
	File contextFile

	// Host is an *fd.FD handle. May be nil.
	Host *fd.FD
}

// DecRef drops a reference on handles.
func (h *handles) DecRef() {
	h.DecRefWithDestructor(func() {
		if h.Host != nil {
			if err := h.Host.Close(); err != nil {
				log.Warningf("error closing host file: %v", err)
			}
		}
		// FIXME(b/38173783): Context is not plumbed here.
		if err := h.File.close(context.Background()); err != nil {
			log.Warningf("error closing p9 file: %v", err)
		}
	})
}

func newHandles(ctx context.Context, file contextFile, flags fs.FileFlags) (*handles, error) {
	_, newFile, err := file.walk(ctx, nil)
	if err != nil {
		return nil, err
	}

	var p9flags p9.OpenFlags
	switch {
	case flags.Read && flags.Write:
		p9flags = p9.ReadWrite
	case flags.Read && !flags.Write:
		p9flags = p9.ReadOnly
	case !flags.Read && flags.Write:
		p9flags = p9.WriteOnly
	default:
		panic("impossible fs.FileFlags")
	}

	hostFile, _, _, err := newFile.open(ctx, p9flags)
	if err != nil {
		newFile.close(ctx)
		return nil, err
	}
	h := &handles{
		File: newFile,
		Host: hostFile,
	}
	return h, nil
}

type handleReadWriter struct {
	ctx context.Context
	h   *handles
	off int64
}

func (h *handles) readWriterAt(ctx context.Context, offset int64) *handleReadWriter {
	return &handleReadWriter{ctx, h, offset}
}

// ReadToBlocks implements safemem.Reader.ReadToBlocks.
func (rw *handleReadWriter) ReadToBlocks(dsts safemem.BlockSeq) (uint64, error) {
	var r io.Reader
	if rw.h.Host != nil {
		r = secio.NewOffsetReader(rw.h.Host, rw.off)
	} else {
		r = &p9.ReadWriterFile{File: rw.h.File.file, Offset: uint64(rw.off)}
	}

	rw.ctx.UninterruptibleSleepStart(false)
	defer rw.ctx.UninterruptibleSleepFinish(false)
	n, err := safemem.FromIOReader{r}.ReadToBlocks(dsts)
	rw.off += int64(n)
	return n, err
}

// WriteFromBlocks implements safemem.Writer.WriteFromBlocks.
func (rw *handleReadWriter) WriteFromBlocks(srcs safemem.BlockSeq) (uint64, error) {
	var w io.Writer
	if rw.h.Host != nil {
		w = secio.NewOffsetWriter(rw.h.Host, rw.off)
	} else {
		w = &p9.ReadWriterFile{File: rw.h.File.file, Offset: uint64(rw.off)}
	}

	rw.ctx.UninterruptibleSleepStart(false)
	defer rw.ctx.UninterruptibleSleepFinish(false)
	n, err := safemem.FromIOWriter{w}.WriteFromBlocks(srcs)
	rw.off += int64(n)
	return n, err
}