gokm/blk

import "gokm/blk"

Package blk creates block devices (/dev/<name>) whose storage is Go code.

type ram struct{ ... }
func (r *ram) ReadAt(p []byte, off int64) error  { ... }
func (r *ram) WriteAt(p []byte, off int64) error { ... }

disk := &blk.Disk{Name: "myram%d", Size: 64 << 20, Device: &ram{}}
err := blk.Register(disk)  // /dev/myram0; partitions work too

The kernel’s block layer (blk-mq) queues the I/O; requests are handed to Go in process context (from the block layer’s worker threads), one data segment at a time: ReadAt/WriteAt get kernel memory valid only during the call, at offsets that are multiples of the logical block size. They can block and allocate. Several requests can be in flight at once, on different CPUs: the Device must be safe for concurrent use.

Disks are removed automatically at module exit.

Index#

Functions#

func Register#

func Register(d *Disk) error

Register creates and adds the disk. Name is updated with the kernel’s name for it (e.g. “gobrd0”).

Types#

type Device#

type Device interface {
	ReadAt(p []byte, off int64) error
	WriteAt(p []byte, off int64) error
}

Device stores the data of a disk.

type Discarder#

type Discarder interface {
	Discard(off, n int64) error
}

Discarder drops the data of a range (TRIM, BLKDISCARD, fstrim); discarded ranges may read back as anything (brd: zeros).

type Disk#

type Disk struct {
	// Name is the device name under /dev; a "%d" is replaced with a
	// number counting up from 0 for each disk the module creates.
	Name string
	// Size in bytes (a multiple of LogicalBlockSize).
	Size int64
	// LogicalBlockSize is the addressing unit (default 512);
	// PhysicalBlockSize the preferred I/O size (default 4096).
	LogicalBlockSize  int
	PhysicalBlockSize int
	// MaxIO limits the bytes per request (default 256 KiB).
	MaxIO    int
	ReadOnly bool
	// Rotational tells the kernel it's a spinning disk (default: an SSD).
	Rotational bool
	// Queues is the number of hardware queues (default: one per CPU), and
	// QueueDepth the requests in flight per queue (default 128).
	Queues     int
	QueueDepth int
	Device     Device

}

Disk describes a block device.

func (*Disk) Unregister#

func (d *Disk) Unregister()

Unregister removes the disk, waiting for in-flight I/O.

type Flusher#

type Flusher interface {
	Flush() error
}

Flusher makes previous writes durable (the disk then advertises a volatile write cache: the kernel sends flushes and FUA writes).