static long vhost_test_set_backend()

in test.c [267:317]


static long vhost_test_set_backend(struct vhost_test *n, unsigned index, int fd)
{
	static void *backend;

	const bool enable = fd != -1;
	struct vhost_virtqueue *vq;
	int r;

	mutex_lock(&n->dev.mutex);
	r = vhost_dev_check_owner(&n->dev);
	if (r)
		goto err;

	if (index >= VHOST_TEST_VQ_MAX) {
		r = -ENOBUFS;
		goto err;
	}
	vq = &n->vqs[index];
	mutex_lock(&vq->mutex);

	/* Verify that ring has been setup correctly. */
	if (!vhost_vq_access_ok(vq)) {
		r = -EFAULT;
		goto err_vq;
	}
	if (!enable) {
		vhost_poll_stop(&vq->poll);
		backend = vhost_vq_get_backend(vq);
		vhost_vq_set_backend(vq, NULL);
	} else {
		vhost_vq_set_backend(vq, backend);
		r = vhost_vq_init_access(vq);
		if (r == 0)
			r = vhost_poll_start(&vq->poll, vq->kick);
	}

	mutex_unlock(&vq->mutex);

	if (enable) {
		vhost_test_flush_vq(n, index);
	}

	mutex_unlock(&n->dev.mutex);
	return 0;

err_vq:
	mutex_unlock(&vq->mutex);
err:
	mutex_unlock(&n->dev.mutex);
	return r;
}