aio.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 = os_write_file(reply_fd, &reply, sizeof(reply));
  117. if(err != sizeof(reply))
  118. printk("aio_thread - write failed, fd = %d, "
  119. "err = %d\n", reply_fd, -err);
  120. }
  121. }
  122. return 0;
  123. }
  124. #endif
  125. static int do_not_aio(struct aio_thread_req *req)
  126. {
  127. char c;
  128. int err;
  129. switch(req->type){
  130. case AIO_READ:
  131. err = os_seek_file(req->io_fd, req->offset);
  132. if(err)
  133. goto out;
  134. err = os_read_file(req->io_fd, req->buf, req->len);
  135. break;
  136. case AIO_WRITE:
  137. err = os_seek_file(req->io_fd, req->offset);
  138. if(err)
  139. goto out;
  140. err = os_write_file(req->io_fd, req->buf, req->len);
  141. break;
  142. case AIO_MMAP:
  143. err = os_seek_file(req->io_fd, req->offset);
  144. if(err)
  145. goto out;
  146. err = os_read_file(req->io_fd, &c, sizeof(c));
  147. break;
  148. default:
  149. printk("do_not_aio - bad request type : %d\n", req->type);
  150. err = -EINVAL;
  151. break;
  152. }
  153. out:
  154. return err;
  155. }
  156. /* These are initialized in initcalls and not changed */
  157. static int aio_req_fd_r = -1;
  158. static int aio_req_fd_w = -1;
  159. static int aio_pid = -1;
  160. static int not_aio_thread(void *arg)
  161. {
  162. struct aio_thread_req req;
  163. struct aio_thread_reply reply;
  164. int err;
  165. signal(SIGWINCH, SIG_IGN);
  166. while(1){
  167. err = os_read_file(aio_req_fd_r, &req, sizeof(req));
  168. if(err != sizeof(req)){
  169. if(err < 0)
  170. printk("not_aio_thread - read failed, "
  171. "fd = %d, err = %d\n", aio_req_fd_r,
  172. -err);
  173. else {
  174. printk("not_aio_thread - short read, fd = %d, "
  175. "length = %d\n", aio_req_fd_r, err);
  176. }
  177. continue;
  178. }
  179. err = do_not_aio(&req);
  180. reply = ((struct aio_thread_reply) { .data = req.aio,
  181. .err = err });
  182. err = os_write_file(req.aio->reply_fd, &reply, sizeof(reply));
  183. if(err != sizeof(reply))
  184. printk("not_aio_thread - write failed, fd = %d, "
  185. "err = %d\n", req.aio->reply_fd, -err);
  186. }
  187. return 0;
  188. }
  189. static int init_aio_24(void)
  190. {
  191. unsigned long stack;
  192. int fds[2], err;
  193. err = os_pipe(fds, 1, 1);
  194. if(err)
  195. goto out;
  196. aio_req_fd_w = fds[0];
  197. aio_req_fd_r = fds[1];
  198. err = run_helper_thread(not_aio_thread, NULL,
  199. CLONE_FILES | CLONE_VM | SIGCHLD, &stack, 0);
  200. if(err < 0)
  201. goto out_close_pipe;
  202. aio_pid = err;
  203. goto out;
  204. out_close_pipe:
  205. os_close_file(fds[0]);
  206. os_close_file(fds[1]);
  207. aio_req_fd_w = -1;
  208. aio_req_fd_r = -1;
  209. out:
  210. #ifndef HAVE_AIO_ABI
  211. printk("/usr/include/linux/aio_abi.h not present during build\n");
  212. #endif
  213. printk("2.6 host AIO support not used - falling back to I/O "
  214. "thread\n");
  215. return 0;
  216. }
  217. #ifdef HAVE_AIO_ABI
  218. #define DEFAULT_24_AIO 0
  219. static int init_aio_26(void)
  220. {
  221. unsigned long stack;
  222. int err;
  223. if(io_setup(256, &ctx)){
  224. err = -errno;
  225. printk("aio_thread failed to initialize context, err = %d\n",
  226. errno);
  227. return err;
  228. }
  229. err = run_helper_thread(aio_thread, NULL,
  230. CLONE_FILES | CLONE_VM | SIGCHLD, &stack, 0);
  231. if(err < 0)
  232. return err;
  233. aio_pid = err;
  234. printk("Using 2.6 host AIO\n");
  235. return 0;
  236. }
  237. static int submit_aio_26(enum aio_type type, int io_fd, char *buf, int len,
  238. unsigned long long offset, struct aio_context *aio)
  239. {
  240. struct aio_thread_reply reply;
  241. int err;
  242. err = do_aio(ctx, type, io_fd, buf, len, offset, aio);
  243. if(err){
  244. reply = ((struct aio_thread_reply) { .data = aio,
  245. .err = err });
  246. err = os_write_file(aio->reply_fd, &reply, sizeof(reply));
  247. if(err != sizeof(reply))
  248. printk("submit_aio_26 - write failed, "
  249. "fd = %d, err = %d\n", aio->reply_fd, -err);
  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 = os_write_file(aio_req_fd_w, &req, sizeof(req));
  329. if(err == sizeof(req))
  330. err = 0;
  331. return err;
  332. }
  333. int submit_aio(enum aio_type type, int io_fd, char *buf, int len,
  334. unsigned long long offset, int reply_fd,
  335. struct aio_context *aio)
  336. {
  337. aio->reply_fd = reply_fd;
  338. if(aio_24)
  339. return submit_aio_24(type, io_fd, buf, len, offset, aio);
  340. else {
  341. return submit_aio_26(type, io_fd, buf, len, offset, aio);
  342. }
  343. }