File: cr_async.c

package info (click to toggle)
blcr 0.8.5-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 10,736 kB
  • sloc: ansic: 31,999; sh: 12,633; makefile: 940; perl: 401; cpp: 79; java: 9
file content (229 lines) | stat: -rw-r--r-- 6,053 bytes parent folder | download | duplicates (4)
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
/*
 * Berkeley Lab Checkpoint/Restart (BLCR) for Linux is Copyright (c)
 * 2003, The Regents of the University of California, through Lawrence
 * Berkeley National Laboratory (subject to receipt of any required
 * approvals from the U.S. Dept. of Energy).  All rights reserved.
 *
 * Portions may be copyrighted by others, as may be noted in specific
 * copyright notices within specific files.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * $Id: cr_async.c,v 1.31 2008/08/21 05:22:49 phargrov Exp $
 *
 *
 * This file deals with Phase1 threads/tasks.
 * The filename "cr_async.c" is legacy from when these were
 * known as "asynchronous threads/tasks".
 */

#include "cr_module.h"

#include <asm/uaccess.h>

// cr_phase1_release(filp, priv)
//
// Remove Phase1 tasks from the task list when the file is closed.
// Note that the close might be implictly at exit and could also
// come from a task other than the one originally registered.
//
// XXX: could keep a hash mapping phase1_filp -> cr_task rather than
// walking the entire list each time.
void cr_phase1_release(struct file *filp, cr_pdata_t *priv)
{
	cr_task_t *cr_task, *next;

	write_lock(&cr_task_lock);
	list_for_each_entry_safe(cr_task, next, &cr_task_list, task_list) {
		if ((cr_task->filp == filp) && (cr_task->phase == CR_PHASE1)) {
			cr_task->phase = CR_NO_PHASE;
			cr_task->filp = NULL;
			cr_task->fd = -1;
			__cr_task_put(cr_task);
		}
	}
	write_unlock(&cr_task_lock);
}

static
int do_suspend(struct file *filp, struct timeval *tvp)
{
	cr_task_t	*cr_task;
	unsigned long	timeout;
	int		retval = 0;
	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(dummy);

	CR_KTRACE_FUNC_ENTRY();

	cr_task = cr_task_get(current);
	if (!cr_task) {
		retval = -EINVAL;
	        CR_KTRACE_FUNC_EXIT("due to -EINVAL");
		goto out;
	}

	// Decode the optional timeout
	timeout = tvp ? timeval_to_jiffies(tvp) : MAX_SCHEDULE_TIMEOUT;

	// Block until signal is pending or timeout
	// NOTE: Condition is checked only to ensure we don't sleep if already pending
	timeout = wait_event_interruptible_timeout(dummy, cr_task->chkpt_req, timeout);

	if (tvp) {
		jiffies_to_timeval(timeout, tvp);
	}

	if (!cr_task->chkpt_req && timeout) {
		// caught a signal other than the checkpoint trigger
		retval = -EINTR;
		CR_KTRACE_FUNC_EXIT("with pending signal");
	} else if (cr_task->chkpt_req) {
		CR_KTRACE_FUNC_EXIT("with pending checkpoint");
	} else if (!timeout) {
		CR_KTRACE_FUNC_EXIT("with expired timeout");
	} else {
		CR_KTRACE_FUNC_EXIT("for unknown reason");
	}

	cr_task_put(cr_task);
out:
	return retval;
}

// cr_suspend(timeval)
//
// Wait for a checkpoint request to arrive.
// Argument is a struct timeval for a bounded wait, or NULL to block forever.
//
// Returns:
//	<0 on errors (including interrupted sleep)
//	0 otherwise
//
int cr_suspend(struct file *filp, struct timeval __user *arg)
{
	struct timeval	timeval;
	int		retval;

	CR_KTRACE_FUNC_ENTRY();

	retval = -EFAULT;
	if (arg && copy_from_user(&timeval, arg, sizeof(timeval))) {
		goto out;
	} 

	retval = do_suspend(filp, arg ? &timeval : NULL);

	if (arg) {
		// like select() we fail silently if user timeval is R/O
		(void)put_user(timeval.tv_sec, &arg->tv_sec);
		(void)put_user(timeval.tv_usec, &arg->tv_usec);
	}

out:
	CR_KTRACE_FUNC_EXIT("Returning %d", retval);
	return retval;
}

// cr_phase1_register(file, arg)
//
// Registers the calling task as having a Phase1 checkpoint handler.
// Returns:
// 	0 on success
// 	-EAGAIN if a checkpoint is pending for this task
// 	-EEXIST if this task is already registered
// 	other < 0 on other error conditions
int cr_phase1_register(struct file *filp, int arg)
{
	cr_task_t	*cr_task;
	int		retval;

	CR_KTRACE_FUNC_ENTRY();

	write_lock(&cr_task_lock);
	cr_task = __cr_task_get(current, 1);

	retval = -ENOMEM;
	if (!cr_task) {
		// Can't create the task list entry.
		goto out_noput;
	}

	retval = -EAGAIN;
	if (cr_task->chkpt_req || cr_task->rstrt_req) {
		// Can't register a Phase1 handler task
		// while a checkpoint or restart is in progress.
		goto out;
	}

	retval = -EEXIST;
	if (cr_task->phase == CR_PHASE1) {
		// Duplicate registration
		goto out;
	}

	retval = -EINVAL;
	if (cr_task->phase == CR_PHASE2) {
		// Conflicting registration
		goto out;
	}

	retval = 0;
	cr_task->phase = CR_PHASE1;
	cr_task->filp = filp;
	cr_task->fd = arg;
	atomic_inc(&cr_task->ref_count);

	// We assume our proper signal handler is registered at this time.
	// So, we save it for later use in case the user has overwitten it.
	CR_SIGNAL_LOCK(current);
	cr_task->handler_sa = CR_SIGACTION(current, CR_SIGNUM);
	CR_SIGNAL_UNLOCK(current);

out:
	__cr_task_put(cr_task);
out_noput:
	write_unlock(&cr_task_lock);

	return retval;
}

#ifdef CONFIG_COMPAT
int cr_suspend32(struct file *filp, struct compat_timeval __user *arg)
{
	struct timeval	timeval;
	int		retval;

	CR_KTRACE_FUNC_ENTRY();

	retval = -EFAULT;
        if (arg && (!access_ok(VERIFY_READ, arg, sizeof(*arg)) ||
		    __get_user(timeval.tv_sec, &arg->tv_sec) ||
		    __get_user(timeval.tv_usec, &arg->tv_usec))) {
		goto out;
	} 

	retval = do_suspend(filp, arg ? &timeval : NULL);

	if (arg) {
		// like select() we fail silently if user timeval is R/O
		(void)put_user(timeval.tv_sec, &arg->tv_sec);
		(void)put_user(timeval.tv_usec, &arg->tv_usec);
	}

out:
	CR_KTRACE_FUNC_EXIT("Returning %d", retval);
	return retval;
}
#endif // CONFIG_COMPAT