Commit 0b4e6a63 authored by Sebastien Gougeaud's avatar Sebastien Gougeaud Committed by Thomas Leibovici
Browse files

phobosd: lock operation notification



Addition of a notify-message operation to inform the daemon that a
device has been locked.

Change-Id: I5caa38921f5f14a59f59d6b7ae4abb55c304a688
Signed-off-by: default avatarSebastien Gougeaud <sebastien.gougeaud@cea.fr>
Reviewed-on: https://gerrit.ccc.ocre.cea.fr/gerrit/c/phobos/+/6793

Tested-by: default avatarJenkins s8open_nr <s8open_nr@ccc.ocre.cea.fr>
Reviewed-by: default avatarThomas Leibovici <thomas.leibovici@cea.fr>
Reviewed-by: default avatarQuentin Bouget <quentin.bouget@cea.fr>
parent ee639c71
......@@ -347,6 +347,32 @@ int phobos_admin_device_add(struct admin_handle *adm, enum rsc_family family,
return 0;
}
/**
* TODO: admin_device_lock will have the responsability to update the device
* state, to then remove this part of code from the CLI.
*/
int phobos_admin_device_lock(struct admin_handle *adm, struct pho_id *dev_ids,
int num_dev)
{
int rc = 0;
int i;
if (!adm->daemon_is_online)
return 0;
for (i = 0; i < num_dev; ++i) {
int rc2;
rc2 = _admin_notify(adm, dev_ids + i, PHO_NTFY_OP_DEVICE_LOCK);
if (rc2)
pho_error(rc2, "Failure during daemon notification for '%s'",
dev_ids[i].name);
rc = rc ? : rc2;
}
return rc;
}
int phobos_admin_device_unlock(struct admin_handle *adm, struct pho_id *dev_ids,
int num_dev, bool is_forced)
{
......
......@@ -625,13 +625,13 @@ class DeviceOptHandler(BaseResourceOptHandler):
def exec_lock(self):
"""Device lock"""
devices = []
serials = self.params.get('res')
devices = []
for serial in serials:
device = self.filter(serial)
if not device:
self.logger.error("Device %s not found", serial)
self.logger.error("Device '%s' not found", serial)
sys.exit(os.EX_DATAERR)
assert len(device) == 1
......@@ -661,6 +661,13 @@ class DeviceOptHandler(BaseResourceOptHandler):
finally:
self.client.devices.unlock(devices)
try:
with AdminClient(lrs_required=False) as adm:
adm.device_lock(self.family, serials)
except EnvironmentError as err:
self.logger.error("Failed to notify: %s", env_error_format(err))
sys.exit(os.EX_DATAERR)
self.logger.info("%d device(s) locked", len(devices))
def exec_unlock(self):
......
......@@ -100,6 +100,17 @@ class Client(object):
raise EnvironmentError(rc, "Cannot add device '%s' to the LRS"
% dev_name)
def device_lock(self, dev_family, dev_names):
"""Inform the daemon that devices are locked."""
c_id = Id * len(dev_names)
dev_ids = [Id(dev_family, name) for name in dev_names]
rc = LIBPHOBOS_ADMIN.phobos_admin_device_lock(byref(self.handle),
c_id(*dev_ids),
len(dev_ids))
if rc:
raise EnvironmentError(rc, "Cannot notify the daemon")
def device_unlock(self, dev_family, dev_names, is_forced):
"""Wrapper for the device unlock command."""
c_id = Id * len(dev_names)
......
......@@ -448,6 +448,7 @@ struct object_info {
enum notify_op {
PHO_NTFY_OP_INVAL = -1,
PHO_NTFY_OP_ADD_DEVICE,
PHO_NTFY_OP_DEVICE_LOCK,
PHO_NTFY_OP_DEVICE_UNLOCK,
PHO_NTFY_OP_LAST
};
......
......@@ -76,6 +76,19 @@ int phobos_admin_init(struct admin_handle *adm, bool lrs_required);
int phobos_admin_device_add(struct admin_handle *adm, enum rsc_family family,
const char *name);
/**
* Inform the LRS devices state has changed to 'locked'.
*
* \param[in] adm Admin module handler.
* \param[in] dev_ids Device IDs to lock.
* \param[in] num_dev Number of device to lock.
*
* \return 0 on success,
* -errno on failure.
*/
int phobos_admin_device_lock(struct admin_handle *adm, struct pho_id *dev_ids,
int num_dev);
/**
* Update the administrative state of the given devices to 'unlocked' and
* inform the LRS devices state has changed.
......
......@@ -2279,6 +2279,32 @@ err:
return rc;
}
/**
* Remove the locked device from the local device array.
* It will be inserted back once the device status is changed to 'unlocked'.
*/
static int sched_device_lock(struct lrs_sched *sched, const char *name)
{
struct dev_descr *dev;
int i;
for (i = 0; i < sched->devices->len; ++i) {
dev = &g_array_index(sched->devices, struct dev_descr, i);
if (!strcmp(name, dev->dss_dev_info->rsc.id.name)) {
g_array_remove_index_fast(sched->devices, i);
pho_verb("Removed locked device '%s' from the local database",
name);
return 0;
}
}
pho_verb("Cannot find local device info for '%s', not critical, "
"will continue", name);
return 0;
}
/**
* Update local admin status of device to 'unlocked',
* or fetch it from the database if unknown
......@@ -2701,6 +2727,9 @@ static int sched_handle_notify(struct lrs_sched *sched, pho_req_t *req,
rc = sched_device_add(sched, nreq->rsrc_id->family,
nreq->rsrc_id->name);
break;
case PHO_NTFY_OP_DEVICE_LOCK:
rc = sched_device_lock(sched, nreq->rsrc_id->name);
break;
case PHO_NTFY_OP_DEVICE_UNLOCK:
rc = sched_device_unlock(sched, nreq->rsrc_id->name);
break;
......
......@@ -39,7 +39,8 @@ message PhoResourceId {
/** Notify operation. */
enum PhoNotifyOp {
OP_ADD_DEV = 0; // Device add operation.
OP_DEV_UNLOCK = 1; // Device unlock operation.
OP_DEV_LOCK = 1; // Device lock operation.
OP_DEV_UNLOCK = 2; // Device unlock operation.
}
/** Selected filesystem type for a medium. */
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment