aio.c 9.1 KB

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