Skip to contentSkip to conversation

Storage manager.

PATCH New Nigel Cunningham 1 participant 14 messages Jun 27, 2006

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover Storage manager.

The storage manager provides a way for a userspace helper to bring up and tear down access to storage, as necessary. The helper must not itself need external programs, but can perform actions such as bringing up access to networked storage.

#1 lore raw

PATCH 1/13 Storage.c header.

Header for the storage.c file. This file provides the interface to a userspace helper that manages access to storage (possibly network storage, eg).

 kernel/power/storage.c |   36 ++++++++++++++++++++++++++++++++++++
 1 files changed, 36 insertions(+), 0 deletions(-)
kernel/power/storage.c new file +36 -0
new file mode 100644
index 0000000..91b24a6
@@ -0,0 +1,36 @@
+/*
+ * kernel/power/storage.c
+ *
+ * Copyright (C) 2005-2006 Nigel Cunningham <[email protected]>
+ *
+ * This file is released under the GPLv2.
+ *
+ * Routines for talking to a userspace program that manages storage.
+ *
+ * The kernel side:
+ * - starts the userspace program;
+ * - sends messages telling it when to open and close the connection;
+ * - tells it when to quit;
+ *
+ * The user space side:
+ * - passes messages regarding status;
+ *
+ */
+
+#include <linux/suspend.h>
+#include <linux/freezer.h>
+ 
+#include "proc.h"
+#include "modules.h"
+#include "netlink.h"
+#include "storage.h"
+#include "ui.h"
+
+static struct user_helper_data usm_helper_data;
+static struct suspend_module_ops usm_ops;
+static int message_received = 0;
+static int activations = 0;
+static int usm_prepare_count = 0;
+static int storage_manager_last_action = 0;
+static int storage_manager_action = 0;
+       

--
Nigel Cunningham nigel at suspend2 dot net

#2 lore raw

PATCH 2/13 Receive storage manager message

In reply to Nigel Cunningham

Routine called when netlink messages are received.

 kernel/power/storage.c |   38 ++++++++++++++++++++++++++++++++++++++
 1 files changed, 38 insertions(+), 0 deletions(-)
kernel/power/storage.c +38 -0
index 91b24a6..b66efe2 100644
@@ -34,3 +34,41 @@ static int usm_prepare_count = 0;
 static int storage_manager_last_action = 0;
 static int storage_manager_action = 0;
        
+static int usm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
+{
+	int type;
+	int *data;
+
+	type = nlh->nlmsg_type;
+
+	/* A control message: ignore them */
+	if (type < NETLINK_MSG_BASE)
+		return 0;
+
+	/* Unknown message: reply with EINVAL */
+	if (type >= USM_MSG_MAX)
+		return -EINVAL;
+
+	/* All operations require privileges, even GET */
+	if (security_netlink_recv(skb))
+		return -EPERM;
+
+	/* Only allow one task to receive NOFREEZE privileges */
+	if (type == NETLINK_MSG_NOFREEZE_ME && usm_helper_data.pid != -1)
+		return -EBUSY;
+
+	data = (int*)NLMSG_DATA(nlh);
+
+	switch (type) {
+		case USM_MSG_SUCCESS:
+		case USM_MSG_FAILED:
+			message_received = type;
+			complete(&usm_helper_data.wait_for_process);
+			break;
+		default:
+			printk("Storage manager doesn't recognise message %d.\n", type);
+	}
+
+	return 1;
+}
+

--
Nigel Cunningham nigel at suspend2 dot net

#3 lore raw

PATCH 3/13 Ask userspace to enable/disable access to our storage.

In reply to Nigel Cunningham

Ask userspace to do whatever it needs to do to provide us with access to the image storage, or to tear down the access.

 kernel/power/storage.c |   58 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 58 insertions(+), 0 deletions(-)
kernel/power/storage.c +58 -0
index b66efe2..71b8c02 100644
@@ -72,3 +72,61 @@ static int usm_user_rcv_msg(struct sk_bu
 	return 1;
 }
 
+int suspend_activate_storage(int force)
+{
+	int tries = 1;
+
+	if (usm_helper_data.pid == -1 || usm_ops.disabled)
+		return 0;
+
+	message_received = 0;
+	activations++;
+
+	if (activations > 1 && !force)
+		return 0;
+
+	while ((!message_received || message_received == USM_MSG_FAILED) && tries < 2) {
+		suspend_prepare_status(DONT_CLEAR_BAR, "Activate storage attempt %d.\n", tries);
+
+		init_completion(&usm_helper_data.wait_for_process);
+
+		suspend_send_netlink_message(&usm_helper_data,
+			USM_MSG_CONNECT,
+			NULL, 0);
+
+		/* Wait 2 seconds for the userspace process to make contact */
+		wait_for_completion_timeout(&usm_helper_data.wait_for_process, 2*HZ);
+
+		tries++;
+	}
+
+	return 0;
+}
+
+int suspend_deactivate_storage(int force)
+{
+	if (usm_helper_data.pid == -1 || usm_ops.disabled)
+		return 0;
+	
+	message_received = 0;
+	activations--;
+
+	if (activations && !force)
+		return 0;
+
+	init_completion(&usm_helper_data.wait_for_process);
+
+	suspend_send_netlink_message(&usm_helper_data,
+			USM_MSG_DISCONNECT,
+			NULL, 0);
+
+	wait_for_completion_timeout(&usm_helper_data.wait_for_process, 2*HZ);
+
+	if (!message_received || message_received == USM_MSG_FAILED) {
+		printk("Returning failure disconnecting storage.\n");
+		return 1;
+	}
+
+	return 0;
+}
+

--
Nigel Cunningham nigel at suspend2 dot net

#4 lore raw

PATCH 4/13 Utility function for testing storage managers.

In reply to Nigel Cunningham

Provide a means whereby a developer (via a proc entry) can test a storage manager. Invoke the prepare, activate, deactivate and cleanup routines in that order.

 kernel/power/storage.c |   18 ++++++++++++++++++
 1 files changed, 18 insertions(+), 0 deletions(-)
kernel/power/storage.c +18 -0
index 71b8c02..c007a8e 100644
@@ -130,3 +130,21 @@ int suspend_deactivate_storage(int force
 	return 0;
 }
 
+#ifdef CONFIG_PM_DEBUG
+static void storage_manager_simulate(void)
+{
+	printk("--- Storage manager simulate ---\n");
+	suspend_prepare_usm();
+	schedule();
+	printk("--- Activate storage 1 ---\n");
+	suspend_activate_storage(1);
+	schedule();
+	printk("--- Deactivate storage 1 ---\n");
+	suspend_deactivate_storage(1);
+	schedule();
+	printk("--- Cleanup usm ---\n");
+	suspend_cleanup_usm();
+	schedule();
+	printk("--- Storage manager simulate ends ---\n");
+}
+#endif

--
Nigel Cunningham nigel at suspend2 dot net

#5 lore raw

PATCH 5/13 Get storage manager space needed in image header.

In reply to Nigel Cunningham

Get the amount of space in the image header needed by the storage manager.

 kernel/power/storage.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)
kernel/power/storage.c +6 -0
index c007a8e..5fdcadd 100644
@@ -148,3 +148,9 @@ static void storage_manager_simulate(voi
 	printk("--- Storage manager simulate ends ---\n");
 }
 #endif
+
+static unsigned long usm_storage_needed(void)
+{
+	return strlen(usm_helper_data.program);
+}
+

--
Nigel Cunningham nigel at suspend2 dot net

#6 lore raw

PATCH 6/13 Serialise storage manager config in an image header.

In reply to Nigel Cunningham

Routines to read and write the storage manager configuration.

 kernel/power/storage.c |   16 ++++++++++++++++
 1 files changed, 16 insertions(+), 0 deletions(-)
kernel/power/storage.c +16 -0
index 5fdcadd..714e95e 100644
@@ -154,3 +154,19 @@ static unsigned long usm_storage_needed(
 	return strlen(usm_helper_data.program);
 }
 
+static int usm_save_config_info(char *buf)
+{
+	int len = strlen(usm_helper_data.program);
+	memcpy(buf, usm_helper_data.program, len);
+	return len;
+}
+
+static void usm_load_config_info(char *buf, int size)
+{
+	/* Don't load the saved path if one has already been set */
+	if (usm_helper_data.program[0])
+		return;
+
+	memcpy(usm_helper_data.program, buf, size);
+}
+

--
Nigel Cunningham nigel at suspend2 dot net

#7 lore raw

PATCH 7/13 Get the amount of memory needed by the storage manager.

In reply to Nigel Cunningham

Get the amount of memory needed by the storage manager. Note that this is just the storage needed while running, not the grand total.

 kernel/power/storage.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)
kernel/power/storage.c +6 -0
index 714e95e..e31623a 100644
@@ -170,3 +170,9 @@ static void usm_load_config_info(char *b
 	memcpy(usm_helper_data.program, buf, size);
 }
 
+static unsigned long usm_memory_needed(void)
+{
+	/* ball park figure of 32 pages */
+	return (32 * PAGE_SIZE);
+}
+

--
Nigel Cunningham nigel at suspend2 dot net

#8 lore raw

PATCH 8/13 Prepare and cleanup the storage manager.

In reply to Nigel Cunningham

Start the storage manager at the beginning of a suspend/resume, and clean it up at the end of a resume or cancelled suspend.

 kernel/power/storage.c |   47 +++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 47 insertions(+), 0 deletions(-)
kernel/power/storage.c +47 -0
index e31623a..fea9276 100644
@@ -176,3 +176,50 @@ static unsigned long usm_memory_needed(v
 	return (32 * PAGE_SIZE);
 }
 
+/* suspend_prepare_usm
+ */
+int suspend_prepare_usm(void)
+{
+	usm_prepare_count++;
+
+	if (usm_prepare_count > 1 || usm_ops.disabled)
+		return 0;
+	
+	usm_helper_data.pid = -1;
+
+	if (!*usm_helper_data.program)
+		return 0;
+
+	suspend_netlink_setup(&usm_helper_data);
+
+	if (usm_helper_data.pid == -1)
+		printk("Suspend2 Storage Manager wanted, but couldn't start it.\n");
+
+	suspend_activate_storage(0);
+
+	return (usm_helper_data.pid != -1);
+}
+
+void suspend_cleanup_usm(void)
+{
+	usm_prepare_count--;
+
+	if (usm_helper_data.pid > -1 && !usm_prepare_count) {
+		struct task_struct *t;
+
+		suspend_deactivate_storage(0);
+
+		suspend_send_netlink_message(&usm_helper_data,
+				NETLINK_MSG_CLEANUP, NULL, 0);
+
+		read_lock(&tasklist_lock);
+		if ((t = find_task_by_pid(usm_helper_data.pid)))
+			t->flags &= ~PF_NOFREEZE;
+		read_unlock(&tasklist_lock);
+
+		suspend_netlink_close(&usm_helper_data);
+
+		usm_helper_data.pid = -1;
+	}
+}
+

--
Nigel Cunningham nigel at suspend2 dot net

#9 lore raw

PATCH 9/13 Manually activate/deactivate the storage manager.

In reply to Nigel Cunningham

Provide (via a proc entry) a means whereby the user can manually activate and deactivate the storage manager for test and configuration purposes.

 kernel/power/storage.c |   13 +++++++++++++
 1 files changed, 13 insertions(+), 0 deletions(-)
kernel/power/storage.c +13 -0
index fea9276..aa140ea 100644
@@ -223,3 +223,16 @@ void suspend_cleanup_usm(void)
 	}
 }
 
+static void storage_manager_activate(void)
+{
+	if (storage_manager_action == storage_manager_last_action)
+		return;
+
+	if (storage_manager_action)
+		suspend_prepare_usm();
+	else
+		suspend_cleanup_usm();
+
+	storage_manager_last_action = storage_manager_action;
+}
+

--
Nigel Cunningham nigel at suspend2 dot net

#10 lore raw

PATCH 10/13 Storage manager proc entries.

In reply to Nigel Cunningham

Define the proc entries for the storage manager - there are entries for disabling the manager, setting the program, manually activating and simulating an atomic copy.

 kernel/power/storage.c |   48 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 48 insertions(+), 0 deletions(-)
kernel/power/storage.c +48 -0
index aa140ea..c8f85db 100644
@@ -236,3 +236,51 @@ static void storage_manager_activate(voi
 	storage_manager_last_action = storage_manager_action;
 }
 
+/*
+ * User interface specific /proc/suspend entries.
+ */
+
+static struct suspend_proc_data proc_params[] = {
+	{ .filename			= "disable_storage_manager",
+	  .permissions			= PROC_RW,
+	  .type				= SUSPEND_PROC_DATA_INTEGER,
+	  .data = {
+		.integer = {
+			.variable	= &usm_ops.disabled,
+			.minimum	= 0,
+			.maximum	= 1,
+		}
+	  }
+	},
+	{ .filename			= "storage_manager",
+	  .permissions			= PROC_RW,
+	  .type				= SUSPEND_PROC_DATA_STRING,
+	  .data = {
+		.string = {
+			.variable	= usm_helper_data.program,
+			.max_length	= 254,
+		}
+	  }
+	},
+	{ .filename			= "activate_storage",
+	  .permissions			= PROC_RW,
+	  .type				= SUSPEND_PROC_DATA_INTEGER,
+	  .data = {
+		.integer = {
+			.variable	= &storage_manager_action,
+			.minimum	= 0,
+			.maximum	= 1,
+		}
+	  },
+	  .write_proc 			= storage_manager_activate,
+	},
+
+#ifdef CONFIG_PM_DEBUG
+	{ .filename			= "simulate_atomic_copy",
+	  .permissions			= PROC_RW,
+	  .type				= SUSPEND_PROC_DATA_NONE,
+	  .write_proc 			= storage_manager_simulate,
+	}
+#endif
+};
+

--
Nigel Cunningham nigel at suspend2 dot net

#11 lore raw

PATCH 11/13 Storage manager module ops.

In reply to Nigel Cunningham

Define the operations that the core can use to access storage manager functionality and data.

 kernel/power/storage.c |   10 ++++++++++
 1 files changed, 10 insertions(+), 0 deletions(-)
kernel/power/storage.c +10 -0
index c8f85db..885501b 100644
@@ -284,3 +284,13 @@ static struct suspend_proc_data proc_par
 #endif
 };
 
+static struct suspend_module_ops usm_ops = {
+	.type				= MISC_MODULE,
+	.name				= "Userspace Storage Manager",
+	.module				= THIS_MODULE,
+	.storage_needed			= usm_storage_needed,
+	.save_config_info		= usm_save_config_info,
+	.load_config_info		= usm_load_config_info,
+	.memory_needed			= usm_memory_needed,
+};
+       

--
Nigel Cunningham nigel at suspend2 dot net

#12 lore raw

PATCH 12/13 Initialisation of the storage manager on load.

In reply to Nigel Cunningham

Register the proc entries for the storage manager and set up the netlink data structure when loaded.

 kernel/power/storage.c |   27 +++++++++++++++++++++++++++
 1 files changed, 27 insertions(+), 0 deletions(-)
kernel/power/storage.c +27 -0
index 885501b..bdb2993 100644
@@ -294,3 +294,30 @@ static struct suspend_module_ops usm_ops
 	.memory_needed			= usm_memory_needed,
 };
        
+/* suspend_usm_proc_init
+ * Description: Boot time initialisation for user interface.
+ */
+static __init int suspend_usm_proc_init(void)
+{
+	int result, i, numfiles = sizeof(proc_params) / sizeof(struct suspend_proc_data);
+
+	if (!(result = suspend_register_module(&usm_ops)))
+		for (i=0; i< numfiles; i++)
+			suspend_register_procfile(&proc_params[i]);
+
+	usm_helper_data.nl = NULL;
+	usm_helper_data.program[0] = '\0';
+	usm_helper_data.pid = -1;
+	usm_helper_data.skb_size = 0;
+	usm_helper_data.pool_limit = 6;
+	usm_helper_data.netlink_id = NETLINK_SUSPEND2_USM;
+	usm_helper_data.name = "userspace storage manager";
+	usm_helper_data.rcv_msg = usm_user_rcv_msg;
+	usm_helper_data.interface_version = 1;
+	usm_helper_data.must_init = 0;
+	init_completion(&usm_helper_data.wait_for_process);
+
+	return result;
+}
+
+late_initcall(suspend_usm_proc_init);

--
Nigel Cunningham nigel at suspend2 dot net

#13 lore raw

PATCH 13/13 Storage manager header file.

In reply to Nigel Cunningham

The header file for the storage manager, which defines the message numbers used by it, and the functions accessible via proc entries.

 kernel/power/storage.h |   37 +++++++++++++++++++++++++++++++++++++
 1 files changed, 37 insertions(+), 0 deletions(-)
kernel/power/storage.h new file +37 -0
new file mode 100644
index 0000000..9039dcc
@@ -0,0 +1,37 @@
+/*
+ * kernel/power/storage.h
+ *
+ * Copyright (C) 2005-2006 Nigel Cunningham <[email protected]>
+ *
+ * This file is released under the GPLv2.
+ */
+
+int suspend_prepare_usm(void);
+void suspend_cleanup_usm(void);
+
+#ifdef CONFIG_NET
+int suspend_activate_storage(int force);
+int suspend_deactivate_storage(int force);
+#else
+static inline int suspend_activate_storage(int force)
+{
+	return 0;
+}
+
+static inline int suspend_deactivate_storage(int force)
+{
+	return 0;
+}
+#endif
+
+enum {
+	USM_MSG_BASE = 0x10,
+
+	/* Kernel -> Userspace */
+	USM_MSG_CONNECT = 0x30,
+	USM_MSG_DISCONNECT = 0x31,
+	USM_MSG_SUCCESS = 0x40,
+	USM_MSG_FAILED = 0x41,
+
+	USM_MSG_MAX,
+};

--
Nigel Cunningham nigel at suspend2 dot net