async.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. /*
  2. * Server-side async I/O support
  3. *
  4. * Copyright (C) 2007 Alexandre Julliard
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  19. */
  20. #include <assert.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <stdarg.h>
  24. #include "ntstatus.h"
  25. #define WIN32_NO_STATUS
  26. #include "windef.h"
  27. #include "winternl.h"
  28. #include "object.h"
  29. #include "file.h"
  30. #include "request.h"
  31. #include "process.h"
  32. #include "handle.h"
  33. struct async
  34. {
  35. struct object obj; /* object header */
  36. struct thread *thread; /* owning thread */
  37. struct list queue_entry; /* entry in async queue list */
  38. struct list process_entry; /* entry in process list */
  39. struct async_queue *queue; /* queue containing this async */
  40. struct fd *fd; /* fd associated with an unqueued async */
  41. unsigned int status; /* current status */
  42. struct timeout_user *timeout;
  43. unsigned int timeout_status; /* status to report upon timeout */
  44. struct event *event;
  45. async_data_t data; /* data for async I/O call */
  46. struct iosb *iosb; /* I/O status block */
  47. obj_handle_t wait_handle; /* pre-allocated wait handle */
  48. unsigned int signaled :1;
  49. unsigned int pending :1; /* request successfully queued, but pending */
  50. unsigned int direct_result :1;/* a flag if we're passing result directly from request instead of APC */
  51. struct completion *completion; /* completion associated with fd */
  52. apc_param_t comp_key; /* completion key associated with fd */
  53. unsigned int comp_flags; /* completion flags */
  54. };
  55. static void async_dump( struct object *obj, int verbose );
  56. static int async_signaled( struct object *obj, struct wait_queue_entry *entry );
  57. static void async_satisfied( struct object * obj, struct wait_queue_entry *entry );
  58. static void async_destroy( struct object *obj );
  59. static const struct object_ops async_ops =
  60. {
  61. sizeof(struct async), /* size */
  62. async_dump, /* dump */
  63. no_get_type, /* get_type */
  64. add_queue, /* add_queue */
  65. remove_queue, /* remove_queue */
  66. async_signaled, /* signaled */
  67. NULL, /* get_esync_fd */
  68. async_satisfied, /* satisfied */
  69. no_signal, /* signal */
  70. no_get_fd, /* get_fd */
  71. no_map_access, /* map_access */
  72. default_get_sd, /* get_sd */
  73. default_set_sd, /* set_sd */
  74. no_lookup_name, /* lookup_name */
  75. no_link_name, /* link_name */
  76. NULL, /* unlink_name */
  77. no_open_file, /* open_file */
  78. no_kernel_obj_list, /* get_kernel_obj_list */
  79. no_alloc_handle, /* alloc_handle */
  80. no_close_handle, /* close_handle */
  81. async_destroy /* destroy */
  82. };
  83. static inline void async_reselect( struct async *async )
  84. {
  85. if (async->queue && async->fd) fd_reselect_async( async->fd, async->queue );
  86. }
  87. static void async_dump( struct object *obj, int verbose )
  88. {
  89. struct async *async = (struct async *)obj;
  90. assert( obj->ops == &async_ops );
  91. fprintf( stderr, "Async thread=%p\n", async->thread );
  92. }
  93. static int async_signaled( struct object *obj, struct wait_queue_entry *entry )
  94. {
  95. struct async *async = (struct async *)obj;
  96. assert( obj->ops == &async_ops );
  97. return async->signaled;
  98. }
  99. static void async_satisfied( struct object *obj, struct wait_queue_entry *entry )
  100. {
  101. struct async *async = (struct async *)obj;
  102. assert( obj->ops == &async_ops );
  103. if (async->direct_result)
  104. {
  105. async_set_result( &async->obj, async->iosb->status, async->iosb->result );
  106. async->direct_result = 0;
  107. }
  108. /* close wait handle here to avoid extra server round trip */
  109. if (async->wait_handle)
  110. {
  111. close_handle( async->thread->process, async->wait_handle );
  112. async->wait_handle = 0;
  113. }
  114. if (async->status == STATUS_PENDING) make_wait_abandoned( entry );
  115. }
  116. static void async_destroy( struct object *obj )
  117. {
  118. struct async *async = (struct async *)obj;
  119. assert( obj->ops == &async_ops );
  120. list_remove( &async->process_entry );
  121. if (async->queue)
  122. {
  123. list_remove( &async->queue_entry );
  124. async_reselect( async );
  125. }
  126. else if (async->fd) release_object( async->fd );
  127. if (async->timeout) remove_timeout_user( async->timeout );
  128. if (async->completion) release_object( async->completion );
  129. if (async->event) release_object( async->event );
  130. if (async->iosb) release_object( async->iosb );
  131. release_object( async->thread );
  132. }
  133. /* notifies client thread of new status of its async request */
  134. void async_terminate( struct async *async, unsigned int status )
  135. {
  136. assert( status != STATUS_PENDING );
  137. if (async->status != STATUS_PENDING)
  138. {
  139. /* already terminated, just update status */
  140. async->status = status;
  141. return;
  142. }
  143. async->status = status;
  144. if (async->iosb && async->iosb->status == STATUS_PENDING) async->iosb->status = status;
  145. if (!async->direct_result)
  146. {
  147. if (async->data.user)
  148. {
  149. apc_call_t data;
  150. memset( &data, 0, sizeof(data) );
  151. data.type = APC_ASYNC_IO;
  152. data.async_io.user = async->data.user;
  153. data.async_io.sb = async->data.iosb;
  154. data.async_io.status = status;
  155. thread_queue_apc( async->thread->process, async->thread, &async->obj, &data );
  156. }
  157. else async_set_result( &async->obj, STATUS_SUCCESS, 0 );
  158. }
  159. async_reselect( async );
  160. if (async->queue) release_object( async ); /* so that it gets destroyed when the async is done */
  161. }
  162. /* callback for timeout on an async request */
  163. static void async_timeout( void *private )
  164. {
  165. struct async *async = private;
  166. async->timeout = NULL;
  167. async_terminate( async, async->timeout_status );
  168. }
  169. /* free an async queue, cancelling all async operations */
  170. void free_async_queue( struct async_queue *queue )
  171. {
  172. struct async *async, *next;
  173. LIST_FOR_EACH_ENTRY_SAFE( async, next, &queue->queue, struct async, queue_entry )
  174. {
  175. grab_object( &async->obj );
  176. if (!async->completion) async->completion = fd_get_completion( async->fd, &async->comp_key );
  177. async->fd = NULL;
  178. async_terminate( async, STATUS_HANDLES_CLOSED );
  179. async->queue = NULL;
  180. release_object( &async->obj );
  181. }
  182. }
  183. void queue_async( struct async_queue *queue, struct async *async )
  184. {
  185. /* fd will be set to NULL in free_async_queue when fd is destroyed */
  186. release_object( async->fd );
  187. async->queue = queue;
  188. grab_object( async );
  189. list_add_tail( &queue->queue, &async->queue_entry );
  190. set_fd_signaled( async->fd, 0 );
  191. }
  192. /* create an async on a given queue of a fd */
  193. struct async *create_async( struct fd *fd, struct thread *thread, const async_data_t *data, struct iosb *iosb )
  194. {
  195. struct event *event = NULL;
  196. struct async *async;
  197. if (data->event && !(event = get_event_obj( thread->process, data->event, EVENT_MODIFY_STATE )))
  198. return NULL;
  199. if (!(async = alloc_object( &async_ops )))
  200. {
  201. if (event) release_object( event );
  202. return NULL;
  203. }
  204. async->thread = (struct thread *)grab_object( thread );
  205. async->event = event;
  206. async->status = STATUS_PENDING;
  207. async->data = *data;
  208. async->timeout = NULL;
  209. async->queue = NULL;
  210. async->fd = (struct fd *)grab_object( fd );
  211. async->signaled = 0;
  212. async->pending = 1;
  213. async->wait_handle = 0;
  214. async->direct_result = 0;
  215. async->completion = fd_get_completion( fd, &async->comp_key );
  216. async->comp_flags = 0;
  217. if (iosb) async->iosb = (struct iosb *)grab_object( iosb );
  218. else async->iosb = NULL;
  219. list_add_head( &thread->process->asyncs, &async->process_entry );
  220. if (event) reset_event( event );
  221. if (async->completion && data->apc)
  222. {
  223. release_object( async );
  224. set_error( STATUS_INVALID_PARAMETER );
  225. return NULL;
  226. }
  227. return async;
  228. }
  229. void set_async_pending( struct async *async, int signal )
  230. {
  231. if (async->status == STATUS_PENDING)
  232. {
  233. async->pending = 1;
  234. if (signal && !async->signaled)
  235. {
  236. async->signaled = 1;
  237. wake_up( &async->obj, 0 );
  238. }
  239. }
  240. }
  241. /* create an async associated with iosb for async-based requests
  242. * returned async must be passed to async_handoff */
  243. struct async *create_request_async( struct fd *fd, unsigned int comp_flags, const async_data_t *data )
  244. {
  245. struct async *async;
  246. struct iosb *iosb;
  247. if (!(iosb = create_iosb( get_req_data(), get_req_data_size(), get_reply_max_size() )))
  248. return NULL;
  249. async = create_async( fd, current, data, iosb );
  250. release_object( iosb );
  251. if (async)
  252. {
  253. if (!(async->wait_handle = alloc_handle( current->process, async, SYNCHRONIZE, 0 )))
  254. {
  255. release_object( async );
  256. return NULL;
  257. }
  258. async->pending = 0;
  259. async->direct_result = 1;
  260. async->comp_flags = comp_flags;
  261. }
  262. return async;
  263. }
  264. /* return async object status and wait handle to client */
  265. obj_handle_t async_handoff( struct async *async, int success, data_size_t *result, int force_blocking )
  266. {
  267. if (!success)
  268. {
  269. if (get_error() == STATUS_PENDING)
  270. {
  271. /* we don't know the result yet, so client needs to wait */
  272. async->direct_result = 0;
  273. return async->wait_handle;
  274. }
  275. close_handle( async->thread->process, async->wait_handle );
  276. async->wait_handle = 0;
  277. return 0;
  278. }
  279. if (get_error() != STATUS_PENDING)
  280. {
  281. /* status and data are already set and returned */
  282. async_terminate( async, get_error() );
  283. }
  284. else if (async->iosb->status != STATUS_PENDING)
  285. {
  286. /* result is already available in iosb, return it */
  287. if (async->iosb->out_data)
  288. {
  289. set_reply_data_ptr( async->iosb->out_data, async->iosb->out_size );
  290. async->iosb->out_data = NULL;
  291. }
  292. }
  293. if (async->iosb->status != STATUS_PENDING)
  294. {
  295. if (result) *result = async->iosb->result;
  296. async->signaled = 1;
  297. }
  298. else
  299. {
  300. async->direct_result = 0;
  301. async->pending = 1;
  302. if (!force_blocking && async->fd && is_fd_overlapped( async->fd ))
  303. {
  304. close_handle( async->thread->process, async->wait_handle);
  305. async->wait_handle = 0;
  306. }
  307. }
  308. set_error( async->iosb->status );
  309. return async->wait_handle;
  310. }
  311. /* set the timeout of an async operation */
  312. void async_set_timeout( struct async *async, timeout_t timeout, unsigned int status )
  313. {
  314. if (async->timeout) remove_timeout_user( async->timeout );
  315. if (timeout != TIMEOUT_INFINITE) async->timeout = add_timeout_user( timeout, async_timeout, async );
  316. else async->timeout = NULL;
  317. async->timeout_status = status;
  318. }
  319. static void add_async_completion( struct async *async, apc_param_t cvalue, unsigned int status,
  320. apc_param_t information )
  321. {
  322. if (async->fd && !async->completion) async->completion = fd_get_completion( async->fd, &async->comp_key );
  323. if (async->completion) add_completion( async->completion, async->comp_key, cvalue, status, information );
  324. }
  325. /* store the result of the client-side async callback */
  326. void async_set_result( struct object *obj, unsigned int status, apc_param_t total )
  327. {
  328. struct async *async = (struct async *)obj;
  329. if (obj->ops != &async_ops) return; /* in case the client messed up the APC results */
  330. assert( async->status != STATUS_PENDING ); /* it must have been woken up if we get a result */
  331. if (status == STATUS_PENDING) /* restart it */
  332. {
  333. status = async->status;
  334. async->status = STATUS_PENDING;
  335. grab_object( async );
  336. if (status != STATUS_ALERTED) /* it was terminated in the meantime */
  337. async_terminate( async, status );
  338. else
  339. async_reselect( async );
  340. }
  341. else
  342. {
  343. if (async->timeout) remove_timeout_user( async->timeout );
  344. async->timeout = NULL;
  345. async->status = status;
  346. if (status == STATUS_MORE_PROCESSING_REQUIRED) return; /* don't report the completion */
  347. if (async->data.apc)
  348. {
  349. apc_call_t data;
  350. memset( &data, 0, sizeof(data) );
  351. data.type = APC_USER;
  352. data.user.func = async->data.apc;
  353. data.user.args[0] = async->data.apc_context;
  354. data.user.args[1] = async->data.iosb;
  355. data.user.args[2] = 0;
  356. thread_queue_apc( NULL, async->thread, NULL, &data );
  357. }
  358. else if (async->data.apc_context && (async->pending ||
  359. !(async->comp_flags & FILE_SKIP_COMPLETION_PORT_ON_SUCCESS)))
  360. {
  361. add_async_completion( async, async->data.apc_context, status, total );
  362. }
  363. if (async->event) set_event( async->event );
  364. else if (async->fd) set_fd_signaled( async->fd, 1 );
  365. if (!async->signaled)
  366. {
  367. async->signaled = 1;
  368. wake_up( &async->obj, 0 );
  369. }
  370. }
  371. }
  372. /* check if an async operation is waiting to be alerted */
  373. int async_waiting( struct async_queue *queue )
  374. {
  375. struct list *ptr;
  376. struct async *async;
  377. if (!(ptr = list_head( &queue->queue ))) return 0;
  378. async = LIST_ENTRY( ptr, struct async, queue_entry );
  379. return async->status == STATUS_PENDING;
  380. }
  381. static int cancel_async( struct process *process, struct object *obj, struct thread *thread, client_ptr_t iosb )
  382. {
  383. struct async *async;
  384. int woken = 0;
  385. restart:
  386. LIST_FOR_EACH_ENTRY( async, &process->asyncs, struct async, process_entry )
  387. {
  388. if (async->status == STATUS_CANCELLED) continue;
  389. if ((!obj || (async->fd && get_fd_user( async->fd ) == obj)) &&
  390. (!thread || async->thread == thread) &&
  391. (!iosb || async->data.iosb == iosb))
  392. {
  393. async_terminate( async, STATUS_CANCELLED );
  394. woken++;
  395. goto restart;
  396. }
  397. }
  398. return woken;
  399. }
  400. void cancel_process_asyncs( struct process *process )
  401. {
  402. cancel_async( process, NULL, NULL, 0 );
  403. }
  404. /* wake up async operations on the queue */
  405. void async_wake_up( struct async_queue *queue, unsigned int status )
  406. {
  407. struct list *ptr, *next;
  408. LIST_FOR_EACH_SAFE( ptr, next, &queue->queue )
  409. {
  410. struct async *async = LIST_ENTRY( ptr, struct async, queue_entry );
  411. async_terminate( async, status );
  412. if (status == STATUS_ALERTED) break; /* only wake up the first one */
  413. }
  414. }
  415. static void iosb_dump( struct object *obj, int verbose );
  416. static void iosb_destroy( struct object *obj );
  417. static const struct object_ops iosb_ops =
  418. {
  419. sizeof(struct iosb), /* size */
  420. iosb_dump, /* dump */
  421. no_get_type, /* get_type */
  422. no_add_queue, /* add_queue */
  423. NULL, /* remove_queue */
  424. NULL, /* signaled */
  425. NULL, /* get_esync_fd */
  426. NULL, /* satisfied */
  427. no_signal, /* signal */
  428. no_get_fd, /* get_fd */
  429. no_map_access, /* map_access */
  430. default_get_sd, /* get_sd */
  431. default_set_sd, /* set_sd */
  432. no_lookup_name, /* lookup_name */
  433. no_link_name, /* link_name */
  434. NULL, /* unlink_name */
  435. no_open_file, /* open_file */
  436. no_kernel_obj_list, /* get_kernel_obj_list */
  437. no_alloc_handle, /* alloc_handle */
  438. no_close_handle, /* close_handle */
  439. iosb_destroy /* destroy */
  440. };
  441. static void iosb_dump( struct object *obj, int verbose )
  442. {
  443. assert( obj->ops == &iosb_ops );
  444. fprintf( stderr, "I/O status block\n" );
  445. }
  446. static void iosb_destroy( struct object *obj )
  447. {
  448. struct iosb *iosb = (struct iosb *)obj;
  449. free( iosb->in_data );
  450. free( iosb->out_data );
  451. }
  452. /* allocate iosb struct */
  453. struct iosb *create_iosb( const void *in_data, data_size_t in_size, data_size_t out_size )
  454. {
  455. struct iosb *iosb;
  456. if (!(iosb = alloc_object( &iosb_ops ))) return NULL;
  457. iosb->status = STATUS_PENDING;
  458. iosb->result = 0;
  459. iosb->in_size = in_size;
  460. iosb->in_data = NULL;
  461. iosb->out_size = out_size;
  462. iosb->out_data = NULL;
  463. if (in_size && !(iosb->in_data = memdup( in_data, in_size )))
  464. {
  465. release_object( iosb );
  466. iosb = NULL;
  467. }
  468. return iosb;
  469. }
  470. struct iosb *async_get_iosb( struct async *async )
  471. {
  472. return async->iosb ? (struct iosb *)grab_object( async->iosb ) : NULL;
  473. }
  474. int async_is_blocking( struct async *async )
  475. {
  476. return !async->event && !async->data.apc && !async->data.apc_context;
  477. }
  478. /* find the first pending async in queue */
  479. struct async *find_pending_async( struct async_queue *queue )
  480. {
  481. struct async *async;
  482. LIST_FOR_EACH_ENTRY( async, &queue->queue, struct async, queue_entry )
  483. if (async->status == STATUS_PENDING) return (struct async *)grab_object( async );
  484. return NULL;
  485. }
  486. /* cancels all async I/O */
  487. DECL_HANDLER(cancel_async)
  488. {
  489. struct object *obj = get_handle_obj( current->process, req->handle, 0, NULL );
  490. struct thread *thread = req->only_thread ? current : NULL;
  491. if (obj)
  492. {
  493. int count = cancel_async( current->process, obj, thread, req->iosb );
  494. if (!count && req->iosb) set_error( STATUS_NOT_FOUND );
  495. release_object( obj );
  496. }
  497. }
  498. /* get async result from associated iosb */
  499. DECL_HANDLER(get_async_result)
  500. {
  501. struct iosb *iosb = NULL;
  502. struct async *async;
  503. LIST_FOR_EACH_ENTRY( async, &current->process->asyncs, struct async, process_entry )
  504. if (async->data.user == req->user_arg)
  505. {
  506. iosb = async->iosb;
  507. break;
  508. }
  509. if (!iosb)
  510. {
  511. set_error( STATUS_INVALID_PARAMETER );
  512. return;
  513. }
  514. if (iosb->out_data)
  515. {
  516. data_size_t size = min( iosb->out_size, get_reply_max_size() );
  517. if (size)
  518. {
  519. set_reply_data_ptr( iosb->out_data, size );
  520. iosb->out_data = NULL;
  521. }
  522. }
  523. reply->size = iosb->result;
  524. set_error( iosb->status );
  525. }