aio.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * Copyright (C) 2004 Jeff Dike (jdike@addtoit.com)
  3. * Licensed under the GPL
  4. */
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <signal.h>
  8. #include <errno.h>
  9. #include <sched.h>
  10. #include <sys/syscall.h>
  11. #include "os.h"
  12. #include "aio.h"
  13. #include "init.h"
  14. #include "user.h"
  15. #include "mode.h"
  16. #include "kern_constants.h"
  17. struct aio_thread_req {
  18. enum aio_type type;
  19. int io_fd;
  20. unsigned long long offset;
  21. char *buf;
  22. int len;
  23. struct aio_context *aio;
  24. };
  25. #if defined(HAVE_AIO_ABI)
  26. #include <linux/aio_abi.h>
  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. /* 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. signal(SIGWINCH, SIG_IGN);
  92. while(1){
  93. n = io_getevents(ctx, 1, 1, &event, NULL);
  94. if(n < 0){
  95. if(errno == EINTR)
  96. continue;
  97. printk("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("aio_thread - write failed, fd = %d, "
  108. "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("do_not_aio - bad request type : %d\n", req->type);
  134. return -EINVAL;
  135. }
  136. if(n < 0)
  137. return -errno;
  138. return 0;
  139. }
  140. /* These are initialized in initcalls and not changed */
  141. static int aio_req_fd_r = -1;
  142. static int aio_req_fd_w = -1;
  143. static int aio_pid = -1;
  144. static unsigned long aio_stack;
  145. static int not_aio_thread(void *arg)
  146. {
  147. struct aio_thread_req req;
  148. struct aio_thread_reply reply;
  149. int err;
  150. signal(SIGWINCH, SIG_IGN);
  151. while(1){
  152. err = read(aio_req_fd_r, &req, sizeof(req));
  153. if(err != sizeof(req)){
  154. if(err < 0)
  155. printk("not_aio_thread - read failed, "
  156. "fd = %d, err = %d\n", aio_req_fd_r,
  157. errno);
  158. else {
  159. printk("not_aio_thread - short read, fd = %d, "
  160. "length = %d\n", aio_req_fd_r, err);
  161. }
  162. continue;
  163. }
  164. err = do_not_aio(&req);
  165. reply = ((struct aio_thread_reply) { .data = req.aio,
  166. .err = err });
  167. err = write(req.aio->reply_fd, &reply, sizeof(reply));
  168. if(err != sizeof(reply))
  169. printk("not_aio_thread - write failed, fd = %d, "
  170. "err = %d\n", req.aio->reply_fd, errno);
  171. }
  172. return 0;
  173. }
  174. static int init_aio_24(void)
  175. {
  176. int fds[2], err;
  177. err = os_pipe(fds, 1, 1);
  178. if(err)
  179. goto out;
  180. aio_req_fd_w = fds[0];
  181. aio_req_fd_r = fds[1];
  182. err = os_set_fd_block(aio_req_fd_w, 0);
  183. if(err)
  184. goto out_close_pipe;
  185. err = run_helper_thread(not_aio_thread, NULL,
  186. CLONE_FILES | CLONE_VM | SIGCHLD, &aio_stack);
  187. if(err < 0)
  188. goto out_close_pipe;
  189. aio_pid = err;
  190. goto out;
  191. out_close_pipe:
  192. os_close_file(fds[0]);
  193. os_close_file(fds[1]);
  194. aio_req_fd_w = -1;
  195. aio_req_fd_r = -1;
  196. out:
  197. #ifndef HAVE_AIO_ABI
  198. printk("/usr/include/linux/aio_abi.h not present during build\n");
  199. #endif
  200. printk("2.6 host AIO support not used - falling back to I/O "
  201. "thread\n");
  202. return 0;
  203. }
  204. #ifdef HAVE_AIO_ABI
  205. #define DEFAULT_24_AIO 0
  206. static int init_aio_26(void)
  207. {
  208. int err;
  209. if(io_setup(256, &ctx)){
  210. err = -errno;
  211. printk("aio_thread failed to initialize context, err = %d\n",
  212. errno);
  213. return err;
  214. }
  215. err = run_helper_thread(aio_thread, NULL,
  216. CLONE_FILES | CLONE_VM | SIGCHLD, &aio_stack);
  217. if(err < 0)
  218. return err;
  219. aio_pid = err;
  220. printk("Using 2.6 host AIO\n");
  221. return 0;
  222. }
  223. static int submit_aio_26(enum aio_type type, int io_fd, char *buf, int len,
  224. unsigned long long offset, struct aio_context *aio)
  225. {
  226. struct aio_thread_reply reply;
  227. int err;
  228. err = do_aio(ctx, type, io_fd, buf, len, offset, aio);
  229. if(err){
  230. reply = ((struct aio_thread_reply) { .data = aio,
  231. .err = err });
  232. err = write(aio->reply_fd, &reply, sizeof(reply));
  233. if(err != sizeof(reply)){
  234. err = -errno;
  235. printk("submit_aio_26 - write failed, "
  236. "fd = %d, err = %d\n", aio->reply_fd, -err);
  237. }
  238. else err = 0;
  239. }
  240. return err;
  241. }
  242. #else
  243. #define DEFAULT_24_AIO 1
  244. static int init_aio_26(void)
  245. {
  246. return -ENOSYS;
  247. }
  248. static int submit_aio_26(enum aio_type type, int io_fd, char *buf, int len,
  249. unsigned long long offset, struct aio_context *aio)
  250. {
  251. return -ENOSYS;
  252. }
  253. #endif
  254. /* Initialized in an initcall and unchanged thereafter */
  255. static int aio_24 = DEFAULT_24_AIO;
  256. static int __init set_aio_24(char *name, int *add)
  257. {
  258. aio_24 = 1;
  259. return 0;
  260. }
  261. __uml_setup("aio=2.4", set_aio_24,
  262. "aio=2.4\n"
  263. " This is used to force UML to use 2.4-style AIO even when 2.6 AIO is\n"
  264. " available. 2.4 AIO is a single thread that handles one request at a\n"
  265. " time, synchronously. 2.6 AIO is a thread which uses the 2.6 AIO \n"
  266. " interface to handle an arbitrary number of pending requests. 2.6 AIO \n"
  267. " is not available in tt mode, on 2.4 hosts, or when UML is built with\n"
  268. " /usr/include/linux/aio_abi.h not available. Many distributions don't\n"
  269. " include aio_abi.h, so you will need to copy it from a kernel tree to\n"
  270. " your /usr/include/linux in order to build an AIO-capable UML\n\n"
  271. );
  272. static int init_aio(void)
  273. {
  274. int err;
  275. CHOOSE_MODE(({ if(!aio_24){
  276. printk("Disabling 2.6 AIO in tt mode\n");
  277. aio_24 = 1;
  278. } }), (void) 0);
  279. if(!aio_24){
  280. err = init_aio_26();
  281. if(err && (errno == ENOSYS)){
  282. printk("2.6 AIO not supported on the host - "
  283. "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. /* The reason for the __initcall/__uml_exitcall asymmetry is that init_aio
  293. * needs to be called when the kernel is running because it calls run_helper,
  294. * which needs get_free_page. exit_aio is a __uml_exitcall because the generic
  295. * kernel does not run __exitcalls on shutdown, and can't because many of them
  296. * break when called outside of module unloading.
  297. */
  298. __initcall(init_aio);
  299. static void exit_aio(void)
  300. {
  301. if (aio_pid != -1) {
  302. os_kill_process(aio_pid, 1);
  303. free_stack(aio_stack, 0);
  304. }
  305. }
  306. __uml_exitcall(exit_aio);
  307. static int submit_aio_24(enum aio_type type, int io_fd, char *buf, int len,
  308. unsigned long long offset, struct aio_context *aio)
  309. {
  310. struct aio_thread_req req = { .type = type,
  311. .io_fd = io_fd,
  312. .offset = offset,
  313. .buf = buf,
  314. .len = len,
  315. .aio = aio,
  316. };
  317. int err;
  318. err = write(aio_req_fd_w, &req, sizeof(req));
  319. if(err == sizeof(req))
  320. err = 0;
  321. else err = -errno;
  322. return err;
  323. }
  324. int submit_aio(enum aio_type type, int io_fd, char *buf, int len,
  325. unsigned long long offset, int reply_fd,
  326. struct aio_context *aio)
  327. {
  328. aio->reply_fd = reply_fd;
  329. if(aio_24)
  330. return submit_aio_24(type, io_fd, buf, len, offset, aio);
  331. else {
  332. return submit_aio_26(type, io_fd, buf, len, offset, aio);
  333. }
  334. }