aio.c 12 KB

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