aio.c 9.1 KB
Newer Older
J
Jeff Dike 已提交
1
/*
J
Jeff Dike 已提交
2
 * Copyright (C) 2004 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
J
Jeff Dike 已提交
3 4 5 6
 * Licensed under the GPL
 */

#include <unistd.h>
J
Jeff Dike 已提交
7
#include <sched.h>
J
Jeff Dike 已提交
8 9
#include <signal.h>
#include <errno.h>
J
Jeff Dike 已提交
10 11
#include <sys/time.h>
#include <asm/unistd.h>
J
Jeff Dike 已提交
12 13
#include "aio.h"
#include "init.h"
J
Jeff Dike 已提交
14
#include "kern_constants.h"
J
Jeff Dike 已提交
15
#include "kern_util.h"
J
Jeff Dike 已提交
16 17
#include "os.h"
#include "user.h"
J
Jeff Dike 已提交
18

19
struct aio_thread_req {
J
Jeff Dike 已提交
20 21 22 23 24 25
	enum aio_type type;
	int io_fd;
	unsigned long long offset;
	char *buf;
	int len;
	struct aio_context *aio;
26 27
};

J
Jeff Dike 已提交
28 29 30
#if defined(HAVE_AIO_ABI)
#include <linux/aio_abi.h>

J
Jeff Dike 已提交
31 32
/*
 * If we have the headers, we are going to build with AIO enabled.
J
Jeff Dike 已提交
33 34 35 36 37 38 39
 * If we don't have aio in libc, we define the necessary stubs here.
 */

#if !defined(HAVE_AIO_LIBC)

static long io_setup(int n, aio_context_t *ctxp)
{
J
Jeff Dike 已提交
40
	return syscall(__NR_io_setup, n, ctxp);
J
Jeff Dike 已提交
41 42 43 44
}

static long io_submit(aio_context_t ctx, long nr, struct iocb **iocbpp)
{
J
Jeff Dike 已提交
45
	return syscall(__NR_io_submit, ctx, nr, iocbpp);
J
Jeff Dike 已提交
46 47 48
}

static long io_getevents(aio_context_t ctx_id, long min_nr, long nr,
J
Jeff Dike 已提交
49
			 struct io_event *events, struct timespec *timeout)
J
Jeff Dike 已提交
50
{
J
Jeff Dike 已提交
51
	return syscall(__NR_io_getevents, ctx_id, min_nr, nr, events, timeout);
J
Jeff Dike 已提交
52 53 54 55
}

#endif

J
Jeff Dike 已提交
56 57
/*
 * The AIO_MMAP cases force the mmapped page into memory here
J
Jeff Dike 已提交
58 59 60 61 62 63 64 65 66 67
 * rather than in whatever place first touches the data.  I used
 * to do this by touching the page, but that's delicate because
 * gcc is prone to optimizing that away.  So, what's done here
 * is we read from the descriptor from which the page was
 * mapped.  The caller is required to pass an offset which is
 * inside the page that was mapped.  Thus, when the read
 * returns, we know that the page is in the page cache, and
 * that it now backs the mmapped area.
 */

68
static int do_aio(aio_context_t ctx, enum aio_type type, int fd, char *buf,
J
Jeff Dike 已提交
69
		  int len, unsigned long long offset, struct aio_context *aio)
J
Jeff Dike 已提交
70
{
J
Jeff Dike 已提交
71 72 73 74 75 76 77
	struct iocb *iocbp = & ((struct iocb) {
				    .aio_data       = (unsigned long) aio,
				    .aio_fildes     = fd,
				    .aio_buf        = (unsigned long) buf,
				    .aio_nbytes     = len,
				    .aio_offset     = offset
			     });
J
Jeff Dike 已提交
78
	char c;
J
Jeff Dike 已提交
79 80

	switch (type) {
J
Jeff Dike 已提交
81
	case AIO_READ:
J
Jeff Dike 已提交
82
		iocbp->aio_lio_opcode = IOCB_CMD_PREAD;
J
Jeff Dike 已提交
83 84
		break;
	case AIO_WRITE:
J
Jeff Dike 已提交
85
		iocbp->aio_lio_opcode = IOCB_CMD_PWRITE;
J
Jeff Dike 已提交
86 87
		break;
	case AIO_MMAP:
J
Jeff Dike 已提交
88 89 90
		iocbp->aio_lio_opcode = IOCB_CMD_PREAD;
		iocbp->aio_buf = (unsigned long) &c;
		iocbp->aio_nbytes = sizeof(c);
J
Jeff Dike 已提交
91 92
		break;
	default:
J
Jeff Dike 已提交
93 94
		printk(UM_KERN_ERR "Bogus op in do_aio - %d\n", type);
		return -EINVAL;
J
Jeff Dike 已提交
95 96
	}

J
Jeff Dike 已提交
97
	return (io_submit(ctx, 1, &iocbp) > 0) ? 0 : -errno;
J
Jeff Dike 已提交
98 99
}

J
Jeff Dike 已提交
100
/* Initialized in an initcall and unchanged thereafter */
J
Jeff Dike 已提交
101 102 103 104
static aio_context_t ctx = 0;

static int aio_thread(void *arg)
{
J
Jeff Dike 已提交
105 106 107 108 109 110
	struct aio_thread_reply reply;
	struct io_event event;
	int err, n, reply_fd;

	signal(SIGWINCH, SIG_IGN);

J
Jeff Dike 已提交
111
	while (1) {
J
Jeff Dike 已提交
112
		n = io_getevents(ctx, 1, 1, &event, NULL);
J
Jeff Dike 已提交
113 114
		if (n < 0) {
			if (errno == EINTR)
J
Jeff Dike 已提交
115
				continue;
J
Jeff Dike 已提交
116
			printk(UM_KERN_ERR "aio_thread - io_getevents failed, "
J
Jeff Dike 已提交
117 118 119 120 121 122
			       "errno = %d\n", errno);
		}
		else {
			reply = ((struct aio_thread_reply)
				{ .data = (void *) (long) event.data,
						.err	= event.res });
123
			reply_fd = ((struct aio_context *) reply.data)->reply_fd;
124
			err = write(reply_fd, &reply, sizeof(reply));
J
Jeff Dike 已提交
125 126 127
			if (err != sizeof(reply))
				printk(UM_KERN_ERR "aio_thread - write failed, "
				       "fd = %d, err = %d\n", reply_fd, errno);
J
Jeff Dike 已提交
128 129 130
		}
	}
	return 0;
J
Jeff Dike 已提交
131 132 133 134
}

#endif

135
static int do_not_aio(struct aio_thread_req *req)
J
Jeff Dike 已提交
136
{
J
Jeff Dike 已提交
137
	char c;
J
Jeff Dike 已提交
138
	unsigned long long actual;
139
	int n;
J
Jeff Dike 已提交
140

J
Jeff Dike 已提交
141
	actual = lseek64(req->io_fd, req->offset, SEEK_SET);
J
Jeff Dike 已提交
142
	if (actual != req->offset)
J
Jeff Dike 已提交
143 144
		return -errno;

J
Jeff Dike 已提交
145
	switch(req->type) {
J
Jeff Dike 已提交
146
	case AIO_READ:
147
		n = read(req->io_fd, req->buf, req->len);
J
Jeff Dike 已提交
148 149
		break;
	case AIO_WRITE:
150
		n = write(req->io_fd, req->buf, req->len);
J
Jeff Dike 已提交
151 152
		break;
	case AIO_MMAP:
153
		n = read(req->io_fd, &c, sizeof(c));
J
Jeff Dike 已提交
154 155
		break;
	default:
J
Jeff Dike 已提交
156 157
		printk(UM_KERN_ERR "do_not_aio - bad request type : %d\n",
		       req->type);
158
		return -EINVAL;
J
Jeff Dike 已提交
159 160
	}

J
Jeff Dike 已提交
161
	if (n < 0)
162 163
		return -errno;
	return 0;
J
Jeff Dike 已提交
164 165
}

J
Jeff Dike 已提交
166 167 168 169
/* These are initialized in initcalls and not changed */
static int aio_req_fd_r = -1;
static int aio_req_fd_w = -1;
static int aio_pid = -1;
J
Jeff Dike 已提交
170
static unsigned long aio_stack;
J
Jeff Dike 已提交
171

J
Jeff Dike 已提交
172 173
static int not_aio_thread(void *arg)
{
J
Jeff Dike 已提交
174 175 176 177 178
	struct aio_thread_req req;
	struct aio_thread_reply reply;
	int err;

	signal(SIGWINCH, SIG_IGN);
J
Jeff Dike 已提交
179
	while (1) {
180
		err = read(aio_req_fd_r, &req, sizeof(req));
J
Jeff Dike 已提交
181 182 183 184 185
		if (err != sizeof(req)) {
			if (err < 0)
				printk(UM_KERN_ERR "not_aio_thread - "
				       "read failed, fd = %d, err = %d\n",
				       aio_req_fd_r,
186
				       errno);
J
Jeff Dike 已提交
187
			else {
J
Jeff Dike 已提交
188 189 190
				printk(UM_KERN_ERR "not_aio_thread - short "
				       "read, fd = %d, length = %d\n",
				       aio_req_fd_r, err);
J
Jeff Dike 已提交
191 192 193 194 195
			}
			continue;
		}
		err = do_not_aio(&req);
		reply = ((struct aio_thread_reply) { .data 	= req.aio,
J
Jeff Dike 已提交
196
						     .err	= err });
197
		err = write(req.aio->reply_fd, &reply, sizeof(reply));
J
Jeff Dike 已提交
198 199 200
		if (err != sizeof(reply))
			printk(UM_KERN_ERR "not_aio_thread - write failed, "
			       "fd = %d, err = %d\n", req.aio->reply_fd, errno);
J
Jeff Dike 已提交
201
	}
202 203

	return 0;
J
Jeff Dike 已提交
204 205 206 207
}

static int init_aio_24(void)
{
J
Jeff Dike 已提交
208 209 210
	int fds[2], err;

	err = os_pipe(fds, 1, 1);
J
Jeff Dike 已提交
211
	if (err)
J
Jeff Dike 已提交
212 213 214 215
		goto out;

	aio_req_fd_w = fds[0];
	aio_req_fd_r = fds[1];
J
Jeff Dike 已提交
216 217

	err = os_set_fd_block(aio_req_fd_w, 0);
J
Jeff Dike 已提交
218
	if (err)
J
Jeff Dike 已提交
219 220
		goto out_close_pipe;

J
Jeff Dike 已提交
221
	err = run_helper_thread(not_aio_thread, NULL,
222
				CLONE_FILES | CLONE_VM, &aio_stack);
J
Jeff Dike 已提交
223
	if (err < 0)
J
Jeff Dike 已提交
224 225 226 227 228 229
		goto out_close_pipe;

	aio_pid = err;
	goto out;

out_close_pipe:
230 231
	close(fds[0]);
	close(fds[1]);
J
Jeff Dike 已提交
232 233 234
	aio_req_fd_w = -1;
	aio_req_fd_r = -1;
out:
J
Jeff Dike 已提交
235
#ifndef HAVE_AIO_ABI
J
Jeff Dike 已提交
236 237
	printk(UM_KERN_INFO "/usr/include/linux/aio_abi.h not present during "
	       "build\n");
J
Jeff Dike 已提交
238
#endif
J
Jeff Dike 已提交
239 240
	printk(UM_KERN_INFO "2.6 host AIO support not used - falling back to "
	       "I/O thread\n");
J
Jeff Dike 已提交
241
	return 0;
J
Jeff Dike 已提交
242 243 244 245 246 247
}

#ifdef HAVE_AIO_ABI
#define DEFAULT_24_AIO 0
static int init_aio_26(void)
{
J
Jeff Dike 已提交
248
	int err;
J
Jeff Dike 已提交
249

J
Jeff Dike 已提交
250
	if (io_setup(256, &ctx)) {
251
		err = -errno;
J
Jeff Dike 已提交
252 253
		printk(UM_KERN_ERR "aio_thread failed to initialize context, "
		       "err = %d\n", errno);
J
Jeff Dike 已提交
254 255
		return err;
	}
J
Jeff Dike 已提交
256

J
Jeff Dike 已提交
257
	err = run_helper_thread(aio_thread, NULL,
258
				CLONE_FILES | CLONE_VM, &aio_stack);
J
Jeff Dike 已提交
259
	if (err < 0)
J
Jeff Dike 已提交
260
		return err;
J
Jeff Dike 已提交
261

J
Jeff Dike 已提交
262
	aio_pid = err;
J
Jeff Dike 已提交
263

J
Jeff Dike 已提交
264
	printk(UM_KERN_INFO "Using 2.6 host AIO\n");
J
Jeff Dike 已提交
265
	return 0;
266 267 268 269 270
}

static int submit_aio_26(enum aio_type type, int io_fd, char *buf, int len,
			 unsigned long long offset, struct aio_context *aio)
{
J
Jeff Dike 已提交
271 272 273 274
	struct aio_thread_reply reply;
	int err;

	err = do_aio(ctx, type, io_fd, buf, len, offset, aio);
J
Jeff Dike 已提交
275
	if (err) {
J
Jeff Dike 已提交
276 277
		reply = ((struct aio_thread_reply) { .data = aio,
					 .err  = err });
278
		err = write(aio->reply_fd, &reply, sizeof(reply));
J
Jeff Dike 已提交
279
		if (err != sizeof(reply)) {
280
			err = -errno;
J
Jeff Dike 已提交
281
			printk(UM_KERN_ERR "submit_aio_26 - write failed, "
J
Jeff Dike 已提交
282
			       "fd = %d, err = %d\n", aio->reply_fd, -err);
283
		}
J
Jeff Dike 已提交
284 285 286 287
		else err = 0;
	}

	return err;
J
Jeff Dike 已提交
288 289 290 291
}

#else
#define DEFAULT_24_AIO 1
292
static int init_aio_26(void)
J
Jeff Dike 已提交
293
{
J
Jeff Dike 已提交
294
	return -ENOSYS;
J
Jeff Dike 已提交
295 296
}

297 298
static int submit_aio_26(enum aio_type type, int io_fd, char *buf, int len,
			 unsigned long long offset, struct aio_context *aio)
J
Jeff Dike 已提交
299
{
J
Jeff Dike 已提交
300
	return -ENOSYS;
J
Jeff Dike 已提交
301 302 303
}
#endif

J
Jeff Dike 已提交
304
/* Initialized in an initcall and unchanged thereafter */
J
Jeff Dike 已提交
305 306 307 308
static int aio_24 = DEFAULT_24_AIO;

static int __init set_aio_24(char *name, int *add)
{
J
Jeff Dike 已提交
309 310
	aio_24 = 1;
	return 0;
J
Jeff Dike 已提交
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
}

__uml_setup("aio=2.4", set_aio_24,
"aio=2.4\n"
"    This is used to force UML to use 2.4-style AIO even when 2.6 AIO is\n"
"    available.  2.4 AIO is a single thread that handles one request at a\n"
"    time, synchronously.  2.6 AIO is a thread which uses the 2.6 AIO \n"
"    interface to handle an arbitrary number of pending requests.  2.6 AIO \n"
"    is not available in tt mode, on 2.4 hosts, or when UML is built with\n"
"    /usr/include/linux/aio_abi.h not available.  Many distributions don't\n"
"    include aio_abi.h, so you will need to copy it from a kernel tree to\n"
"    your /usr/include/linux in order to build an AIO-capable UML\n\n"
);

static int init_aio(void)
{
J
Jeff Dike 已提交
327 328
	int err;

J
Jeff Dike 已提交
329
	if (!aio_24) {
J
Jeff Dike 已提交
330
		err = init_aio_26();
J
Jeff Dike 已提交
331 332 333
		if (err && (errno == ENOSYS)) {
			printk(UM_KERN_INFO "2.6 AIO not supported on the "
			       "host - reverting to 2.4 AIO\n");
J
Jeff Dike 已提交
334 335 336 337 338
			aio_24 = 1;
		}
		else return err;
	}

J
Jeff Dike 已提交
339
	if (aio_24)
J
Jeff Dike 已提交
340 341 342
		return init_aio_24();

	return 0;
J
Jeff Dike 已提交
343 344
}

J
Jeff Dike 已提交
345 346
/*
 * The reason for the __initcall/__uml_exitcall asymmetry is that init_aio
J
Jeff Dike 已提交
347 348 349 350 351 352 353 354 355
 * needs to be called when the kernel is running because it calls run_helper,
 * which needs get_free_page.  exit_aio is a __uml_exitcall because the generic
 * kernel does not run __exitcalls on shutdown, and can't because many of them
 * break when called outside of module unloading.
 */
__initcall(init_aio);

static void exit_aio(void)
{
J
Jeff Dike 已提交
356
	if (aio_pid != -1) {
J
Jeff Dike 已提交
357
		os_kill_process(aio_pid, 1);
J
Jeff Dike 已提交
358 359
		free_stack(aio_stack, 0);
	}
J
Jeff Dike 已提交
360 361 362 363
}

__uml_exitcall(exit_aio);

364 365
static int submit_aio_24(enum aio_type type, int io_fd, char *buf, int len,
			 unsigned long long offset, struct aio_context *aio)
J
Jeff Dike 已提交
366
{
J
Jeff Dike 已提交
367 368 369 370 371 372 373 374 375
	struct aio_thread_req req = { .type 		= type,
				      .io_fd		= io_fd,
				      .offset		= offset,
				      .buf		= buf,
				      .len		= len,
				      .aio		= aio,
	};
	int err;

376
	err = write(aio_req_fd_w, &req, sizeof(req));
J
Jeff Dike 已提交
377
	if (err == sizeof(req))
J
Jeff Dike 已提交
378
		err = 0;
379
	else err = -errno;
J
Jeff Dike 已提交
380 381

	return err;
382 383 384
}

int submit_aio(enum aio_type type, int io_fd, char *buf, int len,
J
Jeff Dike 已提交
385 386
	       unsigned long long offset, int reply_fd,
	       struct aio_context *aio)
387
{
J
Jeff Dike 已提交
388
	aio->reply_fd = reply_fd;
J
Jeff Dike 已提交
389
	if (aio_24)
J
Jeff Dike 已提交
390
		return submit_aio_24(type, io_fd, buf, len, offset, aio);
J
Jeff Dike 已提交
391
	else
J
Jeff Dike 已提交
392
		return submit_aio_26(type, io_fd, buf, len, offset, aio);
J
Jeff Dike 已提交
393
}