mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 10:07:28 +01:00
ublk: refuse any pre-existing device id; idempotent Target.Close; surface queue thread errors
- No automatic reclaim: Start refuses a /dev/ublkcN already present at the requested id, with an error naming it, before sending any command (the device-info read, pid probe and pid-namespace questions are gone). A device created by another starter between the check and ADD_DEV makes ADD_DEV itself fail (EEXIST); nothing is deleted. Removing a leftover device is a documented operator step (package doc). - Target.Close runs the device close once; every caller, concurrent or later, gets that result. - A queue thread's abnormal error, including a failed ring close, is kept and returned through the shutdown (settleQueues joins it). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
6d95280296
commit
ae5f774b72
5 files changed
+55
-148
No files matched your search
@@ -51,7 +51,7 @@ type queue struct {
|
||||
|
||||
// Start creates /dev/ublkbN (N = cfg.DevID, or the kernel's choice when
|
||||
// DevID < 0) backed by cfg.Backend and serves it until Close. A device
|
||||
// left at the same id by a dead server is stopped and deleted first.
|
||||
// already present at the requested id is refused untouched.
|
||||
func Start(cfg Config) (*Device, error) {
|
||||
if err := cfg.validate(); err != nil {
|
||||
return nil, err
|
||||
@@ -75,7 +75,7 @@ func Start(cfg Config) (*Device, error) {
|
||||
if cfg.DevID >= 0 {
|
||||
id = uint32(cfg.DevID)
|
||||
_, statErr := os.Stat(charDevicePrefix + strconv.Itoa(cfg.DevID))
|
||||
if err := reclaimStale(d, id, statErr == nil, processAlive); err != nil {
|
||||
if err := refusePreExisting(id, statErr == nil); err != nil {
|
||||
return fail(err)
|
||||
}
|
||||
}
|
||||
@@ -195,24 +195,6 @@ func (d *Device) teardown(added bool) error {
|
||||
// queueStopTimeout bounds how long Close waits for the queue threads.
|
||||
const queueStopTimeout = 10 * time.Second
|
||||
|
||||
// command implements controller for reclaimStale.
|
||||
func (d *Device) command(cmdOp uint32, devID uint32, buf []byte, data uint64) (int32, error) {
|
||||
return d.ctrlCommand(cmdOp, devID, buf, data)
|
||||
}
|
||||
|
||||
// processAlive reports whether pid names a live process: signal 0 is
|
||||
// delivered (or refused with EPERM: it exists), ESRCH means it is gone.
|
||||
func processAlive(pid int32) (bool, error) {
|
||||
switch err := unix.Kill(int(pid), 0); err {
|
||||
case nil, unix.EPERM:
|
||||
return true, nil
|
||||
case unix.ESRCH:
|
||||
return false, nil
|
||||
default:
|
||||
return false, fmt.Errorf("ublk: probe pid %d: %w", pid, err)
|
||||
}
|
||||
}
|
||||
|
||||
// ctrlCommand runs one control command on /dev/ublk-control and returns
|
||||
// the CQE result. buf (may be nil) is the command's in/out buffer.
|
||||
func (d *Device) ctrlCommand(cmdOp uint32, devID uint32, buf []byte, data uint64) (int32, error) {
|
||||
@@ -302,12 +284,12 @@ func (q *queue) run(ctx context.Context) {
|
||||
q.fetched <- err
|
||||
if err != nil {
|
||||
if r != nil {
|
||||
_ = r.close()
|
||||
err = errors.Join(err, r.close())
|
||||
}
|
||||
q.err = err
|
||||
return
|
||||
}
|
||||
defer r.close()
|
||||
defer func() { q.err = errors.Join(q.err, r.close()) }() // before done closes
|
||||
|
||||
live := q.depth // tags with a fetch outstanding
|
||||
var cq []cqe
|
||||
|
||||
@@ -15,7 +15,13 @@
|
||||
// Server crash or restart is fail-closed: the device is created without
|
||||
// UBLK_F_USER_RECOVERY, so when the server process dies the kernel aborts
|
||||
// outstanding and new I/O on /dev/ublkbN with errors; nothing is
|
||||
// acknowledged that the backend did not complete. A restarted server
|
||||
// deletes a stale device left at the same id and creates it again; the
|
||||
// consumer must reopen /dev/ublkbN.
|
||||
// acknowledged that the backend did not complete.
|
||||
//
|
||||
// A restarted server never reclaims a device left at its id: Start
|
||||
// refuses any pre-existing /dev/ublkcN with an error naming the id and
|
||||
// sends nothing to the kernel. Removing a leftover device is a separate
|
||||
// operator step, taken only after proving no server uses it: delete it
|
||||
// through the ublk control device (for example the ublksrv "ublk del -n N"
|
||||
// tool), or unload ublk_drv once no device is in use. Then start again;
|
||||
// the consumer must reopen /dev/ublkbN.
|
||||
package ublk
|
||||
@@ -5,41 +5,18 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// controller runs one ublk control command (the device's control ring in
|
||||
// production; a recorder in tests).
|
||||
type controller interface {
|
||||
command(cmdOp uint32, devID uint32, buf []byte, data uint64) (int32, error)
|
||||
}
|
||||
|
||||
// reclaimStale handles a device already present at id before this server
|
||||
// creates its own. It is deleted only with proof that its server is dead:
|
||||
// the kernel's record of the server pid (set by START_DEV) names a process
|
||||
// that no longer exists. A device whose owner may be alive, never started
|
||||
// (no pid), or cannot be read is refused untouched; an operator removes it
|
||||
// once it is proven unused. A reused pid reads as alive: refused (safe).
|
||||
func reclaimStale(c controller, id uint32, exists bool, alive func(pid int32) (bool, error)) error {
|
||||
// refusePreExisting refuses a device id that is already present. There is
|
||||
// no automatic reclaim: deciding that another server's device is dead (a
|
||||
// device-info read plus a pid probe, then a numeric STOP/DEL) races a
|
||||
// replacement device created in between, and a pid probe from another PID
|
||||
// namespace proves nothing. Nothing is sent to the kernel here; removing a
|
||||
// leftover device is a separate operator step (see the package doc).
|
||||
func refusePreExisting(id uint32, exists bool) error {
|
||||
if !exists {
|
||||
return nil
|
||||
}
|
||||
var info devInfo
|
||||
if _, err := c.command(uCmdGetDevInfo, id, bytesOf(&info), 0); err != nil {
|
||||
return fmt.Errorf("ublk: device %d exists and its owner cannot be read (%v): not deleting it", id, err)
|
||||
}
|
||||
if info.SrvPID <= 0 {
|
||||
return fmt.Errorf("ublk: device %d exists with no recorded server (never started, or starting): not deleting it; remove it once proven unused", id)
|
||||
}
|
||||
live, err := alive(info.SrvPID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("ublk: device %d: liveness of its server pid %d unknown (%v): not deleting it", id, info.SrvPID, err)
|
||||
}
|
||||
if live {
|
||||
return fmt.Errorf("ublk: device %d belongs to the live server pid %d: refusing", id, info.SrvPID)
|
||||
}
|
||||
_, _ = c.command(uCmdStopDev, id, nil, 0)
|
||||
if _, err := c.command(uCmdDelDev, id, nil, 0); err != nil {
|
||||
return fmt.Errorf("ublk: delete stale device %d: %w", id, err)
|
||||
}
|
||||
return nil
|
||||
return fmt.Errorf("ublk: device %d already exists (%s%d); refusing to start: it may belong to another server. "+
|
||||
"Remove it as a separate operator step once it is proven unused", id, charDevicePrefix, id)
|
||||
}
|
||||
|
||||
// settler is a queue being shut down: done closes when its thread has
|
||||
@@ -52,9 +29,10 @@ type settler interface {
|
||||
}
|
||||
|
||||
// settleQueues waits, until one shared deadline, for the queues to stop
|
||||
// and releases each one that did. A queue that did not stop keeps its
|
||||
// mappings: its thread may still read descriptors or write buffers, so
|
||||
// they are leaked (fail closed) and the failure is returned.
|
||||
// and releases each one that did, returning the first error a stopped
|
||||
// queue's thread ended with. A queue that did not stop keeps its mappings:
|
||||
// its thread may still read descriptors or write buffers, so they are
|
||||
// leaked (fail closed) and the failure is returned.
|
||||
func settleQueues(qs []settler, timeout time.Duration) error {
|
||||
var firstErr error
|
||||
deadline := time.NewTimer(timeout)
|
||||
@@ -65,6 +43,9 @@ func settleQueues(qs []settler, timeout time.Duration) error {
|
||||
select {
|
||||
case <-q.doneCh():
|
||||
q.release()
|
||||
if err := q.result(); err != nil && firstErr == nil {
|
||||
firstErr = fmt.Errorf("ublk: %s: %w", q.name(), err)
|
||||
}
|
||||
continue
|
||||
case <-deadline.C:
|
||||
expired = true
|
||||
@@ -73,6 +54,9 @@ func settleQueues(qs []settler, timeout time.Duration) error {
|
||||
select {
|
||||
case <-q.doneCh(): // stopped by now: safe to release
|
||||
q.release()
|
||||
if err := q.result(); err != nil && firstErr == nil {
|
||||
firstErr = fmt.Errorf("ublk: %s: %w", q.name(), err)
|
||||
}
|
||||
default:
|
||||
if firstErr == nil {
|
||||
firstErr = fmt.Errorf("ublk: %s did not stop; its mappings are kept (leaked) while its thread may run", q.name())
|
||||
|
||||
@@ -6,75 +6,18 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// recordingController records control commands; GET_DEV_INFO fills the
|
||||
// device info with srvPID (or fails with infoErr).
|
||||
type recordingController struct {
|
||||
cmds []uint32
|
||||
srvPID int32
|
||||
infoErr error
|
||||
}
|
||||
|
||||
func (c *recordingController) command(cmdOp uint32, devID uint32, buf []byte, data uint64) (int32, error) {
|
||||
c.cmds = append(c.cmds, cmdOp)
|
||||
if cmdOp == uCmdGetDevInfo {
|
||||
if c.infoErr != nil {
|
||||
return 0, c.infoErr
|
||||
}
|
||||
if len(buf) >= int(unsafe.Sizeof(devInfo{})) {
|
||||
info := (*devInfo)(unsafe.Pointer(&buf[0]))
|
||||
info.DevID, info.SrvPID = devID, c.srvPID
|
||||
}
|
||||
}
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func (c *recordingController) destructive() []string {
|
||||
var out []string
|
||||
for _, op := range c.cmds {
|
||||
switch op {
|
||||
case uCmdStopDev:
|
||||
out = append(out, "STOP")
|
||||
case uCmdDelDev:
|
||||
out = append(out, "DEL")
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// A device already present at our id is never reclaimed automatically:
|
||||
// whatever its owner's state, Start refuses with an error naming the id
|
||||
// and sends no command at all (cleanup is a separate operator step).
|
||||
func TestReclaimStale_DeletesOnlyWithProofTheOwnerIsDead(t *testing.T) {
|
||||
dead := func(int32) (bool, error) { return false, nil }
|
||||
live := func(int32) (bool, error) { return true, nil }
|
||||
unknown := func(int32) (bool, error) { return false, errors.New("cannot signal") }
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
exists bool
|
||||
c *recordingController
|
||||
alive func(int32) (bool, error)
|
||||
refuse bool
|
||||
wantDest string
|
||||
}{
|
||||
{"no device", false, &recordingController{srvPID: 42}, dead, false, ""},
|
||||
{"owner process gone", true, &recordingController{srvPID: 42}, dead, true, ""},
|
||||
{"owner alive", true, &recordingController{srvPID: 42}, live, true, ""},
|
||||
{"owner liveness unknown", true, &recordingController{srvPID: 42}, unknown, true, ""},
|
||||
{"never started (no owner pid)", true, &recordingController{srvPID: 0}, dead, true, ""},
|
||||
{"device info unreadable", true, &recordingController{infoErr: errors.New("EPERM")}, dead, true, ""},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
err := reclaimStale(tc.c, 7, tc.exists, tc.alive)
|
||||
if tc.refuse != (err != nil) {
|
||||
t.Fatalf("refused=%v (err %v), want refused=%v", err != nil, err, tc.refuse)
|
||||
}
|
||||
if got := strings.Join(tc.c.destructive(), ","); got != tc.wantDest {
|
||||
t.Fatalf("destructive commands %q, want %q", got, tc.wantDest)
|
||||
}
|
||||
})
|
||||
// Start refuses with an error naming the id (and refusePreExisting has no
|
||||
// way to send a command). An absent id is accepted.
|
||||
func TestRefusePreExisting_RefusesAnyExistingID(t *testing.T) {
|
||||
if err := refusePreExisting(7, false); err != nil {
|
||||
t.Fatalf("absent id refused: %v", err)
|
||||
}
|
||||
err := refusePreExisting(7, true)
|
||||
if err == nil || !strings.Contains(err.Error(), "device 7") || !strings.Contains(err.Error(), "/dev/ublkc7") {
|
||||
t.Fatalf("err %v, want a refusal naming device 7", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -111,19 +54,6 @@ func TestSettleQueues_NeverReleasesAQueueThatMayStillRun(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// A pre-existing id is refused with zero commands of any kind, and the
|
||||
// error names the id.
|
||||
func TestReclaimStale_PreExistingIDRefusedWithoutAnyCommand(t *testing.T) {
|
||||
c := &recordingController{srvPID: 42}
|
||||
err := reclaimStale(c, 7, true, func(int32) (bool, error) { return false, nil })
|
||||
if err == nil || !strings.Contains(err.Error(), "7") {
|
||||
t.Fatalf("err %v, want a refusal naming device 7", err)
|
||||
}
|
||||
if len(c.cmds) != 0 {
|
||||
t.Fatalf("%d control commands sent for a pre-existing device, want none", len(c.cmds))
|
||||
}
|
||||
}
|
||||
|
||||
// A queue thread that ended abnormally (its ring failed, or closing the
|
||||
// ring failed) reports that error through the shutdown.
|
||||
func TestSettleQueues_SurfacesAQueueThreadError(t *testing.T) {
|
||||
|
||||
@@ -27,6 +27,9 @@ type Target struct {
|
||||
|
||||
mu sync.Mutex
|
||||
device targetDevice
|
||||
|
||||
closeOnce sync.Once
|
||||
closeErr error
|
||||
}
|
||||
|
||||
// targetDevice is what Target needs from a running device.
|
||||
@@ -77,19 +80,21 @@ func (t *Target) Stats() Stats {
|
||||
return d.Stats()
|
||||
}
|
||||
|
||||
// Close stops and deletes the device.
|
||||
// Close stops and deletes the device. It is idempotent: the device is
|
||||
// closed once, and every caller, concurrent or later, gets that result.
|
||||
func (t *Target) Close() error {
|
||||
if t == nil {
|
||||
return nil
|
||||
}
|
||||
t.mu.Lock()
|
||||
d := t.device
|
||||
t.device = nil
|
||||
t.mu.Unlock()
|
||||
if d == nil {
|
||||
return nil
|
||||
}
|
||||
return d.Close()
|
||||
t.closeOnce.Do(func() {
|
||||
t.mu.Lock()
|
||||
d := t.device
|
||||
t.mu.Unlock()
|
||||
if d != nil {
|
||||
t.closeErr = d.Close()
|
||||
}
|
||||
})
|
||||
return t.closeErr
|
||||
}
|
||||
|
||||
type providerBackend struct {
|
||||
|
||||
Reference in new issue
Block a user