linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sergei Shtepa <sergei.shtepa@veeam.com>
To: <axboe@kernel.dk>, <hch@infradead.org>, <corbet@lwn.net>,
	<snitzer@kernel.org>
Cc: <viro@zeniv.linux.org.uk>, <brauner@kernel.org>,
	<dchinner@redhat.com>, <willy@infradead.org>,
	<dlemoal@kernel.org>, <linux@weissschuh.net>, <jack@suse.cz>,
	<ming.lei@redhat.com>, <linux-block@vger.kernel.org>,
	<linux-doc@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
	<linux-fsdevel@vger.kernel.org>, <sergei.shtepa@veeam.com>
Subject: [PATCH v5 09/11] blksnap: event queue from the difference storage
Date: Mon, 12 Jun 2023 15:52:26 +0200	[thread overview]
Message-ID: <20230612135228.10702-10-sergei.shtepa@veeam.com> (raw)
In-Reply-To: <20230612135228.10702-1-sergei.shtepa@veeam.com>

Provides transmission of events from the difference storage to the user
process. Only two events are currently defined. The first is that there
are few free regions in the difference storage. The second is that the
request for a free region for storing differences failed with an error,
since there are no more free regions left in the difference storage
(the snapshot overflow state).

Co-developed-by: Christoph Hellwig <hch@infradead.org>
Signed-off-by: Christoph Hellwig <hch@infradead.org>
Signed-off-by: Sergei Shtepa <sergei.shtepa@veeam.com>
---
 drivers/block/blksnap/event_queue.c | 87 +++++++++++++++++++++++++++++
 drivers/block/blksnap/event_queue.h | 65 +++++++++++++++++++++
 2 files changed, 152 insertions(+)
 create mode 100644 drivers/block/blksnap/event_queue.c
 create mode 100644 drivers/block/blksnap/event_queue.h

diff --git a/drivers/block/blksnap/event_queue.c b/drivers/block/blksnap/event_queue.c
new file mode 100644
index 000000000000..5d3bedd63c10
--- /dev/null
+++ b/drivers/block/blksnap/event_queue.c
@@ -0,0 +1,87 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2023 Veeam Software Group GmbH */
+#define pr_fmt(fmt) KBUILD_MODNAME "-event_queue: " fmt
+
+#include <linux/slab.h>
+#include <linux/sched.h>
+#include "event_queue.h"
+
+void event_queue_init(struct event_queue *event_queue)
+{
+	INIT_LIST_HEAD(&event_queue->list);
+	spin_lock_init(&event_queue->lock);
+	init_waitqueue_head(&event_queue->wq_head);
+}
+
+void event_queue_done(struct event_queue *event_queue)
+{
+	struct event *event;
+
+	spin_lock(&event_queue->lock);
+	while (!list_empty(&event_queue->list)) {
+		event = list_first_entry(&event_queue->list, struct event,
+					 link);
+		list_del(&event->link);
+		event_free(event);
+	}
+	spin_unlock(&event_queue->lock);
+}
+
+int event_gen(struct event_queue *event_queue, gfp_t flags, int code,
+	      const void *data, int data_size)
+{
+	struct event *event;
+
+	event = kzalloc(sizeof(struct event) + data_size + 1, flags);
+	if (!event)
+		return -ENOMEM;
+
+	event->time = ktime_get();
+	event->code = code;
+	event->data_size = data_size;
+	memcpy(event->data, data, data_size);
+
+	pr_debug("Generate event: time=%lld code=%d data_size=%d\n",
+		 event->time, event->code, event->data_size);
+
+	spin_lock(&event_queue->lock);
+	list_add_tail(&event->link, &event_queue->list);
+	spin_unlock(&event_queue->lock);
+
+	wake_up(&event_queue->wq_head);
+	return 0;
+}
+
+struct event *event_wait(struct event_queue *event_queue,
+			 unsigned long timeout_ms)
+{
+	int ret;
+
+	ret = wait_event_interruptible_timeout(event_queue->wq_head,
+				!list_empty(&event_queue->list), timeout_ms);
+	if (ret >= 0) {
+		struct event *event = ERR_PTR(-ENOENT);
+
+		spin_lock(&event_queue->lock);
+		if (!list_empty(&event_queue->list)) {
+			event = list_first_entry(&event_queue->list,
+						 struct event, link);
+			list_del(&event->link);
+		}
+		spin_unlock(&event_queue->lock);
+
+		if (IS_ERR(event))
+			pr_debug("Queue list is empty, timeout_ms=%lu\n", timeout_ms);
+		else
+			pr_debug("Event received: time=%lld code=%d\n",
+				 event->time, event->code);
+		return event;
+	}
+	if (ret == -ERESTARTSYS) {
+		pr_debug("event waiting interrupted\n");
+		return ERR_PTR(-EINTR);
+	}
+
+	pr_err("Failed to wait event. errno=%d\n", abs(ret));
+	return ERR_PTR(ret);
+}
diff --git a/drivers/block/blksnap/event_queue.h b/drivers/block/blksnap/event_queue.h
new file mode 100644
index 000000000000..7f1209bbfc98
--- /dev/null
+++ b/drivers/block/blksnap/event_queue.h
@@ -0,0 +1,65 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (C) 2023 Veeam Software Group GmbH */
+#ifndef __BLKSNAP_EVENT_QUEUE_H
+#define __BLKSNAP_EVENT_QUEUE_H
+
+#include <linux/types.h>
+#include <linux/ktime.h>
+#include <linux/list.h>
+#include <linux/spinlock.h>
+#include <linux/wait.h>
+
+/**
+ * struct event - An event to be passed to the user space.
+ * @link:
+ *	The list header allows to combine events from the queue.
+ * @time:
+ *	A timestamp indicates when an event occurred.
+ * @code:
+ *	Event code.
+ * @data_size:
+ *	The number of bytes in the event data array.
+ * @data:
+ *	An array of event data. The allowed size of the array is limited
+ *      so that the size of the entire structure does not exceed PAGE_SIZE.
+ *
+ * Events can be different, so they contain different data. The size of the
+ * data array is not defined exactly, but it has limitations. The size of
+ * the event structure may exceed the PAGE_SIZE.
+ */
+struct event {
+	struct list_head link;
+	ktime_t time;
+	int code;
+	int data_size;
+	char data[];
+};
+
+/**
+ * struct event_queue - A queue of &struct event.
+ * @list:
+ *	Linked list for storing events.
+ * @lock:
+ *	Spinlock allows to guarantee safety of the linked list.
+ * @wq_head:
+ *	A wait queue allows to put a user thread in a waiting state until
+ *	an event appears in the linked list.
+ */
+struct event_queue {
+	struct list_head list;
+	spinlock_t lock;
+	struct wait_queue_head wq_head;
+};
+
+void event_queue_init(struct event_queue *event_queue);
+void event_queue_done(struct event_queue *event_queue);
+
+int event_gen(struct event_queue *event_queue, gfp_t flags, int code,
+	      const void *data, int data_size);
+struct event *event_wait(struct event_queue *event_queue,
+			 unsigned long timeout_ms);
+static inline void event_free(struct event *event)
+{
+	kfree(event);
+};
+#endif /* __BLKSNAP_EVENT_QUEUE_H */
-- 
2.20.1


  parent reply	other threads:[~2023-06-12 13:56 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-12 13:52 [PATCH v5 00/11] blksnap - block devices snapshots module Sergei Shtepa
2023-06-12 13:52 ` [PATCH v5 01/11] documentation: Block Device Filtering Mechanism Sergei Shtepa
2023-06-12 13:52 ` [PATCH v5 02/11] block: " Sergei Shtepa
2023-07-11  2:02   ` Yu Kuai
2023-07-12 10:04     ` Yu Kuai
2023-07-12 12:34       ` Yu Kuai
2023-07-17 17:39         ` Sergei Shtepa
2023-07-18  1:21           ` Yu Kuai
2023-07-17 16:22       ` Sergei Shtepa
2023-07-17 14:39     ` Sergei Shtepa
2023-07-18  1:37       ` Yu Kuai
2023-07-18 11:25         ` Sergei Shtepa
2023-07-18 12:32           ` Yu Kuai
2023-07-18 16:33             ` Sergei Shtepa
2023-07-19  7:28               ` Yu Kuai
2023-07-19  8:36                 ` Sergei Shtepa
2023-07-20  6:14         ` Christoph Hellwig
2023-06-12 13:52 ` [PATCH v5 03/11] documentation: Block Devices Snapshots Module Sergei Shtepa
2023-06-12 13:52 ` [PATCH v5 04/11] blksnap: header file of the module interface Sergei Shtepa
2023-06-13 22:25   ` Dave Chinner
2023-06-14  6:26     ` Christoph Hellwig
2023-06-14  9:26       ` Sergei Shtepa
2023-06-14 14:07         ` Christoph Hellwig
2023-06-14 16:43           ` Sergei Shtepa
2023-06-15  0:08           ` Dave Chinner
2023-07-17 18:57   ` Thomas Weißschuh
2023-07-18  9:53     ` Sergei Shtepa
2023-07-20  6:16       ` Christoph Hellwig
2023-06-12 13:52 ` [PATCH v5 05/11] blksnap: module management interface functions Sergei Shtepa
2023-06-12 13:52 ` [PATCH v5 06/11] blksnap: handling and tracking I/O units Sergei Shtepa
2023-06-12 13:52 ` [PATCH v5 07/11] blksnap: minimum data storage unit of the original block device Sergei Shtepa
2023-06-12 13:52 ` [PATCH v5 08/11] blksnap: difference storage Sergei Shtepa
2023-06-12 13:52 ` Sergei Shtepa [this message]
2023-06-12 13:52 ` [PATCH v5 10/11] blksnap: snapshot and snapshot image block device Sergei Shtepa
2023-06-12 13:52 ` [PATCH v5 11/11] blksnap: Kconfig and Makefile Sergei Shtepa
2023-06-12 14:32 ` [PATCH v5 00/11] blksnap - block devices snapshots module Christoph Hellwig
2023-06-12 16:19 ` Eric Biggers
2023-06-13  5:50   ` Christoph Hellwig
2023-06-13 10:12   ` Sergei Shtepa
2023-06-14 17:22     ` Eric Biggers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230612135228.10702-10-sergei.shtepa@veeam.com \
    --to=sergei.shtepa@veeam.com \
    --cc=axboe@kernel.dk \
    --cc=brauner@kernel.org \
    --cc=corbet@lwn.net \
    --cc=dchinner@redhat.com \
    --cc=dlemoal@kernel.org \
    --cc=hch@infradead.org \
    --cc=jack@suse.cz \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@weissschuh.net \
    --cc=ming.lei@redhat.com \
    --cc=snitzer@kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    --cc=willy@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).