aio.c 9.1 KB

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