[stgt] Online resizing of LUN's

FUJITA Tomonori fujita.tomonori at lab.ntt.co.jp
Mon Jun 18 15:21:30 CEST 2012


On Mon, 18 Jun 2012 15:12:37 +0200 (CEST)
RedShift <redshift at telenet.be> wrote:

> So the patch in the email posted above should work? Will try that ASAP.

Not sure. Too many changes since then. The following patch can be
applied to the current git head. But not sure if it works.

diff --git a/usr/bs.c b/usr/bs.c
index e2faf30..f060341 100644
--- a/usr/bs.c
+++ b/usr/bs.c
@@ -32,6 +32,7 @@
 #include <sys/types.h>
 #include <sys/epoll.h>
 #include <linux/types.h>
+#include <sys/inotify.h>
 
 #include "list.h"
 #include "tgtd.h"
@@ -304,6 +305,22 @@ destroy_cond_mutex:
 	return 1;
 }
 
+extern void resize_detect(int fd, int events, void *data);
+
+void setup_inotify(void)
+{
+	int ret;
+
+	inotify_fd = inotify_init1(IN_NONBLOCK);
+	if (inotify_fd < 0)
+		eprintf("lun size change detection is disabled\n");
+	else {
+		ret = tgt_event_add(inotify_fd, EPOLLIN,
+				    resize_detect, NULL);
+		eprintf("inotify ready, %d\n", ret);
+	}
+}
+
 int bs_init(void)
 {
 	int ret;
@@ -311,12 +328,14 @@ int bs_init(void)
 	ret = bs_init_signalfd();
 	if (!ret) {
 		eprintf("use signalfd notification\n");
+		setup_inotify();
 		return 0;
 	}
 
 	ret = bs_init_notify_thread();
 	if (!ret) {
 		eprintf("use pthread notification\n");
+		setup_inotify();
 		return 0;
 	}
 
diff --git a/usr/bs_rdwr.c b/usr/bs_rdwr.c
index 7d05359..899ee7f 100644
--- a/usr/bs_rdwr.c
+++ b/usr/bs_rdwr.c
@@ -33,6 +33,7 @@
 
 #include <linux/fs.h>
 #include <sys/epoll.h>
+#include <sys/inotify.h>
 
 #include "list.h"
 #include "util.h"
@@ -269,6 +270,7 @@ static void bs_rdwr_request(struct scsi_cmd *cmd)
 static int bs_rdwr_open(struct scsi_lu *lu, char *path, int *fd, uint64_t *size)
 {
 	uint32_t blksize = 0;
+	int ret;
 
 	*fd = backed_file_open(path, O_RDWR|O_LARGEFILE|lu->bsoflags, size,
 				&blksize);
@@ -284,6 +286,16 @@ static int bs_rdwr_open(struct scsi_lu *lu, char *path, int *fd, uint64_t *size)
 	if (!lu->attrs.no_auto_lbppbe)
 		update_lbppbe(lu, blksize);
 
+	if (inotify_fd > 0) {
+		ret = inotify_add_watch(inotify_fd, path, IN_CLOSE_WRITE);
+		if (ret < 0)
+			eprintf("can't check %s\n", path);
+		else {
+			eprintf("checking %s\n", path);
+			lu->notify_fd = ret;
+		}
+	}
+
 	return 0;
 }
 
diff --git a/usr/target.c b/usr/target.c
index 68222a0..63b4c76 100644
--- a/usr/target.c
+++ b/usr/target.c
@@ -29,6 +29,7 @@
 #include <unistd.h>
 #include <sys/socket.h>
 #include <sys/time.h>
+#include <sys/inotify.h>
 
 #include "list.h"
 #include "util.h"
@@ -63,6 +64,38 @@ static struct device_type_template *device_type_lookup(int type)
 
 static LIST_HEAD(target_list);
 
+void resize_detect(int fd, int events, void *data)
+{
+	struct target *target;
+	struct scsi_lu *lu;
+	struct inotify_event e;
+	int ret;
+
+	ret = read(fd, (char *)&e, sizeof(e));
+
+	eprintf("resize, %d\n", ret);
+	if (ret < 0)
+		return;
+
+	list_for_each_entry(target, &target_list, target_siblings) {
+		list_for_each_entry(lu, &target->device_list, device_siblings) {
+			if (lu->notify_fd == e.wd) {
+				uint64_t size;
+				uint32_t blksize = 0;
+				int fd;
+				eprintf("%s changed\n", lu->path);
+				fd = backed_file_open(lu->path, O_RDONLY, &size, &blksize);
+				if (fd > 0) {
+					eprintf("new size %lld\n", (long long)size);
+					close(fd);
+					lu->size = size;
+				} else
+					eprintf("failed to open\n");
+			}
+		}
+	}
+}
+
 static struct target *target_lookup(int tid)
 {
 	struct target *target;
diff --git a/usr/tgtd.c b/usr/tgtd.c
index cba2b66..97ce08c 100644
--- a/usr/tgtd.c
+++ b/usr/tgtd.c
@@ -45,6 +45,7 @@
 unsigned long pagesize, pageshift;
 
 int system_active = 1;
+int inotify_fd = -1;
 static int ep_fd;
 static char program_name[] = "tgtd";
 static LIST_HEAD(tgt_events_list);
diff --git a/usr/tgtd.h b/usr/tgtd.h
index aa9b9d5..8c8e76f 100644
--- a/usr/tgtd.h
+++ b/usr/tgtd.h
@@ -168,6 +168,7 @@ struct scsi_lu {
 	uint64_t size;
 	uint64_t lun;
 	char *path;
+	int notify_fd;
 	int bsoflags;
 	unsigned int blk_shift;
 
@@ -226,6 +227,7 @@ enum mgmt_req_result {
 
 extern int system_active;
 extern int is_debug;
+extern int inotify_fd;
 extern int nr_iothreads;
 extern struct list_head bst_list;
 
--
To unsubscribe from this list: send the line "unsubscribe stgt" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html



More information about the stgt mailing list