aio.c 11 KB

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