summaryrefslogtreecommitdiff
path: root/kernel/power/tuxonice_storage.c
blob: e99f6e24f76d88ae62ed717927301167aa0cc6ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
 * kernel/power/tuxonice_storage.c
 *
 * Copyright (C) 2005-2015 Nigel Cunningham (nigel at nigelcunningham com au)
 *
 * 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 "tuxonice_sysfs.h"
#include "tuxonice_modules.h"
#include "tuxonice_netlink.h"
#include "tuxonice_storage.h"
#include "tuxonice_ui.h"

static struct user_helper_data usm_helper_data;
static struct toi_module_ops usm_ops;
static int message_received, usm_prepare_count;
static int storage_manager_last_action, storage_manager_action;

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 (!capable(CAP_NET_ADMIN))
		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(KERN_INFO "Storage manager doesn't recognise "
				"message %d.\n", type);
	}

	return 1;
}

#ifdef CONFIG_NET
static int activations;

int toi_activate_storage(int force)
{
	int tries = 1;

	if (usm_helper_data.pid == -1 || !usm_ops.enabled)
		return 0;

	message_received = 0;
	activations++;

	if (activations > 1 && !force)
		return 0;

	while ((!message_received || message_received == USM_MSG_FAILED) &&
			tries < 2) {
		toi_prepare_status(DONT_CLEAR_BAR, "Activate storage attempt "
				"%d.\n", tries);

		init_completion(&usm_helper_data.wait_for_process);

		toi_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 toi_deactivate_storage(int force)
{
	if (usm_helper_data.pid == -1 || !usm_ops.enabled)
		return 0;

	message_received = 0;
	activations--;

	if (activations && !force)
		return 0;

	init_completion(&usm_helper_data.wait_for_process);

	toi_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(KERN_INFO "Returning failure disconnecting storage.\n");
		return 1;
	}

	return 0;
}
#endif

static void storage_manager_simulate(void)
{
	printk(KERN_INFO "--- Storage manager simulate ---\n");
	toi_prepare_usm();
	schedule();
	printk(KERN_INFO "--- Activate storage 1 ---\n");
	toi_activate_storage(1);
	schedule();
	printk(KERN_INFO "--- Deactivate storage 1 ---\n");
	toi_deactivate_storage(1);
	schedule();
	printk(KERN_INFO "--- Cleanup usm ---\n");
	toi_cleanup_usm();
	schedule();
	printk(KERN_INFO "--- Storage manager simulate ends ---\n");
}

static int usm_storage_needed(void)
{
	return sizeof(int) + strlen(usm_helper_data.program) + 1;
}

static int usm_save_config_info(char *buf)
{
	int len = strlen(usm_helper_data.program);
	memcpy(buf, usm_helper_data.program, len + 1);
	return sizeof(int) + len + 1;
}

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 + sizeof(int), *((int *) buf));
}

static int usm_memory_needed(void)
{
	/* ball park figure of 32 pages */
	return 32 * PAGE_SIZE;
}

/* toi_prepare_usm
 */
int toi_prepare_usm(void)
{
	usm_prepare_count++;

	if (usm_prepare_count > 1 || !usm_ops.enabled)
		return 0;

	usm_helper_data.pid = -1;

	if (!*usm_helper_data.program)
		return 0;

	toi_netlink_setup(&usm_helper_data);

	if (usm_helper_data.pid == -1)
		printk(KERN_INFO "TuxOnIce Storage Manager wanted, but couldn't"
				" start it.\n");

	toi_activate_storage(0);

	return usm_helper_data.pid != -1;
}

void toi_cleanup_usm(void)
{
	usm_prepare_count--;

	if (usm_helper_data.pid > -1 && !usm_prepare_count) {
		toi_deactivate_storage(0);
		toi_netlink_close(&usm_helper_data);
	}
}

static void storage_manager_activate(void)
{
	if (storage_manager_action == storage_manager_last_action)
		return;

	if (storage_manager_action)
		toi_prepare_usm();
	else
		toi_cleanup_usm();

	storage_manager_last_action = storage_manager_action;
}

/*
 * User interface specific /sys/power/tuxonice entries.
 */

static struct toi_sysfs_data sysfs_params[] = {
	SYSFS_NONE("simulate_atomic_copy", storage_manager_simulate),
	SYSFS_INT("enabled", SYSFS_RW, &usm_ops.enabled, 0, 1, 0, NULL),
	SYSFS_STRING("program", SYSFS_RW, usm_helper_data.program, 254, 0,
		NULL),
	SYSFS_INT("activate_storage", SYSFS_RW , &storage_manager_action, 0, 1,
			0, storage_manager_activate)
};

static struct toi_module_ops usm_ops = {
	.type				= MISC_MODULE,
	.name				= "usm",
	.directory			= "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,

	.sysfs_data			= sysfs_params,
	.num_sysfs_entries		= sizeof(sysfs_params) /
		sizeof(struct toi_sysfs_data),
};

/* toi_usm_sysfs_init
 * Description: Boot time initialisation for user interface.
 */
int toi_usm_init(void)
{
	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_TOI_USM;
	usm_helper_data.name = "userspace storage manager";
	usm_helper_data.rcv_msg = usm_user_rcv_msg;
	usm_helper_data.interface_version = 2;
	usm_helper_data.must_init = 0;
	init_completion(&usm_helper_data.wait_for_process);

	return toi_register_module(&usm_ops);
}

void toi_usm_exit(void)
{
	toi_netlink_close_complete(&usm_helper_data);
	toi_unregister_module(&usm_ops);
}