summaryrefslogtreecommitdiffhomepage
path: root/pkg/sentry/fs/ext/regular_file.go
diff options
context:
space:
mode:
authorAyush Ranjan <ayushranjan@google.com>2019-07-29 18:32:45 -0700
committergVisor bot <gvisor-bot@google.com>2019-07-29 18:33:55 -0700
commitb765eb45894ea426d2c6d167b6ceb662db6ff4d2 (patch)
tree1dada0a548d86e6b34b01717a9f116a652d396da /pkg/sentry/fs/ext/regular_file.go
parenta3e9031e665e5707ff1d181a577a808ff6d67452 (diff)
ext: inode implementations.
PiperOrigin-RevId: 260624470
Diffstat (limited to 'pkg/sentry/fs/ext/regular_file.go')
-rw-r--r--pkg/sentry/fs/ext/regular_file.go90
1 files changed, 90 insertions, 0 deletions
diff --git a/pkg/sentry/fs/ext/regular_file.go b/pkg/sentry/fs/ext/regular_file.go
new file mode 100644
index 000000000..9bf39acfe
--- /dev/null
+++ b/pkg/sentry/fs/ext/regular_file.go
@@ -0,0 +1,90 @@
+// Copyright 2019 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 ext
+
+import (
+ "io"
+)
+
+// fileReader is used to abstact away the complexity of how the file data is
+// stored under the hood. Provides a method to get a file reader which can be
+// used to read file data without worrying about how it is organized on disk.
+type fileReader interface {
+
+ // getFileReader returns a Reader implementation which can be used to read a
+ // file. It abstracts away the complexity of how the file is actually
+ // organized on disk. The reader is initialized with the passed offset.
+ //
+ // This reader is not meant to be retained across Read operations as it needs
+ // to be reinitialized with the correct offset for every Read.
+ //
+ // Precondition: Must hold the mutex of the filesystem containing dev while
+ // using the Reader.
+ getFileReader(dev io.ReadSeeker, blkSize uint64, offset uint64) io.Reader
+}
+
+// regularFile represents a regular file's inode. This too follows the
+// inheritance pattern prevelant in the vfs layer described in
+// pkg/sentry/vfs/README.md.
+type regularFile struct {
+ inode inode
+
+ // This is immutable. The first field of fileReader implementations must be
+ // regularFile to ensure temporality.
+ impl fileReader
+}
+
+// newRegularFile is the regularFile constructor. It figures out what kind of
+// file this is and initializes the fileReader.
+//
+// Preconditions: Must hold the mutex of the filesystem containing dev.
+func newRegularFile(dev io.ReadSeeker, blkSize uint64, inode inode) (*regularFile, error) {
+ regFile := regularFile{
+ inode: inode,
+ }
+
+ inodeFlags := inode.diskInode.Flags()
+
+ if inodeFlags.Extents {
+ file, err := newExtentFile(dev, blkSize, regFile)
+ if err != nil {
+ return nil, err
+ }
+
+ file.regFile.inode.impl = &file.regFile
+ return &file.regFile, nil
+ }
+
+ if inodeFlags.Inline {
+ if inode.diskInode.Size() > 60 {
+ panic("ext fs: inline file larger than 60 bytes")
+ }
+
+ file := newInlineFile(regFile)
+ file.regFile.inode.impl = &file.regFile
+ return &file.regFile, nil
+ }
+
+ file, err := newBlockMapFile(blkSize, regFile)
+ if err != nil {
+ return nil, err
+ }
+ file.regFile.inode.impl = &file.regFile
+ return &file.regFile, nil
+}
+
+func (f *regularFile) blksUsed(blkSize uint64) uint64 {
+ return (f.inode.diskInode.Size() + blkSize - 1) / blkSize
+}