/* Advertise in the store a change of the given driver to the given new_state. Return 0 on success, or -errno on error. On error, the device will switch to XenbusStateClosing, and the error will be saved in the store.  */
int xenbus_switch_state(struct xenbus_device *dev, enum xenbus_state state)
/* Advertise in the store a change of the given driver to the given new_state. Return 0 on success, or -errno on error. On error, the device will switch to XenbusStateClosing, and the error will be saved in the store.  */
int xenbus_switch_state(struct xenbus_device *dev, enum xenbus_state state)
{
	int current_state;
	int err;
	if (state == dev->state)
		return 0;
	err = xenbus_scanf(XBT_NIL, dev->nodename, "state", "%d",
			   &current_state);
	if (err != 1)
		return 0;
	err = xenbus_printf(XBT_NIL, dev->nodename, "state", "%d", state);
	if (err) {
		if (state != XenbusStateClosing)
			xenbus_dev_fatal(dev, err, "writing new state");
		return err;
	}
	dev->state = state;
	return 0;
}