aio.c 9.1 KB
Newer Older
J
Jeff Dike 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright (C) 2004 Jeff Dike (jdike@addtoit.com)
 * Licensed under the GPL
 */

#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <sched.h>
#include <sys/syscall.h>
#include "os.h"
#include "aio.h"
#include "init.h"
#include "user.h"
#include "mode.h"

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

J
Jeff Dike 已提交
27 28 29 30 31 32 33 34 35 36 37
#if defined(HAVE_AIO_ABI)
#include <linux/aio_abi.h>

/* If we have the headers, we are going to build with AIO enabled.
 * 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 已提交
38
	return syscall(__NR_io_setup, n, ctxp);
J
Jeff Dike 已提交
39 40 41 42
}

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

static long io_getevents(aio_context_t ctx_id, long min_nr, long nr,
J
Jeff Dike 已提交
47
			 struct io_event *events, struct timespec *timeout)
J
Jeff Dike 已提交
48
{
J
Jeff Dike 已提交
49
	return syscall(__NR_io_getevents, ctx_id, min_nr, nr, events, timeout);
J
Jeff Dike 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
}

#endif

/* The AIO_MMAP cases force the mmapped page into memory here
 * 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.
 */

65
static int do_aio(aio_context_t ctx, enum aio_type type, int fd, char *buf,
J
Jeff Dike 已提交
66
		  int len, unsigned long long offset, struct aio_context *aio)
J
Jeff Dike 已提交
67
{
J
Jeff Dike 已提交
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
	struct iocb iocb, *iocbp = &iocb;
	char c;
	int err;

	iocb = ((struct iocb) { .aio_data 	= (unsigned long) aio,
				.aio_reqprio	= 0,
				.aio_fildes	= fd,
				.aio_buf	= (unsigned long) buf,
				.aio_nbytes	= len,
				.aio_offset	= offset,
				.aio_reserved1	= 0,
				.aio_reserved2	= 0,
				.aio_reserved3	= 0 });

	switch(type){
	case AIO_READ:
		iocb.aio_lio_opcode = IOCB_CMD_PREAD;
		err = io_submit(ctx, 1, &iocbp);
		break;
	case AIO_WRITE:
		iocb.aio_lio_opcode = IOCB_CMD_PWRITE;
		err = io_submit(ctx, 1, &iocbp);
		break;
	case AIO_MMAP:
		iocb.aio_lio_opcode = IOCB_CMD_PREAD;
		iocb.aio_buf = (unsigned long) &c;
		iocb.aio_nbytes = sizeof(c);
		err = io_submit(ctx, 1, &iocbp);
		break;
	default:
		printk("Bogus op in do_aio - %d\n", type);
		err = -EINVAL;
		break;
	}

	if(err > 0)
		err = 0;
105 106
	else
		err = -errno;
J
Jeff Dike 已提交
107

J
Jeff Dike 已提交
108
	return err;
J
Jeff Dike 已提交
109 110
}

J
Jeff Dike 已提交
111
/* Initialized in an initcall and unchanged thereafter */
J
Jeff Dike 已提交
112 113 114 115
static aio_context_t ctx = 0;

static int aio_thread(void *arg)
{
J
Jeff Dike 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
	struct aio_thread_reply reply;
	struct io_event event;
	int err, n, reply_fd;

	signal(SIGWINCH, SIG_IGN);

	while(1){
		n = io_getevents(ctx, 1, 1, &event, NULL);
		if(n < 0){
			if(errno == EINTR)
				continue;
			printk("aio_thread - io_getevents failed, "
			       "errno = %d\n", errno);
		}
		else {
			reply = ((struct aio_thread_reply)
				{ .data = (void *) (long) event.data,
						.err	= event.res });
134
			reply_fd = ((struct aio_context *) reply.data)->reply_fd;
135
			err = write(reply_fd, &reply, sizeof(reply));
J
Jeff Dike 已提交
136
			if(err != sizeof(reply))
137
				printk("aio_thread - write failed, fd = %d, "
138
				       "err = %d\n", reply_fd, errno);
J
Jeff Dike 已提交
139 140 141
		}
	}
	return 0;
J
Jeff Dike 已提交
142 143 144 145
}

#endif

146
static int do_not_aio(struct aio_thread_req *req)
J
Jeff Dike 已提交
147
{
J
Jeff Dike 已提交
148
	char c;
J
Jeff Dike 已提交
149
	unsigned long long actual;
150
	int n;
J
Jeff Dike 已提交
151

J
Jeff Dike 已提交
152 153 154 155
	actual = lseek64(req->io_fd, req->offset, SEEK_SET);
	if(actual != req->offset)
		return -errno;

J
Jeff Dike 已提交
156 157
	switch(req->type){
	case AIO_READ:
158
		n = read(req->io_fd, req->buf, req->len);
J
Jeff Dike 已提交
159 160
		break;
	case AIO_WRITE:
161
		n = write(req->io_fd, req->buf, req->len);
J
Jeff Dike 已提交
162 163
		break;
	case AIO_MMAP:
164
		n = read(req->io_fd, &c, sizeof(c));
J
Jeff Dike 已提交
165 166 167
		break;
	default:
		printk("do_not_aio - bad request type : %d\n", req->type);
168
		return -EINVAL;
J
Jeff Dike 已提交
169 170
	}

171 172 173
	if(n < 0)
		return -errno;
	return 0;
J
Jeff Dike 已提交
174 175
}

J
Jeff Dike 已提交
176 177 178 179 180
/* 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 已提交
181 182
static int not_aio_thread(void *arg)
{
J
Jeff Dike 已提交
183 184 185 186 187 188
	struct aio_thread_req req;
	struct aio_thread_reply reply;
	int err;

	signal(SIGWINCH, SIG_IGN);
	while(1){
189
		err = read(aio_req_fd_r, &req, sizeof(req));
J
Jeff Dike 已提交
190 191 192 193
		if(err != sizeof(req)){
			if(err < 0)
				printk("not_aio_thread - read failed, "
				       "fd = %d, err = %d\n", aio_req_fd_r,
194
				       errno);
J
Jeff Dike 已提交
195 196 197 198 199 200 201 202
			else {
				printk("not_aio_thread - short read, fd = %d, "
				       "length = %d\n", aio_req_fd_r, err);
			}
			continue;
		}
		err = do_not_aio(&req);
		reply = ((struct aio_thread_reply) { .data 	= req.aio,
J
Jeff Dike 已提交
203
						     .err	= err });
204
		err = write(req.aio->reply_fd, &reply, sizeof(reply));
J
Jeff Dike 已提交
205 206
		if(err != sizeof(reply))
			printk("not_aio_thread - write failed, fd = %d, "
207
			       "err = %d\n", req.aio->reply_fd, errno);
J
Jeff Dike 已提交
208
	}
209 210

	return 0;
J
Jeff Dike 已提交
211 212 213 214
}

static int init_aio_24(void)
{
J
Jeff Dike 已提交
215 216 217 218 219 220 221 222 223
	unsigned long stack;
	int fds[2], err;

	err = os_pipe(fds, 1, 1);
	if(err)
		goto out;

	aio_req_fd_w = fds[0];
	aio_req_fd_r = fds[1];
J
Jeff Dike 已提交
224 225 226 227 228

	err = os_set_fd_block(aio_req_fd_w, 0);
	if(err)
		goto out_close_pipe;

J
Jeff Dike 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241 242
	err = run_helper_thread(not_aio_thread, NULL,
				CLONE_FILES | CLONE_VM | SIGCHLD, &stack, 0);
	if(err < 0)
		goto out_close_pipe;

	aio_pid = err;
	goto out;

out_close_pipe:
	os_close_file(fds[0]);
	os_close_file(fds[1]);
	aio_req_fd_w = -1;
	aio_req_fd_r = -1;
out:
J
Jeff Dike 已提交
243 244 245 246 247
#ifndef HAVE_AIO_ABI
	printk("/usr/include/linux/aio_abi.h not present during build\n");
#endif
	printk("2.6 host AIO support not used - falling back to I/O "
	       "thread\n");
J
Jeff Dike 已提交
248
	return 0;
J
Jeff Dike 已提交
249 250 251 252 253 254
}

#ifdef HAVE_AIO_ABI
#define DEFAULT_24_AIO 0
static int init_aio_26(void)
{
J
Jeff Dike 已提交
255 256
	unsigned long stack;
	int err;
J
Jeff Dike 已提交
257

J
Jeff Dike 已提交
258
	if(io_setup(256, &ctx)){
259
		err = -errno;
J
Jeff Dike 已提交
260 261 262 263
		printk("aio_thread failed to initialize context, err = %d\n",
		       errno);
		return err;
	}
J
Jeff Dike 已提交
264

J
Jeff Dike 已提交
265 266 267 268
	err = run_helper_thread(aio_thread, NULL,
				CLONE_FILES | CLONE_VM | SIGCHLD, &stack, 0);
	if(err < 0)
		return err;
J
Jeff Dike 已提交
269

J
Jeff Dike 已提交
270
	aio_pid = err;
J
Jeff Dike 已提交
271 272

	printk("Using 2.6 host AIO\n");
J
Jeff Dike 已提交
273
	return 0;
274 275 276 277 278
}

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 已提交
279 280 281 282 283 284 285
	struct aio_thread_reply reply;
	int err;

	err = do_aio(ctx, type, io_fd, buf, len, offset, aio);
	if(err){
		reply = ((struct aio_thread_reply) { .data = aio,
					 .err  = err });
286 287 288
		err = write(aio->reply_fd, &reply, sizeof(reply));
		if(err != sizeof(reply)){
			err = -errno;
J
Jeff Dike 已提交
289 290
			printk("submit_aio_26 - write failed, "
			       "fd = %d, err = %d\n", aio->reply_fd, -err);
291
		}
J
Jeff Dike 已提交
292 293 294 295
		else err = 0;
	}

	return err;
J
Jeff Dike 已提交
296 297 298 299
}

#else
#define DEFAULT_24_AIO 1
300
static int init_aio_26(void)
J
Jeff Dike 已提交
301
{
J
Jeff Dike 已提交
302
	return -ENOSYS;
J
Jeff Dike 已提交
303 304
}

305 306
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 已提交
307
{
J
Jeff Dike 已提交
308
	return -ENOSYS;
J
Jeff Dike 已提交
309 310 311
}
#endif

J
Jeff Dike 已提交
312
/* Initialized in an initcall and unchanged thereafter */
J
Jeff Dike 已提交
313 314 315 316
static int aio_24 = DEFAULT_24_AIO;

static int __init set_aio_24(char *name, int *add)
{
J
Jeff Dike 已提交
317 318
	aio_24 = 1;
	return 0;
J
Jeff Dike 已提交
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
}

__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 已提交
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
	int err;

	CHOOSE_MODE(({ if(!aio_24){
			    printk("Disabling 2.6 AIO in tt mode\n");
			    aio_24 = 1;
		    } }), (void) 0);

	if(!aio_24){
		err = init_aio_26();
		if(err && (errno == ENOSYS)){
			printk("2.6 AIO not supported on the host - "
			       "reverting to 2.4 AIO\n");
			aio_24 = 1;
		}
		else return err;
	}

	if(aio_24)
		return init_aio_24();

	return 0;
J
Jeff Dike 已提交
356 357 358 359 360 361 362 363 364 365 366 367
}

/* The reason for the __initcall/__uml_exitcall asymmetry is that init_aio
 * 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 已提交
368 369
	if(aio_pid != -1)
		os_kill_process(aio_pid, 1);
J
Jeff Dike 已提交
370 371 372 373
}

__uml_exitcall(exit_aio);

374 375
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 已提交
376
{
J
Jeff Dike 已提交
377 378 379 380 381 382 383 384 385
	struct aio_thread_req req = { .type 		= type,
				      .io_fd		= io_fd,
				      .offset		= offset,
				      .buf		= buf,
				      .len		= len,
				      .aio		= aio,
	};
	int err;

386
	err = write(aio_req_fd_w, &req, sizeof(req));
J
Jeff Dike 已提交
387 388
	if(err == sizeof(req))
		err = 0;
389
	else err = -errno;
J
Jeff Dike 已提交
390 391

	return err;
392 393 394
}

int submit_aio(enum aio_type type, int io_fd, char *buf, int len,
J
Jeff Dike 已提交
395 396
	       unsigned long long offset, int reply_fd,
	       struct aio_context *aio)
397
{
J
Jeff Dike 已提交
398 399 400 401 402 403
	aio->reply_fd = reply_fd;
	if(aio_24)
		return submit_aio_24(type, io_fd, buf, len, offset, aio);
	else {
		return submit_aio_26(type, io_fd, buf, len, offset, aio);
	}
J
Jeff Dike 已提交
404
}