aio.c 9.0 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 "aio.h"
  13. #include "init.h"
  14. #include "user.h"
  15. #include "mode.h"
  16. struct aio_thread_req {
  17. enum aio_type type;
  18. int io_fd;
  19. unsigned long long offset;
  20. char *buf;
  21. int len;
  22. struct aio_context *aio;
  23. };
  24. static int aio_req_fd_r = -1;
  25. static int aio_req_fd_w = -1;
  26. #if defined(HAVE_AIO_ABI)
  27. #include <linux/aio_abi.h>
  28. /* If we have the headers, we are going to build with AIO enabled.
  29. * If we don't have aio in libc, we define the necessary stubs here.
  30. */
  31. #if !defined(HAVE_AIO_LIBC)
  32. static long io_setup(int n, aio_context_t *ctxp)
  33. {
  34. return syscall(__NR_io_setup, n, ctxp);
  35. }
  36. static long io_submit(aio_context_t ctx, long nr, struct iocb **iocbpp)
  37. {
  38. return syscall(__NR_io_submit, ctx, nr, iocbpp);
  39. }
  40. static long io_getevents(aio_context_t ctx_id, long min_nr, long nr,
  41. struct io_event *events, struct timespec *timeout)
  42. {
  43. return syscall(__NR_io_getevents, ctx_id, min_nr, nr, events, timeout);
  44. }
  45. #endif
  46. /* The AIO_MMAP cases force the mmapped page into memory here
  47. * rather than in whatever place first touches the data. I used
  48. * to do this by touching the page, but that's delicate because
  49. * gcc is prone to optimizing that away. So, what's done here
  50. * is we read from the descriptor from which the page was
  51. * mapped. The caller is required to pass an offset which is
  52. * inside the page that was mapped. Thus, when the read
  53. * returns, we know that the page is in the page cache, and
  54. * that it now backs the mmapped area.
  55. */
  56. static int do_aio(aio_context_t ctx, enum aio_type type, int fd, char *buf,
  57. int len, unsigned long long offset, struct aio_context *aio)
  58. {
  59. struct iocb iocb, *iocbp = &iocb;
  60. char c;
  61. int err;
  62. iocb = ((struct iocb) { .aio_data = (unsigned long) aio,
  63. .aio_reqprio = 0,
  64. .aio_fildes = fd,
  65. .aio_buf = (unsigned long) buf,
  66. .aio_nbytes = len,
  67. .aio_offset = offset,
  68. .aio_reserved1 = 0,
  69. .aio_reserved2 = 0,
  70. .aio_reserved3 = 0 });
  71. switch(type){
  72. case AIO_READ:
  73. iocb.aio_lio_opcode = IOCB_CMD_PREAD;
  74. err = io_submit(ctx, 1, &iocbp);
  75. break;
  76. case AIO_WRITE:
  77. iocb.aio_lio_opcode = IOCB_CMD_PWRITE;
  78. err = io_submit(ctx, 1, &iocbp);
  79. break;
  80. case AIO_MMAP:
  81. iocb.aio_lio_opcode = IOCB_CMD_PREAD;
  82. iocb.aio_buf = (unsigned long) &c;
  83. iocb.aio_nbytes = sizeof(c);
  84. err = io_submit(ctx, 1, &iocbp);
  85. break;
  86. default:
  87. printk("Bogus op in do_aio - %d\n", type);
  88. err = -EINVAL;
  89. break;
  90. }
  91. if(err > 0)
  92. err = 0;
  93. else
  94. err = -errno;
  95. return err;
  96. }
  97. static aio_context_t ctx = 0;
  98. static int aio_thread(void *arg)
  99. {
  100. struct aio_thread_reply reply;
  101. struct io_event event;
  102. int err, n, reply_fd;
  103. signal(SIGWINCH, SIG_IGN);
  104. while(1){
  105. n = io_getevents(ctx, 1, 1, &event, NULL);
  106. if(n < 0){
  107. if(errno == EINTR)
  108. continue;
  109. printk("aio_thread - io_getevents failed, "
  110. "errno = %d\n", errno);
  111. }
  112. else {
  113. reply = ((struct aio_thread_reply)
  114. { .data = (void *) (long) event.data,
  115. .err = event.res });
  116. reply_fd = ((struct aio_context *) reply.data)->reply_fd;
  117. err = os_write_file(reply_fd, &reply, sizeof(reply));
  118. if(err != sizeof(reply))
  119. printk("aio_thread - write failed, fd = %d, "
  120. "err = %d\n", aio_req_fd_r, -err);
  121. }
  122. }
  123. return 0;
  124. }
  125. #endif
  126. static int do_not_aio(struct aio_thread_req *req)
  127. {
  128. char c;
  129. int err;
  130. switch(req->type){
  131. case AIO_READ:
  132. err = os_seek_file(req->io_fd, req->offset);
  133. if(err)
  134. goto out;
  135. err = os_read_file(req->io_fd, req->buf, req->len);
  136. break;
  137. case AIO_WRITE:
  138. err = os_seek_file(req->io_fd, req->offset);
  139. if(err)
  140. goto out;
  141. err = os_write_file(req->io_fd, req->buf, req->len);
  142. break;
  143. case AIO_MMAP:
  144. err = os_seek_file(req->io_fd, req->offset);
  145. if(err)
  146. goto out;
  147. err = os_read_file(req->io_fd, &c, sizeof(c));
  148. break;
  149. default:
  150. printk("do_not_aio - bad request type : %d\n", req->type);
  151. err = -EINVAL;
  152. break;
  153. }
  154. out:
  155. return err;
  156. }
  157. static int not_aio_thread(void *arg)
  158. {
  159. struct aio_thread_req req;
  160. struct aio_thread_reply reply;
  161. int err;
  162. signal(SIGWINCH, SIG_IGN);
  163. while(1){
  164. err = os_read_file(aio_req_fd_r, &req, sizeof(req));
  165. if(err != sizeof(req)){
  166. if(err < 0)
  167. printk("not_aio_thread - read failed, "
  168. "fd = %d, err = %d\n", aio_req_fd_r,
  169. -err);
  170. else {
  171. printk("not_aio_thread - short read, fd = %d, "
  172. "length = %d\n", aio_req_fd_r, err);
  173. }
  174. continue;
  175. }
  176. err = do_not_aio(&req);
  177. reply = ((struct aio_thread_reply) { .data = req.aio,
  178. .err = err });
  179. err = os_write_file(req.aio->reply_fd, &reply, sizeof(reply));
  180. if(err != sizeof(reply))
  181. printk("not_aio_thread - write failed, fd = %d, "
  182. "err = %d\n", aio_req_fd_r, -err);
  183. }
  184. return 0;
  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(({ if(!aio_24){
  285. printk("Disabling 2.6 AIO in tt mode\n");
  286. aio_24 = 1;
  287. } }), (void) 0);
  288. if(!aio_24){
  289. err = init_aio_26();
  290. if(err && (errno == ENOSYS)){
  291. printk("2.6 AIO not supported on the host - "
  292. "reverting to 2.4 AIO\n");
  293. aio_24 = 1;
  294. }
  295. else return err;
  296. }
  297. if(aio_24)
  298. return init_aio_24();
  299. return 0;
  300. }
  301. /* The reason for the __initcall/__uml_exitcall asymmetry is that init_aio
  302. * needs to be called when the kernel is running because it calls run_helper,
  303. * which needs get_free_page. exit_aio is a __uml_exitcall because the generic
  304. * kernel does not run __exitcalls on shutdown, and can't because many of them
  305. * break when called outside of module unloading.
  306. */
  307. __initcall(init_aio);
  308. static void exit_aio(void)
  309. {
  310. if(aio_pid != -1)
  311. os_kill_process(aio_pid, 1);
  312. }
  313. __uml_exitcall(exit_aio);
  314. static int submit_aio_24(enum aio_type type, int io_fd, char *buf, int len,
  315. unsigned long long offset, struct aio_context *aio)
  316. {
  317. struct aio_thread_req req = { .type = type,
  318. .io_fd = io_fd,
  319. .offset = offset,
  320. .buf = buf,
  321. .len = len,
  322. .aio = aio,
  323. };
  324. int err;
  325. err = os_write_file(aio_req_fd_w, &req, sizeof(req));
  326. if(err == sizeof(req))
  327. err = 0;
  328. return err;
  329. }
  330. int submit_aio(enum aio_type type, int io_fd, char *buf, int len,
  331. unsigned long long offset, int reply_fd,
  332. struct aio_context *aio)
  333. {
  334. aio->reply_fd = reply_fd;
  335. if(aio_24)
  336. return submit_aio_24(type, io_fd, buf, len, offset, aio);
  337. else {
  338. return submit_aio_26(type, io_fd, buf, len, offset, aio);
  339. }
  340. }