upperstore

package
v0.42.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 5, 2026 License: MIT Imports: 23 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package upperstore holds the durable state of the upper of the macOS merged file system: a redo log of records plus immutable version files. It reaches the disk only through Disk.

Index

Constants

View Source
const (
	PointBeforeAppend       = "before-append"
	PointAfterAppend        = "after-append"
	PointBeforeVersionFsync = "before-version-fsync"
	PointAfterVersionFsync  = "after-version-fsync"
	PointBeforeDirFsync     = "before-dir-fsync"
	PointBeforeSegmentFsync = "before-segment-fsync"
	PointAfterSegmentFsync  = "after-segment-fsync"

	PointAfterCapture            = "after-capture"
	PointBeforeSnapshotRename    = "before-snapshot-rename"
	PointAfterSnapshotRename     = "after-snapshot-rename"
	PointBeforeSnapshotCheck     = "before-snapshot-check"
	PointBeforeEncode            = "before-encode"
	PointBeforeSwap              = "before-swap"
	PointBeforeOldSnapshotDelete = "before-old-snapshot-delete"
	PointBeforeOldSegmentDelete  = "before-old-segment-delete"
	PointBeforeVersionDelete     = "before-version-delete"
	PointAfterCleanup            = "after-cleanup"
	PointReadReferenced          = "read-referenced"
)

Names of the interruption points that the store passes to Disk.Point.

View Source
const (

	// MaxFileSize is the largest file that the store holds, the limit of the shim.
	MaxFileSize = 256 << 20
)
View Source
const (

	// MaxWrite is the most bytes that one write record carries.
	MaxWrite = 1 << 20
)
View Source
const RootUnode = 1

RootUnode is the unode of the root directory.

Variables

View Source
var (
	ErrDirGone  = fmt.Errorf("upperstore: directory removed: %w", fs.ErrNotExist)
	ErrNotDir   = errors.New("upperstore: not a directory")
	ErrNotFile  = errors.New("upperstore: not a regular file")
	ErrNotEmpty = errors.New("upperstore: directory not empty")
	ErrInvalid  = errors.New("upperstore: invalid argument")
	ErrTooBig   = errors.New("upperstore: name, path or value too long")
	ErrCorrupt  = errors.New("upperstore: the journal does not continue from the recovery state")
)
View Source
var ErrClosed = errors.New("upperstore: store is closed")
View Source
var ErrCrashed = errors.New("upperstore: fake disk crashed")

ErrCrashed is the answer of every call of FakeDisk between a crash and Restart.

View Source
var ErrNoXattr = errors.New("upperstore: no such extended attribute")

ErrNoXattr is the answer for an extended attribute that the node lacks.

Functions

func Pending

func Pending(i int) uint64

Pending names the node that step i of a plan makes, for the Parent of a later step and for the arguments of the call that carries the plan.

Types

type Attr

type Attr struct {
	Unode        uint64
	Kind         Kind
	Mode         uint32
	Size         int64
	Atime, Mtime int64
	Ctime        int64
	// AtimeSet, MtimeSet and CtimeSet say that the store recorded the time. An unset time is the time of the base
	// commit, and a recorded zero is the epoch.
	AtimeSet, MtimeSet, CtimeSet bool
	// FileID is the identity that the caller recorded when it made the node; zero when it recorded none.
	FileID uint64
	Origin string
	Target string
	Opaque bool
	// Copied is true when the content is a version file of the store and not the base blob.
	Copied bool
}

Attr is the state of a node. Origin names the base path whose blob is the content, or whose directory the node merges.

type AttrUpdate

type AttrUpdate struct {
	Mode         *uint32
	Size         *int64
	Atime, Mtime *int64
}

AttrUpdate lists the attributes that SetAttr changes. A change of Size to another value cuts or extends.

type Base

type Base interface {
	Blob(origin string) (Blob, error)
}

Base serves the blobs of the base tree to the store.

type Blob

type Blob interface {
	io.ReaderAt
	io.Closer
	Size() int64
}

Blob is one base file.

type CrashMode

type CrashMode int

CrashMode says what a crash of FakeDisk keeps of the state that no flush made durable.

const (
	// KeepAll is a kill of the process: the OS still holds every written byte and entry.
	KeepAll CrashMode = iota
	// DropUnflushed is a power loss that keeps only flushed state.
	DropUnflushed
	// RandomSubset is a power loss that also keeps a seeded random subset of the unflushed state.
	RandomSubset
)

type DirEntry

type DirEntry struct {
	Name     string
	Unode    uint64
	Kind     Kind
	Whiteout bool
}

DirEntry is a name of a directory. A whiteout hides the entry of the base tree.

type Disk

type Disk interface {
	MkdirAll(path string) error
	Mkdir(path string) error
	// Create makes a new empty file and fails when the name exists.
	Create(path string) (File, error)
	Open(path string) (File, error)
	Rename(oldPath, newPath string) error
	Remove(path string) error
	ReadDir(path string) ([]Entry, error)
	// SyncDir makes the entries of a directory durable.
	SyncDir(path string) error
	// Lock takes an exclusive lock on a file that it creates, and fails when another holder has it.
	Lock(path string) (io.Closer, error)
	// Point names a place in the store where a fault can stop it. The real disk answers nil.
	Point(name string) error
}

Disk is every call that the store makes to the file system. A missing name answers an error that matches fs.ErrNotExist, and an existing one fs.ErrExist.

type Entry

type Entry struct {
	Name string
	Dir  bool
}

Entry is one name of a directory.

type FakeDisk

type FakeDisk struct {
	// contains filtered or unexported fields
}

FakeDisk is an in-memory Disk. For each file and directory it keeps the state that a flush made durable apart from the live state, as F_FULLFSYNC does.

func NewFakeDisk

func NewFakeDisk() *FakeDisk

NewFakeDisk returns an empty disk with a root directory.

func (*FakeDisk) Crash

func (d *FakeDisk) Crash(mode CrashMode, seed int64)

Crash stops the disk at once.

func (*FakeDisk) CrashAt

func (d *FakeDisk) CrashAt(n int, mode CrashMode, seed int64)

CrashAt arms a crash at the nth call of Point, or of the torn write inside a WriteAt, counted from the call.

func (*FakeDisk) Create

func (d *FakeDisk) Create(path string) (File, error)

func (*FakeDisk) FailOpen

func (d *FakeDisk) FailOpen(match string, err error)

FailOpen makes Open fail with err for a path that contains match, until it is called with a nil err.

func (*FakeDisk) FailSyncDir

func (d *FakeDisk) FailSyncDir(path string, err error)

FailSyncDir makes SyncDir fail with err for the path, until it is called with a nil err.

func (*FakeDisk) Hits

func (d *FakeDisk) Hits() []string

Hits lists the names of the points that were passed, in order.

func (*FakeDisk) Hook

func (d *FakeDisk) Hook(f func(name string))

Hook sets a function that Point calls, without the disk lock, after it counts a point that did not crash.

func (*FakeDisk) Lock

func (d *FakeDisk) Lock(path string) (io.Closer, error)

func (*FakeDisk) Mkdir

func (d *FakeDisk) Mkdir(path string) error

func (*FakeDisk) MkdirAll

func (d *FakeDisk) MkdirAll(path string) error

func (*FakeDisk) Open

func (d *FakeDisk) Open(path string) (File, error)

func (*FakeDisk) OpenFiles

func (d *FakeDisk) OpenFiles() int

OpenFiles counts the files that were opened or created and not closed.

func (*FakeDisk) Point

func (d *FakeDisk) Point(name string) error

func (*FakeDisk) ReadDir

func (d *FakeDisk) ReadDir(path string) ([]Entry, error)

func (*FakeDisk) Remove

func (d *FakeDisk) Remove(path string) error

func (*FakeDisk) Rename

func (d *FakeDisk) Rename(oldPath, newPath string) error

func (*FakeDisk) Restart

func (d *FakeDisk) Restart()

Restart makes the disk usable after a crash. It frees the locks and invalidates every open file.

func (*FakeDisk) SetFineTear

func (d *FakeDisk) SetFineTear(on bool)

SetFineTear makes a WriteAt stop at more places: inside a record header, at each 512-byte sector of a small write, and one byte before its end. The default is the middle only.

func (*FakeDisk) SyncDir

func (d *FakeDisk) SyncDir(path string) error

type File

type File interface {
	io.ReaderAt
	io.WriterAt
	io.Closer
	Truncate(size int64) error
	Sync() error
	Size() (int64, error)
}

File is an open file. Sync makes the data durable: F_FULLFSYNC on darwin, fsync elsewhere.

type Kind

type Kind uint8

Kind is the type of a node.

const (
	KindFile Kind = iota + 1
	KindDir
	KindSymlink
	KindSpecial
)

type NewNode

type NewNode struct {
	Kind           Kind
	Mode           uint32
	Origin, Target string
	// Opaque makes a new directory hide the base entries below it, in the same operation as its create.
	Opaque bool
	// FromBase says that the node is a promotion of an entry of the base tree. It keeps the base time, and the
	// store records none.
	FromBase bool
	// FileID is recorded with the node and read back by GetAttr.
	FileID uint64
}

NewNode describes the node that Create makes.

type OSDisk

type OSDisk struct{}

OSDisk is the Disk of the operating system.

func (OSDisk) Create

func (OSDisk) Create(path string) (File, error)

func (OSDisk) Lock

func (OSDisk) Lock(path string) (io.Closer, error)

func (OSDisk) Mkdir

func (OSDisk) Mkdir(path string) error

func (OSDisk) MkdirAll

func (OSDisk) MkdirAll(path string) error

func (OSDisk) Open

func (OSDisk) Open(path string) (File, error)

func (OSDisk) Point

func (OSDisk) Point(string) error

func (OSDisk) ReadDir

func (OSDisk) ReadDir(path string) ([]Entry, error)

func (OSDisk) Remove

func (OSDisk) Remove(path string) error

func (OSDisk) Rename

func (OSDisk) Rename(oldPath, newPath string) error

func (OSDisk) SyncDir

func (OSDisk) SyncDir(path string) error

type Options

type Options struct {
	Disk       Disk
	Dir        string
	Base       Base
	BaseCommit string
	// Root is the state directory: Dir and every directory between Dir and Root are made durable at Open.
	// The default is the parent of Dir.
	Root string
	// Boot names this daemon process; it is the first half of the token.
	Boot  string
	Clock clock.Clock
	// SegmentLimit is the size at which the next operation starts a new segment. The default is 64 MiB.
	SegmentLimit int64
	// CheckpointBytes and CheckpointRecords are the journal size and the record count above the recovery
	// snapshot that start a background checkpoint. The defaults are 64 MiB and 200,000.
	CheckpointBytes   int64
	CheckpointRecords int
}

Options configures Open. Disk, Dir, BaseCommit and Boot are required.

type Record

type Record struct {
	Seq     uint64
	More    bool
	Type    Type
	Payload []byte
	// Time is the time of the operation in nanoseconds, or zero when the record carries none.
	Time int64
}

Record is a decoded journal record.

type Snapshot

type Snapshot struct {
	Seq uint64
	// contains filtered or unexported fields
}

Snapshot is a copy of the index at one sequence. A later operation does not change it, so a caller reads it without any lock.

func (*Snapshot) Attr

func (c *Snapshot) Attr(unode uint64) (Attr, bool)

func (*Snapshot) Entries

func (c *Snapshot) Entries(dir uint64) []DirEntry

Entries lists the entries of a directory by name, whiteouts included.

func (*Snapshot) Entry

func (c *Snapshot) Entry(dir uint64, name string) (DirEntry, bool)

Entry finds one name of a directory, whiteouts included.

func (*Snapshot) Xattr

func (c *Snapshot) Xattr(unode uint64, name string) ([]byte, bool)

Xattr returns the value of an extended attribute of a node.

type Step

type Step struct {
	Parent uint64
	Name   string
	New    NewNode
	// Pin pins the node as the group applies it, for a caller that holds the node open.
	Pin bool
}

Step makes one node of a plan under Parent: an upper directory, or Pending(i) for the node of an earlier step.

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store is the upper store of one workspace.

func Open

func Open(o Options) (*Store, error)

Open locks the upper directory, replays its journal and starts a new epoch.

func (*Store) Checkpoint

func (s *Store) Checkpoint() error

Checkpoint compacts the journal: it writes a version file for each node that has extents at the current sequence, writes a snapshot of the index, and then deletes what the snapshot makes redundant. Only one checkpoint runs at a time; a call that arrives during one waits for it.

func (*Store) Close

func (s *Store) Close() error

Close refuses new mutations, flushes, and only when the flush succeeds closes the files and releases the lock. A failed flush releases nothing and the store keeps refusing mutations; the failure is kept, so a later Close fails the same way. It leaves the data on disk.

func (*Store) Create

func (s *Store) Create(dir uint64, name string, nn NewNode) (uint64, error)

func (*Store) Flush

func (s *Store) Flush() error

Flush makes every record that was answered before the call durable.

func (*Store) GetAttr

func (s *Store) GetAttr(unode uint64) (Attr, error)

func (*Store) GetXattr

func (s *Store) GetXattr(unode uint64, name string) ([]byte, bool, error)

GetXattr returns the value of an extended attribute and whether the node has it.

func (*Store) Health

func (s *Store) Health() error

Health returns the error of the last checkpoint, or nil when the last one succeeded.

func (*Store) ListXattr

func (s *Store) ListXattr(unode uint64) ([]string, error)

ListXattr names the extended attributes of a node, sorted.

func (*Store) Lookup

func (s *Store) Lookup(dir uint64, name string) (DirEntry, error)

Lookup answers ErrDirGone when dir is removed or unknown, and fs.ErrNotExist when only the name is missing.

func (*Store) Pin

func (s *Store) Pin(unode uint64) error

Pin keeps a node alive for a caller that holds it open. A pinned node that Remove or Rename detaches stays readable and writable until the last Unpin; nothing of it survives a restart.

func (*Store) Read

func (s *Store) Read(unode uint64, off int64, n int) ([]byte, error)

Read returns up to n bytes of the logical content of a file at off: the version file or the base blob, the extents above it in order, the holes as zeros. It returns fewer bytes only at the end of the file. The read holds a reference on the files of its backing, so that a checkpoint does not delete them under it.

func (*Store) ReadDir

func (s *Store) ReadDir(dir uint64) ([]DirEntry, error)

ReadDir lists the upper entries of a directory by name.

func (*Store) Remove

func (s *Store) Remove(dir uint64, name string, whiteout bool) error

Remove deletes an entry. With whiteout it also hides the entry of the base tree, and then the entry may be absent from the upper.

func (*Store) RemoveXattr

func (s *Store) RemoveXattr(unode uint64, name string) error

RemoveXattr deletes an extended attribute. A value that is empty is a value, not a removal.

func (*Store) Rename

func (s *Store) Rename(oldDir uint64, oldName string, newDir uint64, newName string, inBase bool) error

Rename moves an entry and replaces the target. When the old name is also in the base tree (inBase), the operation leaves a whiteout there, so the base entry does not show again.

func (*Store) RenameBase

func (s *Store) RenameBase(oldDir uint64, oldName string, nn NewNode, newDir uint64, newName string) error

RenameBase moves an entry that only the base tree shows. In one operation it makes the entry in the upper at its old name (nn names the base path), moves it, replaces the target, and leaves a whiteout at the old name.

func (*Store) Seq

func (s *Store) Seq() uint64

Seq is the sequence of the last record. Token is the change token: the boot ID of this process and Seq.

func (*Store) SetAttr

func (s *Store) SetAttr(unode uint64, u AttrUpdate) error

SetAttr changes attributes. A size change copies a base file up first, in the same operation.

func (*Store) SetOpaque

func (s *Store) SetOpaque(dir uint64) error

SetOpaque makes a directory hide the base entries below it.

func (*Store) SetXattr

func (s *Store) SetXattr(unode uint64, name string, value []byte) error

func (*Store) Snapshot

func (s *Store) Snapshot() *Snapshot

Snapshot copies the index under the read lock.

func (*Store) Token

func (s *Store) Token() string

func (*Store) Tx

func (s *Store) Tx(plan []Step) *Tx

Tx starts a call that makes the nodes of plan first.

func (*Store) Unpin

func (s *Store) Unpin(unode uint64) error

func (*Store) Write

func (s *Store) Write(unode uint64, off int64, p []byte) error

Write stores p at off. A write over 1 MiB is several records of one operation.

type Tx

type Tx struct {

	// Unodes holds the unode of each step after the call succeeded.
	Unodes []uint64
	// contains filtered or unexported fields
}

Tx is a call with a plan: the steps make their nodes in the same operation group as the call, so a call that fails appends nothing, not even the steps.

func (*Tx) Create

func (t *Tx) Create(dir uint64, name string, nn NewNode) (uint64, error)

func (*Tx) Remove

func (t *Tx) Remove(dir uint64, name string, whiteout bool) error

func (*Tx) RemoveXattr

func (t *Tx) RemoveXattr(unode uint64, name string) error

func (*Tx) Rename

func (t *Tx) Rename(oldDir uint64, oldName string, newDir uint64, newName string, inBase bool) error

func (*Tx) RenameBase

func (t *Tx) RenameBase(oldDir uint64, oldName string, nn NewNode, newDir uint64, newName string) error

func (*Tx) SetAttr

func (t *Tx) SetAttr(unode uint64, u AttrUpdate) error

func (*Tx) SetXattr

func (t *Tx) SetXattr(unode uint64, name string, value []byte) error

func (*Tx) Write

func (t *Tx) Write(unode uint64, off int64, p []byte) error

type Type

type Type uint8

Type is the type of a record.

const (
	TypeCreate Type = iota + 1
	TypeRemove
	TypeRename
	TypeSetattr
	TypeWrite
	TypeCopyup
	TypeXattr
	TypeOpaque
	TypeEpoch
	TypeXattrRemove
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL