aio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. else
  105. err = -errno;
  106. out:
  107. return err;
  108. }
  109. static aio_context_t ctx = 0;
  110. static int aio_thread(void *arg)
  111. {
  112. struct aio_thread_reply reply;
  113. struct aio_context *aio;
  114. struct io_event event;
  115. int err, n;
  116. signal(SIGWINCH, SIG_IGN);
  117. while(1){
  118. n = io_getevents(ctx, 1, 1, &event, NULL);
  119. if(n < 0){
  120. if(errno == EINTR)
  121. continue;
  122. printk("aio_thread - io_getevents failed, "
  123. "errno = %d\n", errno);
  124. }
  125. else {
  126. aio = (struct aio_context *) (long) event.data;
  127. if(update_aio(aio, event.res)){
  128. do_aio(ctx, aio);
  129. continue;
  130. }
  131. reply = ((struct aio_thread_reply)
  132. { .data = aio,
  133. .err = aio->len });
  134. err = os_write_file(aio->reply_fd, &reply,
  135. sizeof(reply));
  136. if(err != sizeof(reply))
  137. printk("aio_thread - write failed, "
  138. "fd = %d, err = %d\n", aio->reply_fd,
  139. -err);
  140. }
  141. }
  142. return 0;
  143. }
  144. #endif
  145. static int do_not_aio(struct aio_context *aio)
  146. {
  147. char c;
  148. int err;
  149. switch(aio->type){
  150. case AIO_READ:
  151. err = os_seek_file(aio->fd, aio->offset);
  152. if(err)
  153. goto out;
  154. err = os_read_file(aio->fd, aio->data, aio->len);
  155. break;
  156. case AIO_WRITE:
  157. err = os_seek_file(aio->fd, aio->offset);
  158. if(err)
  159. goto out;
  160. err = os_write_file(aio->fd, aio->data, aio->len);
  161. break;
  162. case AIO_MMAP:
  163. err = os_seek_file(aio->fd, aio->offset);
  164. if(err)
  165. goto out;
  166. err = os_read_file(aio->fd, &c, sizeof(c));
  167. break;
  168. default:
  169. printk("do_not_aio - bad request type : %d\n", aio->type);
  170. err = -EINVAL;
  171. break;
  172. }
  173. out:
  174. return err;
  175. }
  176. static int not_aio_thread(void *arg)
  177. {
  178. struct aio_context *aio;
  179. struct aio_thread_reply reply;
  180. int err;
  181. signal(SIGWINCH, SIG_IGN);
  182. while(1){
  183. err = os_read_file(aio_req_fd_r, &aio, sizeof(aio));
  184. if(err != sizeof(aio)){
  185. if(err < 0)
  186. printk("not_aio_thread - read failed, "
  187. "fd = %d, err = %d\n", aio_req_fd_r,
  188. -err);
  189. else {
  190. printk("not_aio_thread - short read, fd = %d, "
  191. "length = %d\n", aio_req_fd_r, err);
  192. }
  193. continue;
  194. }
  195. again:
  196. err = do_not_aio(aio);
  197. if(update_aio(aio, err))
  198. goto again;
  199. reply = ((struct aio_thread_reply) { .data = aio,
  200. .err = aio->len });
  201. err = os_write_file(aio->reply_fd, &reply, sizeof(reply));
  202. if(err != sizeof(reply))
  203. printk("not_aio_thread - write failed, fd = %d, "
  204. "err = %d\n", aio_req_fd_r, -err);
  205. }
  206. }
  207. static int submit_aio_24(struct aio_context *aio)
  208. {
  209. int err;
  210. err = os_write_file(aio_req_fd_w, &aio, sizeof(aio));
  211. if(err == sizeof(aio))
  212. err = 0;
  213. return err;
  214. }
  215. static int aio_pid = -1;
  216. static int (*submit_proc)(struct aio_context *aio);
  217. static int init_aio_24(void)
  218. {
  219. unsigned long stack;
  220. int fds[2], err;
  221. err = os_pipe(fds, 1, 1);
  222. if(err)
  223. goto out;
  224. aio_req_fd_w = fds[0];
  225. aio_req_fd_r = fds[1];
  226. err = run_helper_thread(not_aio_thread, NULL,
  227. CLONE_FILES | CLONE_VM | SIGCHLD, &stack, 0);
  228. if(err < 0)
  229. goto out_close_pipe;
  230. aio_pid = err;
  231. goto out;
  232. out_close_pipe:
  233. os_close_file(fds[0]);
  234. os_close_file(fds[1]);
  235. aio_req_fd_w = -1;
  236. aio_req_fd_r = -1;
  237. out:
  238. #ifndef HAVE_AIO_ABI
  239. printk("/usr/include/linux/aio_abi.h not present during build\n");
  240. #endif
  241. printk("2.6 host AIO support not used - falling back to I/O "
  242. "thread\n");
  243. submit_proc = submit_aio_24;
  244. return 0;
  245. }
  246. #ifdef HAVE_AIO_ABI
  247. #define DEFAULT_24_AIO 0
  248. static int submit_aio_26(struct aio_context *aio)
  249. {
  250. struct aio_thread_reply reply;
  251. int err;
  252. err = do_aio(ctx, aio);
  253. if(err){
  254. reply = ((struct aio_thread_reply) { .data = aio,
  255. .err = err });
  256. err = os_write_file(aio->reply_fd, &reply, sizeof(reply));
  257. if(err != sizeof(reply))
  258. printk("submit_aio_26 - write failed, "
  259. "fd = %d, err = %d\n", aio->reply_fd, -err);
  260. else err = 0;
  261. }
  262. return err;
  263. }
  264. static int init_aio_26(void)
  265. {
  266. unsigned long stack;
  267. int err;
  268. if(io_setup(256, &ctx)){
  269. err = -errno;
  270. printk("aio_thread failed to initialize context, err = %d\n",
  271. errno);
  272. return err;
  273. }
  274. err = run_helper_thread(aio_thread, NULL,
  275. CLONE_FILES | CLONE_VM | SIGCHLD, &stack, 0);
  276. if(err < 0)
  277. return err;
  278. aio_pid = err;
  279. printk("Using 2.6 host AIO\n");
  280. submit_proc = submit_aio_26;
  281. return 0;
  282. }
  283. #else
  284. #define DEFAULT_24_AIO 1
  285. static int submit_aio_26(struct aio_context *aio)
  286. {
  287. return -ENOSYS;
  288. }
  289. static int init_aio_26(void)
  290. {
  291. submit_proc = submit_aio_26;
  292. return -ENOSYS;
  293. }
  294. #endif
  295. static int aio_24 = DEFAULT_24_AIO;
  296. static int __init set_aio_24(char *name, int *add)
  297. {
  298. aio_24 = 1;
  299. return 0;
  300. }
  301. __uml_setup("aio=2.4", set_aio_24,
  302. "aio=2.4\n"
  303. " This is used to force UML to use 2.4-style AIO even when 2.6 AIO is\n"
  304. " available. 2.4 AIO is a single thread that handles one request at a\n"
  305. " time, synchronously. 2.6 AIO is a thread which uses the 2.6 AIO \n"
  306. " interface to handle an arbitrary number of pending requests. 2.6 AIO \n"
  307. " is not available in tt mode, on 2.4 hosts, or when UML is built with\n"
  308. " /usr/include/linux/aio_abi.h not available. Many distributions don't\n"
  309. " include aio_abi.h, so you will need to copy it from a kernel tree to\n"
  310. " your /usr/include/linux in order to build an AIO-capable UML\n\n"
  311. );
  312. static int init_aio(void)
  313. {
  314. int err;
  315. CHOOSE_MODE(({
  316. if(!aio_24){
  317. printk("Disabling 2.6 AIO in tt mode\n");
  318. aio_24 = 1;
  319. } }), (void) 0);
  320. if(!aio_24){
  321. err = init_aio_26();
  322. if(err && (errno == ENOSYS)){
  323. printk("2.6 AIO not supported on the host - "
  324. "reverting to 2.4 AIO\n");
  325. aio_24 = 1;
  326. }
  327. else return err;
  328. }
  329. if(aio_24)
  330. return init_aio_24();
  331. return 0;
  332. }
  333. /* The reason for the __initcall/__uml_exitcall asymmetry is that init_aio
  334. * needs to be called when the kernel is running because it calls run_helper,
  335. * which needs get_free_page. exit_aio is a __uml_exitcall because the generic
  336. * kernel does not run __exitcalls on shutdown, and can't because many of them
  337. * break when called outside of module unloading.
  338. */
  339. __initcall(init_aio);
  340. static void exit_aio(void)
  341. {
  342. if(aio_pid != -1)
  343. os_kill_process(aio_pid, 1);
  344. }
  345. __uml_exitcall(exit_aio);
  346. int submit_aio(struct aio_context *aio)
  347. {
  348. return (*submit_proc)(aio);
  349. }