Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 1 addition & 126 deletions discoverd/src/events.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@
* Authors: Martin Belanger <martin.belanger@dell.com>
*/

#include <errno.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <systemd/sd-device.h>
#include <systemd/sd-event.h>

Expand All @@ -24,121 +21,17 @@
#include "sysfs.h"
#include "tid.h"

/* Sysfs soak delay: kernel sysfs attributes settle ~1 s after the add uevent. */
#define SOAK_DELAY_USEC (UINT64_C(1000000))

/* NVMe AEN for "Discovery Log Page Changed" (NVMe Base Spec §5.2.4.2). */
#define NVME_AEN_DLP_CHANGED "0x70f002"

struct soak_entry {
struct soak_entry *next;
char *devname;
sd_event_source *timer;
struct events_ctx *ctx;
};

struct events_ctx {
sd_event *event;
struct events_callbacks callbacks;
void *user_data;
sd_device_monitor *nvme_monitor;
sd_device_monitor *fc_monitor;
struct soak_entry *soaking;
uint64_t soak_usec;
};

static void soak_entry_free(struct soak_entry *e)
{
if (!e)
return;
sd_event_source_unref(e->timer);
free(e->devname);
free(e);
}

static void soak_unlink(struct events_ctx *ctx, struct soak_entry *entry)
{
struct soak_entry **ep;

for (ep = &ctx->soaking; *ep; ep = &(*ep)->next) {
if (*ep == entry) {
*ep = entry->next;
return;
}
}
}

static int soak_timeout(sd_event_source *src __attribute__((unused)),
uint64_t usec __attribute__((unused)),
void *user_data)
{
struct soak_entry *e = user_data;
struct events_ctx *ctx = e->ctx;
__cleanup_free char *cntrltype = NULL;
__cleanup_free char *path = sysfs_nvme_path(e->devname);

if (!path)
goto out;

if (access(path, F_OK) < 0) {
// Device disappeared during soak — treat as removal.
if (ctx->callbacks.nvme_remove)
ctx->callbacks.nvme_remove(e->devname, ctx->user_data);
goto out;
}

cntrltype = sysfs_nvme_attr(e->devname, "cntrltype");

if (shr_streq0(cntrltype, "discovery")) {
if (ctx->callbacks.dc_add)
ctx->callbacks.dc_add(e->devname, ctx->user_data);
} else if (shr_streq0(cntrltype, "io")) {
if (ctx->callbacks.ioc_add)
ctx->callbacks.ioc_add(e->devname, ctx->user_data);
}

out:
soak_unlink(ctx, e);
soak_entry_free(e);
return 0;
}

static int schedule_soak(struct events_ctx *ctx, const char *devname)
{
struct soak_entry *e;
uint64_t now;
int r;

e = calloc(1, sizeof(*e));
if (!e)
return -ENOMEM;

e->devname = strdup(devname);
e->ctx = ctx;
if (!e->devname) {
soak_entry_free(e);
return -ENOMEM;
}

r = sd_event_now(ctx->event, CLOCK_BOOTTIME, &now);
if (r < 0) {
soak_entry_free(e);
return r;
}

r = sd_event_add_time(ctx->event, &e->timer, CLOCK_BOOTTIME,
now + ctx->soak_usec, 0,
soak_timeout, e);
if (r < 0) {
soak_entry_free(e);
return r;
}

e->next = ctx->soaking;
ctx->soaking = e;
return 0;
}

static const char *uevent_get(const struct uevent *ev, const char *key)
{
return ev->property ? ev->property(ev, key) : NULL;
Expand All @@ -149,13 +42,6 @@ static void handle_nvme(struct events_ctx *ctx, const struct uevent *ev)
__cleanup_free char *cntrltype = NULL;
const char *nvme_event, *nvme_aen;

if (shr_streq0(ev->action, "add")) {
// Schedule a soak timer; the soak callback reads cntrltype
// and calls the appropriate add handler.
schedule_soak(ctx, ev->devname);
return;
}

if (shr_streq0(ev->action, "remove")) {
if (ctx->callbacks.nvme_remove)
ctx->callbacks.nvme_remove(ev->devname, ctx->user_data);
Expand Down Expand Up @@ -305,13 +191,9 @@ struct events_ctx *events_start(sd_event *event,
ctx->callbacks = *callbacks;
ctx->user_data = user_data;

/*
* Without udev, a test sends the uevents through events_dispatch()
* after it has written the sysfs attributes: nothing to soak.
*/
// Without udev, a test sends the uevents through events_dispatch().
if (!udev)
return ctx;
ctx->soak_usec = SOAK_DELAY_USEC;

// nvme subsystem monitor
r = sd_device_monitor_new(&ctx->nvme_monitor);
Expand Down Expand Up @@ -352,16 +234,9 @@ struct events_ctx *events_start(sd_event *event,

void events_stop(struct events_ctx *ctx)
{
struct soak_entry *e, *next;

if (!ctx)
return;

for (e = ctx->soaking; e; e = next) {
next = e->next;
soak_entry_free(e);
}

sd_device_monitor_unref(ctx->nvme_monitor);
sd_device_monitor_unref(ctx->fc_monitor);
free(ctx);
Expand Down
14 changes: 0 additions & 14 deletions discoverd/src/events.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@ struct uevent {
* All callbacks are called from the event loop thread.
*/
struct events_callbacks {
/*
* nvmeX device appeared with cntrltype=="discovery".
* Called after the ~1 s sysfs soak.
*/
void (*dc_add)(const char *devname, void *user_data);

/*
* DC sent NVME_AEN=="0x70f002" (DLP changed) or was rediscovered
* after a ctrl-loss (NVME_EVENT=="rediscover"). Caller should
Expand All @@ -54,14 +48,6 @@ struct events_callbacks {
void (*dc_changed)(const char *devname, const struct dc_addr *addr,
void *user_data);

/*
* nvmeX device appeared with cntrltype=="io".
* The event can be processed before ExecStartPost= of the transient
* unit wrote the state directory; the unit's job completion links
* the device then.
*/
void (*ioc_add)(const char *devname, void *user_data);

/*
* nvmeX device was removed by the kernel.
* Caller is responsible for state cleanup and reconnect decisions.
Expand Down
41 changes: 3 additions & 38 deletions discoverd/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1293,9 +1293,9 @@ static void on_job_done(const char *unit_name, bool success,
e->ncc_stopped = false;

/*
* The device's add event is soaked for about a second and
* can be processed before ExecStartPost= wrote the state
* file. The job completes only after ExecStartPost=.
* nvme connect returns once the controller is identified,
* and the job completes after ExecStartPost= wrote the
* state file: sysfs and the device name are ready here.
*/
if (e->is_dc) {
__cleanup_free char *devname =
Expand All @@ -1319,23 +1319,6 @@ static void on_job_done(const char *unit_name, bool success,
libnvmf_tid_str(e->tid));
}

static void on_dc_add(const char *devname,
void *user_data __attribute__((unused)))
{
struct active_ctrl *e;
char *unit_name;

// Only a DC that nvme-discoverd connected has a state file.
unit_name = state_read_unit(devname);
if (!unit_name)
return;

e = ctrl_find_by_unit(unit_name);
free(unit_name);
if (e)
dc_connected(e, devname);
}

/*
* A DC that nobody owns, e.g. from a manual "nvme discover --persistent".
* Without the legacy udev rules, nothing else handles its events. Run
Expand Down Expand Up @@ -1388,22 +1371,6 @@ static void on_dc_changed(const char *devname, const struct dc_addr *addr,
dlp_fetch(e);
}

static void on_ioc_add(const char *devname,
void *user_data __attribute__((unused)))
{
struct active_ctrl *e;
char *unit_name;

unit_name = state_read_unit(devname);
if (!unit_name)
return;

e = ctrl_find_by_unit(unit_name);
if (e && !e->devname)
e->devname = strdup(devname);
free(unit_name);
}

static bool sysfs_ctrl_deleting(const char *devname)
{
__cleanup_free char *state = sysfs_nvme_attr(devname, "state");
Expand Down Expand Up @@ -2046,9 +2013,7 @@ static int apply_test_base_dir(const char *dir)
int main(int argc, char **argv)
{
static const struct events_callbacks callbacks = {
.dc_add = on_dc_add,
.dc_changed = on_dc_changed,
.ioc_add = on_ioc_add,
.nvme_remove = on_nvme_remove,
.fc_discovery = on_fc_discovery,
};
Expand Down