summaryrefslogtreecommitdiffhomepage
path: root/pkg/sentry/fsimpl/mqfs/mqfs.go
blob: c2b53c9d0c2bcfe4b231a088804d2fdc64627c5b (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
// Copyright 2021 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 mqfs provides a filesystem implementation to back POSIX message
// queues.
package mqfs

import (
	"fmt"
	"strconv"

	"gvisor.dev/gvisor/pkg/context"
	"gvisor.dev/gvisor/pkg/errors/linuxerr"
	"gvisor.dev/gvisor/pkg/sentry/fsimpl/kernfs"
	"gvisor.dev/gvisor/pkg/sentry/kernel/auth"
	"gvisor.dev/gvisor/pkg/sentry/kernel/ipc"
	"gvisor.dev/gvisor/pkg/sentry/kernel/mq"
	"gvisor.dev/gvisor/pkg/sentry/vfs"
)

const (
	// Name is the user-visible filesystem name.
	Name                     = "mqueue"
	defaultMaxCachedDentries = uint64(1000)
)

// FilesystemType implements vfs.FilesystemType.
//
// +stateify savable
type FilesystemType struct{}

// Name implements vfs.FilesystemType.Name.
func (FilesystemType) Name() string {
	return Name
}

// Release implements vfs.FilesystemType.Release.
func (FilesystemType) Release(ctx context.Context) {}

// GetFilesystem implements vfs.FilesystemType.GetFilesystem.
func (ft FilesystemType) GetFilesystem(ctx context.Context, vfsObj *vfs.VirtualFilesystem, creds *auth.Credentials, source string, opts vfs.GetFilesystemOptions) (*vfs.Filesystem, *vfs.Dentry, error) {
	// mqfs is initialized only once per ipc namespace. Each ipc namespace has
	// a POSIX message registry with a root dentry, filesystem, and a
	// disconnected mount. We want the fs to be consistent for all processes in
	// the same ipc namespace, so instead of creating a new fs and root dentry,
	// we retreive them using IPCNamespace.PosixQueues and use them.

	i := ipcNamespaceFromContext(ctx)
	if i == nil {
		return nil, nil, fmt.Errorf("mqfs.FilesystemType.GetFilesystem: ipc namespace doesn't exist")
	}
	defer i.DecRef(ctx)

	registry := i.PosixQueues()
	if registry == nil {
		return nil, nil, fmt.Errorf("mqfs.FilesystemType.GetFilesystem: ipc namespace doesn't have a POSIX registry")
	}
	impl := registry.Impl().(*RegistryImpl)

	maxCachedDentries, err := maxCachedDentries(ctx, vfs.GenericParseMountOptions(opts.Data))
	if err != nil {
		return nil, nil, err
	}
	impl.fs.MaxCachedDentries = maxCachedDentries

	impl.fs.VFSFilesystem().IncRef()
	return impl.fs.VFSFilesystem(), impl.root.VFSDentry(), nil
}

// maxCachedDentries checks mopts for dentry_cache_limit. If a value is
// specified, parse it into uint64 and return it. Otherwise, return the default
// value. An error is returned if a value is found but can't be parsed.
func maxCachedDentries(ctx context.Context, mopts map[string]string) (_ uint64, err error) {
	max := defaultMaxCachedDentries
	if str, ok := mopts["dentry_cache_limit"]; ok {
		delete(mopts, "dentry_cache_limit")
		max, err = strconv.ParseUint(str, 10, 64)
		if err != nil {
			ctx.Warningf("mqfs.FilesystemType.GetFilesystem: invalid dentry cache limit: dentry_cache_limit=%s", str)
			return 0, linuxerr.EINVAL
		}
	}
	return max, nil
}

// filesystem implements kernfs.Filesystem.
//
// +stateify savable
type filesystem struct {
	kernfs.Filesystem
	devMinor uint32

	// root is the filesystem's root dentry. Since we take a reference on it in
	// GetFilesystem, we should release it when the fs is released.
	root *kernfs.Dentry
}

// Release implements vfs.FilesystemImpl.Release.
func (fs *filesystem) Release(ctx context.Context) {
	fs.Filesystem.VFSFilesystem().VirtualFilesystem().PutAnonBlockDevMinor(fs.devMinor)
	fs.Filesystem.Release(ctx)
}

// MountOptions implements vfs.FilesystemImpl.MountOptions.
func (fs *filesystem) MountOptions() string {
	return fmt.Sprintf("dentry_cache_limit=%d", fs.MaxCachedDentries)
}

// ipcNamespace defines functions we need from kernel.IPCNamespace. We redefine
// ipcNamespace along with ipcNamespaceFromContext to avoid circular dependency
// with package sentry/kernel.
type ipcNamespace interface {
	// PosixQueues returns a POSIX message queue registry.
	PosixQueues() *mq.Registry

	// DecRef decrements ipcNamespace's number of references.
	DecRef(ctx context.Context)
}

// ipcNamespaceFromContext returns the IPC namespace in which ctx is executing.
// Copied from package sentry/kernel.
func ipcNamespaceFromContext(ctx context.Context) ipcNamespace {
	if v := ctx.Value(ipc.CtxIPCNamespace); v != nil {
		return v.(ipcNamespace)
	}
	return nil
}