aio.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * Copyright (C) 2004 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Licensed under the GPL
  4. */
  5. #include <unistd.h>
  6. #include <sched.h>
  7. #include <signal.h>
  8. #include <errno.h>
  9. #include <sys/time.h>
  10. #include <asm/unistd.h>
  11. #include "aio.h"
  12. #include "init.h"
  13. #include "kern_constants.h"
  14. #include "os.h"
  15. #include "user.h"
  16. struct aio_thread_req {
  17. enum aio_type type;
  18. int io_fd;
  19. unsigned long long offset;
  20. char *buf;
  21. int len;
  22. struct aio_context *aio;
  23. };
  24. #if defined(HAVE_AIO_ABI)
  25. #include <linux/aio_abi.h>
  26. /*
  27. * If we have the headers, we are going to build with AIO enabled.
  28. * If we don't have aio in libc, we define the necessary stubs here.
  29. */
  30. #if !defined(HAVE_AIO_LIBC)
  31. static long io_setup(int n, aio_context_t *ctxp)
  32. {
  33. return syscall(__NR_io_setup, n, ctxp);
  34. }
  35. static long io_submit(aio_context_t ctx, long nr, struct iocb **iocbpp)
  36. {
  37. return syscall(__NR_io_submit, ctx, nr, iocbpp);
  38. }
  39. static long io_getevents(aio_context_t ctx_id, long min_nr, long nr,
  40. struct io_event *events, struct timespec *timeout)
  41. {
  42. return syscall(__NR_io_getevents, ctx_id, min_nr, nr, events, timeout);
  43. }
  44. #endif
  45. /*
  46. * The AIO_MMAP cases force the mmapped page into memory here
  47. * rather than in whatever place first touches the data. I used
  48. * to do this by touching the page, but that's delicate because
  49. * gcc is prone to optimizing that away. So, what's done here
  50. * is we read from the descriptor from which the page was
  51. * mapped. The caller is required to pass an offset which is
  52. * inside the page that was mapped. Thus, when the read
  53. * returns, we know that the page is in the page cache, and
  54. * that it now backs the mmapped area.
  55. */
  56. static int do_aio(aio_context_t ctx, enum aio_type type, int fd, char *buf,
  57. int len, unsigned long long offset, struct aio_context *aio)
  58. {
  59. struct iocb *iocbp = & ((struct iocb) {
  60. .aio_data = (unsigned long) aio,
  61. .aio_fildes = fd,
  62. .aio_buf = (unsigned long) buf,
  63. .aio_nbytes = len,
  64. .aio_offset = offset
  65. });
  66. char c;
  67. switch (type) {
  68. case AIO_READ:
  69. iocbp->aio_lio_opcode = IOCB_CMD_PREAD;
  70. break;
  71. case AIO_WRITE:
  72. iocbp->aio_lio_opcode = IOCB_CMD_PWRITE;
  73. break;
  74. case AIO_MMAP:
  75. iocbp->aio_lio_opcode = IOCB_CMD_PREAD;
  76. iocbp->aio_buf = (unsigned long) &c;
  77. iocbp->aio_nbytes = sizeof(c);
  78. break;
  79. default:
  80. printk(UM_KERN_ERR "Bogus op in do_aio - %d\n", type);
  81. return -EINVAL;
  82. }
  83. return (io_submit(ctx, 1, &iocbp) > 0) ? 0 : -errno;
  84. }
  85. /* Initialized in an initcall and unchanged thereafter */
  86. static aio_context_t ctx = 0;
  87. static int aio_thread(void *arg)
  88. {
  89. struct aio_thread_reply reply;
  90. struct io_event event;
  91. int err, n, reply_fd;
  92. signal(SIGWINCH, SIG_IGN);
  93. while (1) {
  94. n = io_getevents(ctx, 1, 1, &event, NULL);
  95. if (n < 0) {
  96. if (errno == EINTR)
  97. continue;
  98. printk(UM_KERN_ERR "aio_thread - io_getevents failed, "
  99. "errno = %d\n", errno);
  100. }
  101. else {
  102. reply = ((struct aio_thread_reply)
  103. { .data = (void *) (long) event.data,
  104. .err = event.res });
  105. reply_fd = ((struct aio_context *) reply.data)->reply_fd;
  106. err = write(reply_fd, &reply, sizeof(reply));
  107. if (err != sizeof(reply))
  108. printk(UM_KERN_ERR "aio_thread - write failed, "
  109. "fd = %d, err = %d\n", reply_fd, errno);
  110. }
  111. }
  112. return 0;
  113. }
  114. #endif
  115. static int do_not_aio(struct aio_thread_req *req)
  116. {
  117. char c;
  118. unsigned long long actual;
  119. int n;
  120. actual = lseek64(req->io_fd, req->offset, SEEK_SET);
  121. if (actual != req->offset)
  122. return -errno;
  123. switch(req->type) {
  124. case AIO_READ:
  125. n = read(req->io_fd, req->buf, req->len);
  126. break;
  127. case AIO_WRITE:
  128. n = write(req->io_fd, req->buf, req->len);
  129. break;
  130. case AIO_MMAP:
  131. n = read(req->io_fd, &c, sizeof(c));
  132. break;
  133. default:
  134. printk(UM_KERN_ERR "do_not_aio - bad request type : %d\n",
  135. req->type);
  136. return -EINVAL;
  137. }
  138. if (n < 0)
  139. return -errno;
  140. return 0;
  141. }
  142. /* These are initialized in initcalls and not changed */
  143. static int aio_req_fd_r = -1;
  144. static int aio_req_fd_w = -1;
  145. static int aio_pid = -1;
  146. static unsigned long aio_stack;
  147. static int not_aio_thread(void *arg)
  148. {
  149. struct aio_thread_req req;
  150. struct aio_thread_reply reply;
  151. int err;
  152. signal(SIGWINCH, SIG_IGN);
  153. while (1) {
  154. err = read(aio_req_fd_r, &req, sizeof(req));
  155. if (err != sizeof(req)) {
  156. if (err < 0)
  157. printk(UM_KERN_ERR "not_aio_thread - "
  158. "read failed, fd = %d, err = %d\n",
  159. aio_req_fd_r,
  160. errno);
  161. else {
  162. printk(UM_KERN_ERR "not_aio_thread - short "
  163. "read, fd = %d, length = %d\n",
  164. aio_req_fd_r, err);
  165. }
  166. continue;
  167. }
  168. err = do_not_aio(&req);
  169. reply = ((struct aio_thread_reply) { .data = req.aio,
  170. .err = err });
  171. err = write(req.aio->reply_fd, &reply, sizeof(reply));
  172. if (err != sizeof(reply))
  173. printk(UM_KERN_ERR "not_aio_thread - write failed, "
  174. "fd = %d, err = %d\n", req.aio->reply_fd, errno);
  175. }
  176. return 0;
  177. }
  178. static int init_aio_24(void)
  179. {
  180. int fds[2], err;
  181. err = os_pipe(fds, 1, 1);
  182. if (err)
  183. goto out;
  184. aio_req_fd_w = fds[0];
  185. aio_req_fd_r = fds[1];
  186. err = os_set_fd_block(aio_req_fd_w, 0);
  187. if (err)
  188. goto out_close_pipe;
  189. err = run_helper_thread(not_aio_thread, NULL,
  190. CLONE_FILES | CLONE_VM | SIGCHLD, &aio_stack);
  191. if (err < 0)
  192. goto out_close_pipe;
  193. aio_pid = err;
  194. goto out;
  195. out_close_pipe:
  196. close(fds[0]);
  197. close(fds[1]);
  198. aio_req_fd_w = -1;
  199. aio_req_fd_r = -1;
  200. out:
  201. #ifndef HAVE_AIO_ABI
  202. printk(UM_KERN_INFO "/usr/include/linux/aio_abi.h not present during "
  203. "build\n");
  204. #endif
  205. printk(UM_KERN_INFO "2.6 host AIO support not used - falling back to "
  206. "I/O thread\n");
  207. return 0;
  208. }
  209. #ifdef HAVE_AIO_ABI
  210. #define DEFAULT_24_AIO 0
  211. static int init_aio_26(void)
  212. {
  213. int err;
  214. if (io_setup(256, &ctx)) {
  215. err = -errno;
  216. printk(UM_KERN_ERR "aio_thread failed to initialize context, "
  217. "err = %d\n", errno);
  218. return err;
  219. }
  220. err = run_helper_thread(aio_thread, NULL,
  221. CLONE_FILES | CLONE_VM | SIGCHLD, &aio_stack);
  222. if (err < 0)
  223. return err;
  224. aio_pid = err;
  225. printk(UM_KERN_INFO "Using 2.6 host AIO\n");
  226. return 0;
  227. }
  228. static int submit_aio_26(enum aio_type type, int io_fd, char *buf, int len,
  229. unsigned long long offset, struct aio_context *aio)
  230. {
  231. struct aio_thread_reply reply;
  232. int err;
  233. err = do_aio(ctx, type, io_fd, buf, len, offset, aio);
  234. if (err) {
  235. reply = ((struct aio_thread_reply) { .data = aio,
  236. .err = err });
  237. err = write(aio->reply_fd, &reply, sizeof(reply));
  238. if (err != sizeof(reply)) {
  239. err = -errno;
  240. printk(UM_KERN_ERR "submit_aio_26 - write failed, "
  241. "fd = %d, err = %d\n", aio->reply_fd, -err);
  242. }
  243. else err = 0;
  244. }
  245. return err;
  246. }
  247. #else
  248. #define DEFAULT_24_AIO 1
  249. static int init_aio_26(void)
  250. {
  251. return -ENOSYS;
  252. }
  253. static int submit_aio_26(enum aio_type type, int io_fd, char *buf, int len,
  254. unsigned long long offset, struct aio_context *aio)
  255. {
  256. return -ENOSYS;
  257. }
  258. #endif
  259. /* Initialized in an initcall and unchanged thereafter */
  260. static int aio_24 = DEFAULT_24_AIO;
  261. static int __init set_aio_24(char *name, int *add)
  262. {
  263. aio_24 = 1;
  264. return 0;
  265. }
  266. __uml_setup("aio=2.4", set_aio_24,
  267. "aio=2.4\n"
  268. " This is used to force UML to use 2.4-style AIO even when 2.6 AIO is\n"
  269. " available. 2.4 AIO is a single thread that handles one request at a\n"
  270. " time, synchronously. 2.6 AIO is a thread which uses the 2.6 AIO \n"
  271. " interface to handle an arbitrary number of pending requests. 2.6 AIO \n"
  272. " is not available in tt mode, on 2.4 hosts, or when UML is built with\n"
  273. " /usr/include/linux/aio_abi.h not available. Many distributions don't\n"
  274. " include aio_abi.h, so you will need to copy it from a kernel tree to\n"
  275. " your /usr/include/linux in order to build an AIO-capable UML\n\n"
  276. );
  277. static int init_aio(void)
  278. {
  279. int err;
  280. if (!aio_24) {
  281. err = init_aio_26();
  282. if (err && (errno == ENOSYS)) {
  283. printk(UM_KERN_INFO "2.6 AIO not supported on the "
  284. "host - reverting to 2.4 AIO\n");
  285. aio_24 = 1;
  286. }
  287. else return err;
  288. }
  289. if (aio_24)
  290. return init_aio_24();
  291. return 0;
  292. }
  293. /*
  294. * The reason for the __initcall/__uml_exitcall asymmetry is that init_aio
  295. * needs to be called when the kernel is running because it calls run_helper,
  296. * which needs get_free_page. exit_aio is a __uml_exitcall because the generic
  297. * kernel does not run __exitcalls on shutdown, and can't because many of them
  298. * break when called outside of module unloading.
  299. */
  300. __initcall(init_aio);
  301. static void exit_aio(void)
  302. {
  303. if (aio_pid != -1) {
  304. os_kill_process(aio_pid, 1);
  305. free_stack(aio_stack, 0);
  306. }
  307. }
  308. __uml_exitcall(exit_aio);
  309. static int submit_aio_24(enum aio_type type, int io_fd, char *buf, int len,
  310. unsigned long long offset, struct aio_context *aio)
  311. {
  312. struct aio_thread_req req = { .type = type,
  313. .io_fd = io_fd,
  314. .offset = offset,
  315. .buf = buf,
  316. .len = len,
  317. .aio = aio,
  318. };
  319. int err;
  320. err = write(aio_req_fd_w, &req, sizeof(req));
  321. if (err == sizeof(req))
  322. err = 0;
  323. else err = -errno;
  324. return err;
  325. }
  326. int submit_aio(enum aio_type type, int io_fd, char *buf, int len,
  327. unsigned long long offset, int reply_fd,
  328. struct aio_context *aio)
  329. {
  330. aio->reply_fd = reply_fd;
  331. if (aio_24)
  332. return submit_aio_24(type, io_fd, buf, len, offset, aio);
  333. else
  334. return submit_aio_26(type, io_fd, buf, len, offset, aio);
  335. }