netapi: add NetShareDel example code.
[Samba/gbeck.git] / source3 / smbd / aio.c
blob74275368bdd2c2f119ca022a58f17f5ee72a81f5
1 /*
2 Unix SMB/Netbios implementation.
3 Version 3.0
4 async_io read handling using POSIX async io.
5 Copyright (C) Jeremy Allison 2005.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
23 #if defined(WITH_AIO)
25 /* The signal we'll use to signify aio done. */
26 #ifndef RT_SIGNAL_AIO
27 #define RT_SIGNAL_AIO (SIGRTMIN+3)
28 #endif
30 /****************************************************************************
31 The buffer we keep around whilst an aio request is in process.
32 *****************************************************************************/
34 struct aio_extra {
35 struct aio_extra *next, *prev;
36 SMB_STRUCT_AIOCB acb;
37 files_struct *fsp;
38 bool read_req;
39 uint16 mid;
40 char *inbuf;
41 char *outbuf;
44 static struct aio_extra *aio_list_head;
46 /****************************************************************************
47 Create the extended aio struct we must keep around for the lifetime
48 of the aio_read call.
49 *****************************************************************************/
51 static struct aio_extra *create_aio_ex_read(files_struct *fsp, size_t buflen,
52 uint16 mid)
54 struct aio_extra *aio_ex = SMB_MALLOC_P(struct aio_extra);
56 if (!aio_ex) {
57 return NULL;
59 ZERO_STRUCTP(aio_ex);
60 /* The output buffer stored in the aio_ex is the start of
61 the smb return buffer. The buffer used in the acb
62 is the start of the reply data portion of that buffer. */
63 aio_ex->outbuf = SMB_MALLOC_ARRAY(char, buflen);
64 if (!aio_ex->outbuf) {
65 SAFE_FREE(aio_ex);
66 return NULL;
68 DLIST_ADD(aio_list_head, aio_ex);
69 aio_ex->fsp = fsp;
70 aio_ex->read_req = True;
71 aio_ex->mid = mid;
72 return aio_ex;
75 /****************************************************************************
76 Create the extended aio struct we must keep around for the lifetime
77 of the aio_write call.
78 *****************************************************************************/
80 static struct aio_extra *create_aio_ex_write(files_struct *fsp,
81 size_t inbuflen,
82 size_t outbuflen,
83 uint16 mid)
85 struct aio_extra *aio_ex = SMB_MALLOC_P(struct aio_extra);
87 if (!aio_ex) {
88 return NULL;
90 ZERO_STRUCTP(aio_ex);
92 /* We need space for an output reply of outbuflen bytes. */
93 aio_ex->outbuf = SMB_MALLOC_ARRAY(char, outbuflen);
94 if (!aio_ex->outbuf) {
95 SAFE_FREE(aio_ex);
96 return NULL;
99 if (!(aio_ex->inbuf = SMB_MALLOC_ARRAY(char, inbuflen))) {
100 SAFE_FREE(aio_ex->outbuf);
101 SAFE_FREE(aio_ex);
102 return NULL;
105 DLIST_ADD(aio_list_head, aio_ex);
106 aio_ex->fsp = fsp;
107 aio_ex->read_req = False;
108 aio_ex->mid = mid;
109 return aio_ex;
112 /****************************************************************************
113 Delete the extended aio struct.
114 *****************************************************************************/
116 static void delete_aio_ex(struct aio_extra *aio_ex)
118 DLIST_REMOVE(aio_list_head, aio_ex);
119 SAFE_FREE(aio_ex->inbuf);
120 SAFE_FREE(aio_ex->outbuf);
121 SAFE_FREE(aio_ex);
124 /****************************************************************************
125 Given the aiocb struct find the extended aio struct containing it.
126 *****************************************************************************/
128 static struct aio_extra *find_aio_ex(uint16 mid)
130 struct aio_extra *p;
132 for( p = aio_list_head; p; p = p->next) {
133 if (mid == p->mid) {
134 return p;
137 return NULL;
140 /****************************************************************************
141 We can have these many aio buffers in flight.
142 *****************************************************************************/
144 static int aio_pending_size;
145 static sig_atomic_t signals_received;
146 static int outstanding_aio_calls;
147 static uint16 *aio_pending_array;
149 /****************************************************************************
150 Signal handler when an aio request completes.
151 *****************************************************************************/
153 void aio_request_done(uint16_t mid)
155 if (signals_received < aio_pending_size) {
156 aio_pending_array[signals_received] = mid;
157 signals_received++;
159 /* Else signal is lost. */
162 static void signal_handler(int sig, siginfo_t *info, void *unused)
164 aio_request_done(info->si_value.sival_int);
165 sys_select_signal(RT_SIGNAL_AIO);
168 /****************************************************************************
169 Is there a signal waiting ?
170 *****************************************************************************/
172 bool aio_finished(void)
174 return (signals_received != 0);
177 /****************************************************************************
178 Initialize the signal handler for aio read/write.
179 *****************************************************************************/
181 void initialize_async_io_handler(void)
183 struct sigaction act;
185 aio_pending_size = lp_maxmux();
186 aio_pending_array = SMB_MALLOC_ARRAY(uint16, aio_pending_size);
187 SMB_ASSERT(aio_pending_array != NULL);
189 ZERO_STRUCT(act);
190 act.sa_sigaction = signal_handler;
191 act.sa_flags = SA_SIGINFO;
192 sigemptyset( &act.sa_mask );
193 if (sigaction(RT_SIGNAL_AIO, &act, NULL) != 0) {
194 DEBUG(0,("Failed to setup RT_SIGNAL_AIO handler\n"));
197 /* the signal can start off blocked due to a bug in bash */
198 BlockSignals(False, RT_SIGNAL_AIO);
201 /****************************************************************************
202 Set up an aio request from a SMBreadX call.
203 *****************************************************************************/
205 bool schedule_aio_read_and_X(connection_struct *conn,
206 struct smb_request *req,
207 files_struct *fsp, SMB_OFF_T startpos,
208 size_t smb_maxcnt)
210 struct aio_extra *aio_ex;
211 SMB_STRUCT_AIOCB *a;
212 size_t bufsize;
213 size_t min_aio_read_size = lp_aio_read_size(SNUM(conn));
215 if (fsp->base_fsp != NULL) {
216 /* No AIO on streams yet */
217 DEBUG(10, ("AIO on streams not yet supported\n"));
218 return false;
221 if ((!min_aio_read_size || (smb_maxcnt < min_aio_read_size))
222 && !SMB_VFS_AIO_FORCE(fsp)) {
223 /* Too small a read for aio request. */
224 DEBUG(10,("schedule_aio_read_and_X: read size (%u) too small "
225 "for minimum aio_read of %u\n",
226 (unsigned int)smb_maxcnt,
227 (unsigned int)min_aio_read_size ));
228 return False;
231 /* Only do this on non-chained and non-chaining reads not using the
232 * write cache. */
233 if (chain_size !=0 || (CVAL(req->inbuf,smb_vwv0) != 0xFF)
234 || (lp_write_cache_size(SNUM(conn)) != 0) ) {
235 return False;
238 if (outstanding_aio_calls >= aio_pending_size) {
239 DEBUG(10,("schedule_aio_read_and_X: Already have %d aio "
240 "activities outstanding.\n",
241 outstanding_aio_calls ));
242 return False;
245 /* The following is safe from integer wrap as we've already checked
246 smb_maxcnt is 128k or less. Wct is 12 for read replies */
248 bufsize = smb_size + 12 * 2 + smb_maxcnt;
250 if ((aio_ex = create_aio_ex_read(fsp, bufsize, req->mid)) == NULL) {
251 DEBUG(10,("schedule_aio_read_and_X: malloc fail.\n"));
252 return False;
255 construct_reply_common((char *)req->inbuf, aio_ex->outbuf);
256 srv_set_message(aio_ex->outbuf, 12, 0, True);
257 SCVAL(aio_ex->outbuf,smb_vwv0,0xFF); /* Never a chained reply. */
259 a = &aio_ex->acb;
261 /* Now set up the aio record for the read call. */
263 a->aio_fildes = fsp->fh->fd;
264 a->aio_buf = smb_buf(aio_ex->outbuf);
265 a->aio_nbytes = smb_maxcnt;
266 a->aio_offset = startpos;
267 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
268 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
269 a->aio_sigevent.sigev_value.sival_int = aio_ex->mid;
271 become_root();
272 if (SMB_VFS_AIO_READ(fsp,a) == -1) {
273 DEBUG(0,("schedule_aio_read_and_X: aio_read failed. "
274 "Error %s\n", strerror(errno) ));
275 delete_aio_ex(aio_ex);
276 unbecome_root();
277 return False;
279 unbecome_root();
281 DEBUG(10,("schedule_aio_read_and_X: scheduled aio_read for file %s, "
282 "offset %.0f, len = %u (mid = %u)\n",
283 fsp->fsp_name, (double)startpos, (unsigned int)smb_maxcnt,
284 (unsigned int)aio_ex->mid ));
286 srv_defer_sign_response(aio_ex->mid);
287 outstanding_aio_calls++;
288 return True;
291 /****************************************************************************
292 Set up an aio request from a SMBwriteX call.
293 *****************************************************************************/
295 bool schedule_aio_write_and_X(connection_struct *conn,
296 struct smb_request *req,
297 files_struct *fsp, char *data,
298 SMB_OFF_T startpos,
299 size_t numtowrite)
301 struct aio_extra *aio_ex;
302 SMB_STRUCT_AIOCB *a;
303 size_t inbufsize, outbufsize;
304 bool write_through = BITSETW(req->inbuf+smb_vwv7,0);
305 size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
307 if (fsp->base_fsp != NULL) {
308 /* No AIO on streams yet */
309 DEBUG(10, ("AIO on streams not yet supported\n"));
310 return false;
313 if ((!min_aio_write_size || (numtowrite < min_aio_write_size))
314 && !SMB_VFS_AIO_FORCE(fsp)) {
315 /* Too small a write for aio request. */
316 DEBUG(10,("schedule_aio_write_and_X: write size (%u) too "
317 "small for minimum aio_write of %u\n",
318 (unsigned int)numtowrite,
319 (unsigned int)min_aio_write_size ));
320 return False;
323 /* Only do this on non-chained and non-chaining reads not using the
324 * write cache. */
325 if (chain_size !=0 || (CVAL(req->inbuf,smb_vwv0) != 0xFF)
326 || (lp_write_cache_size(SNUM(conn)) != 0) ) {
327 return False;
330 if (outstanding_aio_calls >= aio_pending_size) {
331 DEBUG(3,("schedule_aio_write_and_X: Already have %d aio "
332 "activities outstanding.\n",
333 outstanding_aio_calls ));
334 DEBUG(10,("schedule_aio_write_and_X: failed to schedule "
335 "aio_write for file %s, offset %.0f, len = %u "
336 "(mid = %u)\n",
337 fsp->fsp_name, (double)startpos,
338 (unsigned int)numtowrite,
339 (unsigned int)req->mid ));
340 return False;
343 inbufsize = smb_len(req->inbuf) + 4;
344 reply_outbuf(req, 6, 0);
345 outbufsize = smb_len(req->outbuf) + 4;
346 if (!(aio_ex = create_aio_ex_write(fsp, inbufsize, outbufsize,
347 req->mid))) {
348 DEBUG(0,("schedule_aio_write_and_X: malloc fail.\n"));
349 return False;
352 /* Copy the SMB header already setup in outbuf. */
353 memcpy(aio_ex->inbuf, req->inbuf, inbufsize);
355 /* Copy the SMB header already setup in outbuf. */
356 memcpy(aio_ex->outbuf, req->outbuf, outbufsize);
357 TALLOC_FREE(req->outbuf);
358 SCVAL(aio_ex->outbuf,smb_vwv0,0xFF); /* Never a chained reply. */
360 a = &aio_ex->acb;
362 /* Now set up the aio record for the write call. */
364 a->aio_fildes = fsp->fh->fd;
365 a->aio_buf = aio_ex->inbuf + (PTR_DIFF(data, req->inbuf));
366 a->aio_nbytes = numtowrite;
367 a->aio_offset = startpos;
368 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
369 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
370 a->aio_sigevent.sigev_value.sival_int = aio_ex->mid;
372 become_root();
373 if (SMB_VFS_AIO_WRITE(fsp,a) == -1) {
374 DEBUG(3,("schedule_aio_wrote_and_X: aio_write failed. "
375 "Error %s\n", strerror(errno) ));
376 delete_aio_ex(aio_ex);
377 unbecome_root();
378 return False;
380 unbecome_root();
382 release_level_2_oplocks_on_change(fsp);
384 if (!write_through && !lp_syncalways(SNUM(fsp->conn))
385 && fsp->aio_write_behind) {
386 /* Lie to the client and immediately claim we finished the
387 * write. */
388 SSVAL(aio_ex->outbuf,smb_vwv2,numtowrite);
389 SSVAL(aio_ex->outbuf,smb_vwv4,(numtowrite>>16)&1);
390 show_msg(aio_ex->outbuf);
391 if (!srv_send_smb(smbd_server_fd(),aio_ex->outbuf,
392 IS_CONN_ENCRYPTED(fsp->conn))) {
393 exit_server_cleanly("handle_aio_write: srv_send_smb "
394 "failed.");
396 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write "
397 "behind for file %s\n", fsp->fsp_name ));
398 } else {
399 srv_defer_sign_response(aio_ex->mid);
401 outstanding_aio_calls++;
403 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write for file "
404 "%s, offset %.0f, len = %u (mid = %u) "
405 "outstanding_aio_calls = %d\n",
406 fsp->fsp_name, (double)startpos, (unsigned int)numtowrite,
407 (unsigned int)aio_ex->mid, outstanding_aio_calls ));
409 return True;
413 /****************************************************************************
414 Complete the read and return the data or error back to the client.
415 Returns errno or zero if all ok.
416 *****************************************************************************/
418 static int handle_aio_read_complete(struct aio_extra *aio_ex)
420 int ret = 0;
421 int outsize;
422 char *outbuf = aio_ex->outbuf;
423 char *data = smb_buf(outbuf);
424 ssize_t nread = SMB_VFS_AIO_RETURN(aio_ex->fsp,&aio_ex->acb);
426 if (nread < 0) {
427 /* We're relying here on the fact that if the fd is
428 closed then the aio will complete and aio_return
429 will return an error. Hopefully this is
430 true.... JRA. */
432 /* If errno is ECANCELED then don't return anything to the
433 * client. */
434 if (errno == ECANCELED) {
435 srv_cancel_sign_response(aio_ex->mid);
436 return 0;
439 DEBUG( 3,( "handle_aio_read_complete: file %s nread == -1. "
440 "Error = %s\n",
441 aio_ex->fsp->fsp_name, strerror(errno) ));
443 ret = errno;
444 ERROR_NT(map_nt_error_from_unix(ret));
445 outsize = srv_set_message(outbuf,0,0,true);
446 } else {
447 outsize = srv_set_message(outbuf,12,nread,False);
448 SSVAL(outbuf,smb_vwv2,0xFFFF); /* Remaining - must be * -1. */
449 SSVAL(outbuf,smb_vwv5,nread);
450 SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
451 SSVAL(outbuf,smb_vwv7,((nread >> 16) & 1));
452 SSVAL(smb_buf(outbuf),-2,nread);
454 aio_ex->fsp->fh->pos = aio_ex->acb.aio_offset + nread;
455 aio_ex->fsp->fh->position_information = aio_ex->fsp->fh->pos;
457 DEBUG( 3, ( "handle_aio_read_complete file %s max=%d "
458 "nread=%d\n",
459 aio_ex->fsp->fsp_name,
460 (int)aio_ex->acb.aio_nbytes, (int)nread ) );
463 smb_setlen(outbuf,outsize - 4);
464 show_msg(outbuf);
465 if (!srv_send_smb(smbd_server_fd(),outbuf,
466 IS_CONN_ENCRYPTED(aio_ex->fsp->conn))) {
467 exit_server_cleanly("handle_aio_read_complete: srv_send_smb "
468 "failed.");
471 DEBUG(10,("handle_aio_read_complete: scheduled aio_read completed "
472 "for file %s, offset %.0f, len = %u\n",
473 aio_ex->fsp->fsp_name, (double)aio_ex->acb.aio_offset,
474 (unsigned int)nread ));
476 return ret;
479 /****************************************************************************
480 Complete the write and return the data or error back to the client.
481 Returns errno or zero if all ok.
482 *****************************************************************************/
484 static int handle_aio_write_complete(struct aio_extra *aio_ex)
486 int ret = 0;
487 files_struct *fsp = aio_ex->fsp;
488 char *outbuf = aio_ex->outbuf;
489 ssize_t numtowrite = aio_ex->acb.aio_nbytes;
490 ssize_t nwritten = SMB_VFS_AIO_RETURN(fsp,&aio_ex->acb);
492 if (fsp->aio_write_behind) {
493 if (nwritten != numtowrite) {
494 if (nwritten == -1) {
495 DEBUG(5,("handle_aio_write_complete: "
496 "aio_write_behind failed ! File %s "
497 "is corrupt ! Error %s\n",
498 fsp->fsp_name, strerror(errno) ));
499 ret = errno;
500 } else {
501 DEBUG(0,("handle_aio_write_complete: "
502 "aio_write_behind failed ! File %s "
503 "is corrupt ! Wanted %u bytes but "
504 "only wrote %d\n", fsp->fsp_name,
505 (unsigned int)numtowrite,
506 (int)nwritten ));
507 ret = EIO;
509 } else {
510 DEBUG(10,("handle_aio_write_complete: "
511 "aio_write_behind completed for file %s\n",
512 fsp->fsp_name ));
514 return 0;
517 /* We don't need outsize or set_message here as we've already set the
518 fixed size length when we set up the aio call. */
520 if(nwritten == -1) {
521 DEBUG( 3,( "handle_aio_write: file %s wanted %u bytes. "
522 "nwritten == %d. Error = %s\n",
523 fsp->fsp_name, (unsigned int)numtowrite,
524 (int)nwritten, strerror(errno) ));
526 /* If errno is ECANCELED then don't return anything to the
527 * client. */
528 if (errno == ECANCELED) {
529 srv_cancel_sign_response(aio_ex->mid);
530 return 0;
533 ret = errno;
534 ERROR_BOTH(map_nt_error_from_unix(ret), ERRHRD, ERRdiskfull);
535 srv_set_message(outbuf,0,0,true);
536 } else {
537 bool write_through = BITSETW(aio_ex->inbuf+smb_vwv7,0);
538 NTSTATUS status;
540 SSVAL(outbuf,smb_vwv2,nwritten);
541 SSVAL(outbuf,smb_vwv4,(nwritten>>16)&1);
542 if (nwritten < (ssize_t)numtowrite) {
543 SCVAL(outbuf,smb_rcls,ERRHRD);
544 SSVAL(outbuf,smb_err,ERRdiskfull);
547 DEBUG(3,("handle_aio_write: fnum=%d num=%d wrote=%d\n",
548 fsp->fnum, (int)numtowrite, (int)nwritten));
549 status = sync_file(fsp->conn,fsp, write_through);
550 if (!NT_STATUS_IS_OK(status)) {
551 ret = errno;
552 ERROR_BOTH(map_nt_error_from_unix(ret),
553 ERRHRD, ERRdiskfull);
554 srv_set_message(outbuf,0,0,true);
555 DEBUG(5,("handle_aio_write: sync_file for %s returned %s\n",
556 fsp->fsp_name, nt_errstr(status) ));
559 aio_ex->fsp->fh->pos = aio_ex->acb.aio_offset + nwritten;
562 show_msg(outbuf);
563 if (!srv_send_smb(smbd_server_fd(),outbuf,IS_CONN_ENCRYPTED(fsp->conn))) {
564 exit_server_cleanly("handle_aio_write: srv_send_smb failed.");
567 DEBUG(10,("handle_aio_write_complete: scheduled aio_write completed "
568 "for file %s, offset %.0f, requested %u, written = %u\n",
569 fsp->fsp_name, (double)aio_ex->acb.aio_offset,
570 (unsigned int)numtowrite, (unsigned int)nwritten ));
572 return ret;
575 /****************************************************************************
576 Handle any aio completion. Returns True if finished (and sets *perr if err
577 was non-zero), False if not.
578 *****************************************************************************/
580 static bool handle_aio_completed(struct aio_extra *aio_ex, int *perr)
582 int err;
584 /* Ensure the operation has really completed. */
585 if (SMB_VFS_AIO_ERROR(aio_ex->fsp, &aio_ex->acb) == EINPROGRESS) {
586 DEBUG(10,( "handle_aio_completed: operation mid %u still in "
587 "process for file %s\n",
588 aio_ex->mid, aio_ex->fsp->fsp_name ));
589 return False;
592 if (aio_ex->read_req) {
593 err = handle_aio_read_complete(aio_ex);
594 } else {
595 err = handle_aio_write_complete(aio_ex);
598 if (err) {
599 *perr = err; /* Only save non-zero errors. */
602 return True;
605 /****************************************************************************
606 Handle any aio completion inline.
607 Returns non-zero errno if fail or zero if all ok.
608 *****************************************************************************/
610 int process_aio_queue(void)
612 int i;
613 int ret = 0;
615 BlockSignals(True, RT_SIGNAL_AIO);
617 DEBUG(10,("process_aio_queue: signals_received = %d\n",
618 (int)signals_received));
619 DEBUG(10,("process_aio_queue: outstanding_aio_calls = %d\n",
620 outstanding_aio_calls));
622 if (!signals_received) {
623 BlockSignals(False, RT_SIGNAL_AIO);
624 return 0;
627 /* Drain all the complete aio_reads. */
628 for (i = 0; i < signals_received; i++) {
629 uint16 mid = aio_pending_array[i];
630 files_struct *fsp = NULL;
631 struct aio_extra *aio_ex = find_aio_ex(mid);
633 if (!aio_ex) {
634 DEBUG(3,("process_aio_queue: Can't find record to "
635 "match mid %u.\n", (unsigned int)mid));
636 srv_cancel_sign_response(mid);
637 continue;
640 fsp = aio_ex->fsp;
641 if (fsp == NULL) {
642 /* file was closed whilst I/O was outstanding. Just
643 * ignore. */
644 DEBUG( 3,( "process_aio_queue: file closed whilst "
645 "aio outstanding.\n"));
646 srv_cancel_sign_response(mid);
647 continue;
650 if (!handle_aio_completed(aio_ex, &ret)) {
651 continue;
654 delete_aio_ex(aio_ex);
657 outstanding_aio_calls -= signals_received;
658 signals_received = 0;
659 BlockSignals(False, RT_SIGNAL_AIO);
660 return ret;
663 /****************************************************************************
664 We're doing write behind and the client closed the file. Wait up to 30
665 seconds (my arbitrary choice) for the aio to complete. Return 0 if all writes
666 completed, errno to return if not.
667 *****************************************************************************/
669 #define SMB_TIME_FOR_AIO_COMPLETE_WAIT 29
671 int wait_for_aio_completion(files_struct *fsp)
673 struct aio_extra *aio_ex;
674 const SMB_STRUCT_AIOCB **aiocb_list;
675 int aio_completion_count = 0;
676 time_t start_time = time(NULL);
677 int seconds_left;
679 for (seconds_left = SMB_TIME_FOR_AIO_COMPLETE_WAIT;
680 seconds_left >= 0;) {
681 int err = 0;
682 int i;
683 struct timespec ts;
685 aio_completion_count = 0;
686 for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
687 if (aio_ex->fsp == fsp) {
688 aio_completion_count++;
692 if (!aio_completion_count) {
693 return 0;
696 DEBUG(3,("wait_for_aio_completion: waiting for %d aio events "
697 "to complete.\n", aio_completion_count ));
699 aiocb_list = SMB_MALLOC_ARRAY(const SMB_STRUCT_AIOCB *,
700 aio_completion_count);
701 if (!aiocb_list) {
702 return ENOMEM;
705 for( i = 0, aio_ex = aio_list_head;
706 aio_ex;
707 aio_ex = aio_ex->next) {
708 if (aio_ex->fsp == fsp) {
709 aiocb_list[i++] = &aio_ex->acb;
713 /* Now wait up to seconds_left for completion. */
714 ts.tv_sec = seconds_left;
715 ts.tv_nsec = 0;
717 DEBUG(10,("wait_for_aio_completion: %d events, doing a wait "
718 "of %d seconds.\n",
719 aio_completion_count, seconds_left ));
721 err = SMB_VFS_AIO_SUSPEND(fsp, aiocb_list,
722 aio_completion_count, &ts);
724 DEBUG(10,("wait_for_aio_completion: returned err = %d, "
725 "errno = %s\n", err, strerror(errno) ));
727 if (err == -1 && errno == EAGAIN) {
728 DEBUG(0,("wait_for_aio_completion: aio_suspend timed "
729 "out waiting for %d events after a wait of "
730 "%d seconds\n", aio_completion_count,
731 seconds_left));
732 /* Timeout. */
733 cancel_aio_by_fsp(fsp);
734 SAFE_FREE(aiocb_list);
735 return EIO;
738 /* One or more events might have completed - process them if
739 * so. */
740 for( i = 0; i < aio_completion_count; i++) {
741 uint16 mid = aiocb_list[i]->aio_sigevent.sigev_value.sival_int;
743 aio_ex = find_aio_ex(mid);
745 if (!aio_ex) {
746 DEBUG(0, ("wait_for_aio_completion: mid %u "
747 "doesn't match an aio record\n",
748 (unsigned int)mid ));
749 continue;
752 if (!handle_aio_completed(aio_ex, &err)) {
753 continue;
755 delete_aio_ex(aio_ex);
758 SAFE_FREE(aiocb_list);
759 seconds_left = SMB_TIME_FOR_AIO_COMPLETE_WAIT
760 - (time(NULL) - start_time);
763 /* We timed out - we don't know why. Return ret if already an error,
764 * else EIO. */
765 DEBUG(10,("wait_for_aio_completion: aio_suspend timed out waiting "
766 "for %d events\n",
767 aio_completion_count));
769 return EIO;
772 /****************************************************************************
773 Cancel any outstanding aio requests. The client doesn't care about the reply.
774 *****************************************************************************/
776 void cancel_aio_by_fsp(files_struct *fsp)
778 struct aio_extra *aio_ex;
780 for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
781 if (aio_ex->fsp == fsp) {
782 /* Don't delete the aio_extra record as we may have
783 completed and don't yet know it. Just do the
784 aio_cancel call and return. */
785 SMB_VFS_AIO_CANCEL(fsp, &aio_ex->acb);
786 aio_ex->fsp = NULL; /* fsp will be closed when we
787 * return. */
792 #else
793 bool aio_finished(void)
795 return False;
798 void initialize_async_io_handler(void)
802 int process_aio_queue(void)
804 return False;
807 bool schedule_aio_read_and_X(connection_struct *conn,
808 struct smb_request *req,
809 files_struct *fsp, SMB_OFF_T startpos,
810 size_t smb_maxcnt)
812 return False;
815 bool schedule_aio_write_and_X(connection_struct *conn,
816 struct smb_request *req,
817 files_struct *fsp, char *data,
818 SMB_OFF_T startpos,
819 size_t numtowrite)
821 return False;
824 void cancel_aio_by_fsp(files_struct *fsp)
828 int wait_for_aio_completion(files_struct *fsp)
830 return ENOSYS;
832 #endif