source: branches/samba-3.3.x/source/smbd/aio.c@ 790

Last change on this file since 790 was 370, checked in by Herwig Bauernfeind, 16 years ago

Update Samba 3.3 to 3.3.10 (source)

File size: 24.6 KB
Line 
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.
6
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.
11
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.
16
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/>.
19*/
20
21#include "includes.h"
22
23#if defined(WITH_AIO)
24
25/* The signal we'll use to signify aio done. */
26#ifndef RT_SIGNAL_AIO
27#ifndef SIGRTMIN
28#define SIGRTMIN NSIG
29#endif
30#define RT_SIGNAL_AIO (SIGRTMIN+3)
31#endif
32
33#ifndef HAVE_STRUCT_SIGEVENT_SIGEV_VALUE_SIVAL_PTR
34#ifdef HAVE_STRUCT_SIGEVENT_SIGEV_VALUE_SIGVAL_PTR
35#define sival_int sigval_int
36#define sival_ptr sigval_ptr
37#endif
38#endif
39
40/****************************************************************************
41 The buffer we keep around whilst an aio request is in process.
42*****************************************************************************/
43
44struct aio_extra {
45 struct aio_extra *next, *prev;
46 SMB_STRUCT_AIOCB acb;
47 files_struct *fsp;
48 bool read_req;
49 uint16 mid;
50 char *inbuf;
51 char *outbuf;
52};
53
54static struct aio_extra *aio_list_head;
55
56/****************************************************************************
57 Create the extended aio struct we must keep around for the lifetime
58 of the aio_read call.
59*****************************************************************************/
60
61static struct aio_extra *create_aio_ex_read(files_struct *fsp, size_t buflen,
62 uint16 mid)
63{
64 struct aio_extra *aio_ex = SMB_MALLOC_P(struct aio_extra);
65
66 if (!aio_ex) {
67 return NULL;
68 }
69 ZERO_STRUCTP(aio_ex);
70 /* The output buffer stored in the aio_ex is the start of
71 the smb return buffer. The buffer used in the acb
72 is the start of the reply data portion of that buffer. */
73 aio_ex->outbuf = SMB_MALLOC_ARRAY(char, buflen);
74 if (!aio_ex->outbuf) {
75 SAFE_FREE(aio_ex);
76 return NULL;
77 }
78 DLIST_ADD(aio_list_head, aio_ex);
79 aio_ex->fsp = fsp;
80 aio_ex->read_req = True;
81 aio_ex->mid = mid;
82 return aio_ex;
83}
84
85/****************************************************************************
86 Create the extended aio struct we must keep around for the lifetime
87 of the aio_write call.
88*****************************************************************************/
89
90static struct aio_extra *create_aio_ex_write(files_struct *fsp,
91 size_t inbuflen,
92 size_t outbuflen,
93 uint16 mid)
94{
95 struct aio_extra *aio_ex = SMB_MALLOC_P(struct aio_extra);
96
97 if (!aio_ex) {
98 return NULL;
99 }
100 ZERO_STRUCTP(aio_ex);
101
102 /* We need space for an output reply of outbuflen bytes. */
103 aio_ex->outbuf = SMB_MALLOC_ARRAY(char, outbuflen);
104 if (!aio_ex->outbuf) {
105 SAFE_FREE(aio_ex);
106 return NULL;
107 }
108
109 if (!(aio_ex->inbuf = SMB_MALLOC_ARRAY(char, inbuflen))) {
110 SAFE_FREE(aio_ex->outbuf);
111 SAFE_FREE(aio_ex);
112 return NULL;
113 }
114
115 DLIST_ADD(aio_list_head, aio_ex);
116 aio_ex->fsp = fsp;
117 aio_ex->read_req = False;
118 aio_ex->mid = mid;
119 return aio_ex;
120}
121
122/****************************************************************************
123 Delete the extended aio struct.
124*****************************************************************************/
125
126static void delete_aio_ex(struct aio_extra *aio_ex)
127{
128 DLIST_REMOVE(aio_list_head, aio_ex);
129 SAFE_FREE(aio_ex->inbuf);
130 SAFE_FREE(aio_ex->outbuf);
131 SAFE_FREE(aio_ex);
132}
133
134/****************************************************************************
135 Given the aiocb struct find the extended aio struct containing it.
136*****************************************************************************/
137
138static struct aio_extra *find_aio_ex(uint16 mid)
139{
140 struct aio_extra *p;
141
142 for( p = aio_list_head; p; p = p->next) {
143 if (mid == p->mid) {
144 return p;
145 }
146 }
147 return NULL;
148}
149
150/****************************************************************************
151 We can have these many aio buffers in flight.
152*****************************************************************************/
153
154static int aio_pending_size;
155static sig_atomic_t signals_received;
156static int outstanding_aio_calls;
157static uint16 *aio_pending_array;
158
159/****************************************************************************
160 Signal handler when an aio request completes.
161*****************************************************************************/
162
163void aio_request_done(uint16_t mid)
164{
165 if (signals_received < aio_pending_size) {
166 aio_pending_array[signals_received] = mid;
167 signals_received++;
168 }
169 /* Else signal is lost. */
170}
171
172static void signal_handler(int sig, siginfo_t *info, void *unused)
173{
174 aio_request_done(info->si_value.sival_int);
175 sys_select_signal(RT_SIGNAL_AIO);
176}
177
178/****************************************************************************
179 Is there a signal waiting ?
180*****************************************************************************/
181
182bool aio_finished(void)
183{
184 return (signals_received != 0);
185}
186
187/****************************************************************************
188 Initialize the signal handler for aio read/write.
189*****************************************************************************/
190
191void initialize_async_io_handler(void)
192{
193 struct sigaction act;
194
195 aio_pending_size = lp_maxmux();
196 aio_pending_array = SMB_MALLOC_ARRAY(uint16, aio_pending_size);
197 SMB_ASSERT(aio_pending_array != NULL);
198
199 ZERO_STRUCT(act);
200 act.sa_sigaction = signal_handler;
201 act.sa_flags = SA_SIGINFO;
202 sigemptyset( &act.sa_mask );
203 if (sigaction(RT_SIGNAL_AIO, &act, NULL) != 0) {
204 DEBUG(0,("Failed to setup RT_SIGNAL_AIO handler\n"));
205 }
206
207 /* the signal can start off blocked due to a bug in bash */
208 BlockSignals(False, RT_SIGNAL_AIO);
209}
210
211/****************************************************************************
212 Set up an aio request from a SMBreadX call.
213*****************************************************************************/
214
215bool schedule_aio_read_and_X(connection_struct *conn,
216 struct smb_request *req,
217 files_struct *fsp, SMB_OFF_T startpos,
218 size_t smb_maxcnt)
219{
220 struct aio_extra *aio_ex;
221 SMB_STRUCT_AIOCB *a;
222 size_t bufsize;
223 size_t min_aio_read_size = lp_aio_read_size(SNUM(conn));
224
225 if (fsp->base_fsp != NULL) {
226 /* No AIO on streams yet */
227 DEBUG(10, ("AIO on streams not yet supported\n"));
228 return false;
229 }
230
231 if ((!min_aio_read_size || (smb_maxcnt < min_aio_read_size))
232 && !SMB_VFS_AIO_FORCE(fsp)) {
233 /* Too small a read for aio request. */
234 DEBUG(10,("schedule_aio_read_and_X: read size (%u) too small "
235 "for minimum aio_read of %u\n",
236 (unsigned int)smb_maxcnt,
237 (unsigned int)min_aio_read_size ));
238 return False;
239 }
240
241 /* Only do this on non-chained and non-chaining reads not using the
242 * write cache. */
243 if (chain_size !=0 || (CVAL(req->inbuf,smb_vwv0) != 0xFF)
244 || (lp_write_cache_size(SNUM(conn)) != 0) ) {
245 return False;
246 }
247
248 if (outstanding_aio_calls >= aio_pending_size) {
249 DEBUG(10,("schedule_aio_read_and_X: Already have %d aio "
250 "activities outstanding.\n",
251 outstanding_aio_calls ));
252 return False;
253 }
254
255 /* The following is safe from integer wrap as we've already checked
256 smb_maxcnt is 128k or less. Wct is 12 for read replies */
257
258 bufsize = smb_size + 12 * 2 + smb_maxcnt;
259
260 if ((aio_ex = create_aio_ex_read(fsp, bufsize, req->mid)) == NULL) {
261 DEBUG(10,("schedule_aio_read_and_X: malloc fail.\n"));
262 return False;
263 }
264
265 construct_reply_common((char *)req->inbuf, aio_ex->outbuf);
266 srv_set_message(aio_ex->outbuf, 12, 0, True);
267 SCVAL(aio_ex->outbuf,smb_vwv0,0xFF); /* Never a chained reply. */
268
269 a = &aio_ex->acb;
270
271 /* Now set up the aio record for the read call. */
272
273 a->aio_fildes = fsp->fh->fd;
274 a->aio_buf = smb_buf(aio_ex->outbuf);
275 a->aio_nbytes = smb_maxcnt;
276 a->aio_offset = startpos;
277 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
278 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
279 a->aio_sigevent.sigev_value.sival_int = aio_ex->mid;
280
281
282 become_root();
283 if (SMB_VFS_AIO_READ(fsp,a) == -1) {
284 DEBUG(0,("schedule_aio_read_and_X: aio_read failed. "
285 "Error %s\n", strerror(errno) ));
286 delete_aio_ex(aio_ex);
287 unbecome_root();
288 return False;
289 }
290 unbecome_root();
291
292 DEBUG(10,("schedule_aio_read_and_X: scheduled aio_read for file %s, "
293 "offset %.0f, len = %u (mid = %u)\n",
294 fsp->fsp_name, (double)startpos, (unsigned int)smb_maxcnt,
295 (unsigned int)aio_ex->mid ));
296
297 srv_defer_sign_response(aio_ex->mid);
298 outstanding_aio_calls++;
299 return True;
300}
301
302/****************************************************************************
303 Set up an aio request from a SMBwriteX call.
304*****************************************************************************/
305
306bool schedule_aio_write_and_X(connection_struct *conn,
307 struct smb_request *req,
308 files_struct *fsp, char *data,
309 SMB_OFF_T startpos,
310 size_t numtowrite)
311{
312 struct aio_extra *aio_ex;
313 SMB_STRUCT_AIOCB *a;
314 size_t inbufsize, outbufsize;
315 bool write_through = BITSETW(req->inbuf+smb_vwv7,0);
316 size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
317
318 if (fsp->base_fsp != NULL) {
319 /* No AIO on streams yet */
320 DEBUG(10, ("AIO on streams not yet supported\n"));
321 return false;
322 }
323
324 if ((!min_aio_write_size || (numtowrite < min_aio_write_size))
325 && !SMB_VFS_AIO_FORCE(fsp)) {
326 /* Too small a write for aio request. */
327 DEBUG(10,("schedule_aio_write_and_X: write size (%u) too "
328 "small for minimum aio_write of %u\n",
329 (unsigned int)numtowrite,
330 (unsigned int)min_aio_write_size ));
331 return False;
332 }
333
334 /* Only do this on non-chained and non-chaining reads not using the
335 * write cache. */
336 if (chain_size !=0 || (CVAL(req->inbuf,smb_vwv0) != 0xFF)
337 || (lp_write_cache_size(SNUM(conn)) != 0) ) {
338 return False;
339 }
340
341 if (outstanding_aio_calls >= aio_pending_size) {
342 DEBUG(3,("schedule_aio_write_and_X: Already have %d aio "
343 "activities outstanding.\n",
344 outstanding_aio_calls ));
345 DEBUG(10,("schedule_aio_write_and_X: failed to schedule "
346 "aio_write for file %s, offset %.0f, len = %u "
347 "(mid = %u)\n",
348 fsp->fsp_name, (double)startpos,
349 (unsigned int)numtowrite,
350 (unsigned int)req->mid ));
351 return False;
352 }
353
354 inbufsize = smb_len(req->inbuf) + 4;
355 reply_outbuf(req, 6, 0);
356 outbufsize = smb_len(req->outbuf) + 4;
357 if (!(aio_ex = create_aio_ex_write(fsp, inbufsize, outbufsize,
358 req->mid))) {
359 DEBUG(0,("schedule_aio_write_and_X: malloc fail.\n"));
360 return False;
361 }
362
363 /* Copy the SMB header already setup in outbuf. */
364 memcpy(aio_ex->inbuf, req->inbuf, inbufsize);
365
366 /* Copy the SMB header already setup in outbuf. */
367 memcpy(aio_ex->outbuf, req->outbuf, outbufsize);
368 TALLOC_FREE(req->outbuf);
369 SCVAL(aio_ex->outbuf,smb_vwv0,0xFF); /* Never a chained reply. */
370
371 a = &aio_ex->acb;
372
373 /* Now set up the aio record for the write call. */
374
375 a->aio_fildes = fsp->fh->fd;
376 a->aio_buf = aio_ex->inbuf + (PTR_DIFF(data, req->inbuf));
377 a->aio_nbytes = numtowrite;
378 a->aio_offset = startpos;
379 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
380 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
381 a->aio_sigevent.sigev_value.sival_int = aio_ex->mid;
382
383 become_root();
384 if (SMB_VFS_AIO_WRITE(fsp,a) == -1) {
385 DEBUG(3,("schedule_aio_wrote_and_X: aio_write failed. "
386 "Error %s\n", strerror(errno) ));
387 delete_aio_ex(aio_ex);
388 unbecome_root();
389 return False;
390 }
391 unbecome_root();
392
393 release_level_2_oplocks_on_change(fsp);
394
395 if (!write_through && !lp_syncalways(SNUM(fsp->conn))
396 && fsp->aio_write_behind) {
397 /* Lie to the client and immediately claim we finished the
398 * write. */
399 SSVAL(aio_ex->outbuf,smb_vwv2,numtowrite);
400 SSVAL(aio_ex->outbuf,smb_vwv4,(numtowrite>>16)&1);
401 show_msg(aio_ex->outbuf);
402 if (!srv_send_smb(smbd_server_fd(),aio_ex->outbuf,
403 IS_CONN_ENCRYPTED(fsp->conn))) {
404 exit_server_cleanly("handle_aio_write: srv_send_smb "
405 "failed.");
406 }
407 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write "
408 "behind for file %s\n", fsp->fsp_name ));
409 } else {
410 srv_defer_sign_response(aio_ex->mid);
411 }
412 outstanding_aio_calls++;
413
414 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write for file "
415 "%s, offset %.0f, len = %u (mid = %u) "
416 "outstanding_aio_calls = %d\n",
417 fsp->fsp_name, (double)startpos, (unsigned int)numtowrite,
418 (unsigned int)aio_ex->mid, outstanding_aio_calls ));
419
420 return True;
421}
422
423
424/****************************************************************************
425 Complete the read and return the data or error back to the client.
426 Returns errno or zero if all ok.
427*****************************************************************************/
428
429static int handle_aio_read_complete(struct aio_extra *aio_ex, int errcode)
430{
431 int outsize;
432 char *outbuf = aio_ex->outbuf;
433 char *data = smb_buf(outbuf);
434 ssize_t nread = SMB_VFS_AIO_RETURN(aio_ex->fsp,&aio_ex->acb);
435
436 if (nread < 0) {
437 /* We're relying here on the fact that if the fd is
438 closed then the aio will complete and aio_return
439 will return an error. Hopefully this is
440 true.... JRA. */
441
442 DEBUG( 3,( "handle_aio_read_complete: file %s nread == %d. "
443 "Error = %s\n",
444 aio_ex->fsp->fsp_name, (int)nread, strerror(errcode) ));
445
446 ERROR_NT(map_nt_error_from_unix(errcode));
447 outsize = srv_set_message(outbuf,0,0,true);
448 } else {
449 outsize = srv_set_message(outbuf,12,nread,False);
450 SSVAL(outbuf,smb_vwv2,0xFFFF); /* Remaining - must be * -1. */
451 SSVAL(outbuf,smb_vwv5,nread);
452 SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
453 SSVAL(outbuf,smb_vwv7,((nread >> 16) & 1));
454 SSVAL(smb_buf(outbuf),-2,nread);
455
456 aio_ex->fsp->fh->pos = aio_ex->acb.aio_offset + nread;
457 aio_ex->fsp->fh->position_information = aio_ex->fsp->fh->pos;
458
459 DEBUG( 3, ( "handle_aio_read_complete file %s max=%d "
460 "nread=%d\n",
461 aio_ex->fsp->fsp_name,
462 (int)aio_ex->acb.aio_nbytes, (int)nread ) );
463
464 }
465 smb_setlen(outbuf,outsize - 4);
466 show_msg(outbuf);
467 if (!srv_send_smb(smbd_server_fd(),outbuf,
468 IS_CONN_ENCRYPTED(aio_ex->fsp->conn))) {
469 exit_server_cleanly("handle_aio_read_complete: srv_send_smb "
470 "failed.");
471 }
472
473 DEBUG(10,("handle_aio_read_complete: scheduled aio_read completed "
474 "for file %s, offset %.0f, len = %u\n",
475 aio_ex->fsp->fsp_name, (double)aio_ex->acb.aio_offset,
476 (unsigned int)nread ));
477
478 return errcode;
479}
480
481/****************************************************************************
482 Complete the write and return the data or error back to the client.
483 Returns errno or zero if all ok.
484*****************************************************************************/
485
486static int handle_aio_write_complete(struct aio_extra *aio_ex, int errcode)
487{
488 files_struct *fsp = aio_ex->fsp;
489 char *outbuf = aio_ex->outbuf;
490 ssize_t numtowrite = aio_ex->acb.aio_nbytes;
491 ssize_t nwritten = SMB_VFS_AIO_RETURN(fsp,&aio_ex->acb);
492
493 if (fsp->aio_write_behind) {
494 if (nwritten != numtowrite) {
495 if (nwritten == -1) {
496 DEBUG(5,("handle_aio_write_complete: "
497 "aio_write_behind failed ! File %s "
498 "is corrupt ! Error %s\n",
499 fsp->fsp_name, strerror(errcode) ));
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 errcode = EIO;
508 }
509 } else {
510 DEBUG(10,("handle_aio_write_complete: "
511 "aio_write_behind completed for file %s\n",
512 fsp->fsp_name ));
513 }
514 /* TODO: should no return 0 in case of an error !!! */
515 return 0;
516 }
517
518 /* We don't need outsize or set_message here as we've already set the
519 fixed size length when we set up the aio call. */
520
521 if(nwritten == -1) {
522 DEBUG( 3,( "handle_aio_write: file %s wanted %u bytes. "
523 "nwritten == %d. Error = %s\n",
524 fsp->fsp_name, (unsigned int)numtowrite,
525 (int)nwritten, strerror(errno) ));
526
527 ERROR_NT(map_nt_error_from_unix(errcode));
528 srv_set_message(outbuf,0,0,true);
529 } else {
530 bool write_through = BITSETW(aio_ex->inbuf+smb_vwv7,0);
531 NTSTATUS status;
532
533 SSVAL(outbuf,smb_vwv2,nwritten);
534 SSVAL(outbuf,smb_vwv4,(nwritten>>16)&1);
535 if (nwritten < (ssize_t)numtowrite) {
536 SCVAL(outbuf,smb_rcls,ERRHRD);
537 SSVAL(outbuf,smb_err,ERRdiskfull);
538 }
539
540 DEBUG(3,("handle_aio_write: fnum=%d num=%d wrote=%d\n",
541 fsp->fnum, (int)numtowrite, (int)nwritten));
542 status = sync_file(fsp->conn,fsp, write_through);
543 if (!NT_STATUS_IS_OK(status)) {
544 errcode = errno;
545 ERROR_BOTH(map_nt_error_from_unix(errcode),
546 ERRHRD, ERRdiskfull);
547 srv_set_message(outbuf,0,0,true);
548 DEBUG(5,("handle_aio_write: sync_file for %s returned %s\n",
549 fsp->fsp_name, nt_errstr(status) ));
550 }
551
552 aio_ex->fsp->fh->pos = aio_ex->acb.aio_offset + nwritten;
553 }
554
555 show_msg(outbuf);
556 if (!srv_send_smb(smbd_server_fd(),outbuf,IS_CONN_ENCRYPTED(fsp->conn))) {
557 exit_server_cleanly("handle_aio_write: srv_send_smb failed.");
558 }
559
560 DEBUG(10,("handle_aio_write_complete: scheduled aio_write completed "
561 "for file %s, offset %.0f, requested %u, written = %u\n",
562 fsp->fsp_name, (double)aio_ex->acb.aio_offset,
563 (unsigned int)numtowrite, (unsigned int)nwritten ));
564
565 return errcode;
566}
567
568/****************************************************************************
569 Handle any aio completion. Returns True if finished (and sets *perr if err
570 was non-zero), False if not.
571*****************************************************************************/
572
573static bool handle_aio_completed(struct aio_extra *aio_ex, int *perr)
574{
575 int err;
576
577 if(!aio_ex) {
578 DEBUG(3, ("handle_aio_completed: Non-existing aio_ex passed\n"));
579 return false;
580 }
581
582 /* Ensure the operation has really completed. */
583 err = SMB_VFS_AIO_ERROR(aio_ex->fsp, &aio_ex->acb);
584 if (err == EINPROGRESS) {
585 DEBUG(10,( "handle_aio_completed: operation mid %u still in "
586 "process for file %s\n",
587 aio_ex->mid, aio_ex->fsp->fsp_name ));
588 return False;
589 } else if (err == ECANCELED) {
590 /* If error is ECANCELED then don't return anything to the
591 * client. */
592 DEBUG(10,( "handle_aio_completed: operation mid %u"
593 " canceled\n", aio_ex->mid));
594 srv_cancel_sign_response(aio_ex->mid, false);
595 return True;
596 }
597
598
599 if (aio_ex->read_req) {
600 err = handle_aio_read_complete(aio_ex, err);
601 } else {
602 err = handle_aio_write_complete(aio_ex, err);
603 }
604
605 if (err) {
606 *perr = err; /* Only save non-zero errors. */
607 }
608
609 return True;
610}
611
612/****************************************************************************
613 Handle any aio completion inline.
614 Returns non-zero errno if fail or zero if all ok.
615*****************************************************************************/
616
617int process_aio_queue(void)
618{
619 int i;
620 int ret = 0;
621
622 BlockSignals(True, RT_SIGNAL_AIO);
623
624 DEBUG(10,("process_aio_queue: signals_received = %d\n",
625 (int)signals_received));
626 DEBUG(10,("process_aio_queue: outstanding_aio_calls = %d\n",
627 outstanding_aio_calls));
628
629 if (!signals_received) {
630 BlockSignals(False, RT_SIGNAL_AIO);
631 return 0;
632 }
633
634 /* Drain all the complete aio_reads. */
635 for (i = 0; i < signals_received; i++) {
636 uint16 mid = aio_pending_array[i];
637 files_struct *fsp = NULL;
638 struct aio_extra *aio_ex = find_aio_ex(mid);
639
640 if (!aio_ex) {
641 DEBUG(3,("process_aio_queue: Can't find record to "
642 "match mid %u.\n", (unsigned int)mid));
643 srv_cancel_sign_response(mid, false);
644 continue;
645 }
646
647 fsp = aio_ex->fsp;
648 if (fsp == NULL) {
649 /* file was closed whilst I/O was outstanding. Just
650 * ignore. */
651 DEBUG( 3,( "process_aio_queue: file closed whilst "
652 "aio outstanding.\n"));
653 srv_cancel_sign_response(mid, false);
654 continue;
655 }
656
657 if (!handle_aio_completed(aio_ex, &ret)) {
658 continue;
659 }
660
661 delete_aio_ex(aio_ex);
662 }
663
664 outstanding_aio_calls -= signals_received;
665 signals_received = 0;
666 BlockSignals(False, RT_SIGNAL_AIO);
667 return ret;
668}
669
670/****************************************************************************
671 We're doing write behind and the client closed the file. Wait up to 30
672 seconds (my arbitrary choice) for the aio to complete. Return 0 if all writes
673 completed, errno to return if not.
674*****************************************************************************/
675
676#define SMB_TIME_FOR_AIO_COMPLETE_WAIT 29
677
678int wait_for_aio_completion(files_struct *fsp)
679{
680 struct aio_extra *aio_ex;
681 const SMB_STRUCT_AIOCB **aiocb_list;
682 int aio_completion_count = 0;
683 time_t start_time = time(NULL);
684 int seconds_left;
685
686 for (seconds_left = SMB_TIME_FOR_AIO_COMPLETE_WAIT;
687 seconds_left >= 0;) {
688 int err = 0;
689 int i;
690 struct timespec ts;
691
692 aio_completion_count = 0;
693 for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
694 if (aio_ex->fsp == fsp) {
695 aio_completion_count++;
696 }
697 }
698
699 if (!aio_completion_count) {
700 return 0;
701 }
702
703 DEBUG(3,("wait_for_aio_completion: waiting for %d aio events "
704 "to complete.\n", aio_completion_count ));
705
706 aiocb_list = SMB_MALLOC_ARRAY(const SMB_STRUCT_AIOCB *,
707 aio_completion_count);
708 if (!aiocb_list) {
709 return ENOMEM;
710 }
711
712 for( i = 0, aio_ex = aio_list_head;
713 aio_ex;
714 aio_ex = aio_ex->next) {
715 if (aio_ex->fsp == fsp) {
716 aiocb_list[i++] = &aio_ex->acb;
717 }
718 }
719
720 /* Now wait up to seconds_left for completion. */
721 ts.tv_sec = seconds_left;
722 ts.tv_nsec = 0;
723
724 DEBUG(10,("wait_for_aio_completion: %d events, doing a wait "
725 "of %d seconds.\n",
726 aio_completion_count, seconds_left ));
727
728 err = SMB_VFS_AIO_SUSPEND(fsp, aiocb_list,
729 aio_completion_count, &ts);
730
731 DEBUG(10,("wait_for_aio_completion: returned err = %d, "
732 "errno = %s\n", err, strerror(errno) ));
733
734 if (err == -1 && errno == EAGAIN) {
735 DEBUG(0,("wait_for_aio_completion: aio_suspend timed "
736 "out waiting for %d events after a wait of "
737 "%d seconds\n", aio_completion_count,
738 seconds_left));
739 /* Timeout. */
740 cancel_aio_by_fsp(fsp);
741 SAFE_FREE(aiocb_list);
742 return EIO;
743 }
744
745 /* One or more events might have completed - process them if
746 * so. */
747 for( i = 0; i < aio_completion_count; i++) {
748 uint16 mid = aiocb_list[i]->aio_sigevent.sigev_value.sival_int;
749
750 aio_ex = find_aio_ex(mid);
751
752 if (!aio_ex) {
753 DEBUG(0, ("wait_for_aio_completion: mid %u "
754 "doesn't match an aio record\n",
755 (unsigned int)mid ));
756 continue;
757 }
758
759 if (!handle_aio_completed(aio_ex, &err)) {
760 continue;
761 }
762 delete_aio_ex(aio_ex);
763 }
764
765 SAFE_FREE(aiocb_list);
766 seconds_left = SMB_TIME_FOR_AIO_COMPLETE_WAIT
767 - (time(NULL) - start_time);
768 }
769
770 /* We timed out - we don't know why. Return ret if already an error,
771 * else EIO. */
772 DEBUG(10,("wait_for_aio_completion: aio_suspend timed out waiting "
773 "for %d events\n",
774 aio_completion_count));
775
776 return EIO;
777}
778
779/****************************************************************************
780 Cancel any outstanding aio requests. The client doesn't care about the reply.
781*****************************************************************************/
782
783void cancel_aio_by_fsp(files_struct *fsp)
784{
785 struct aio_extra *aio_ex;
786
787 for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
788 if (aio_ex->fsp == fsp) {
789 /* Don't delete the aio_extra record as we may have
790 completed and don't yet know it. Just do the
791 aio_cancel call and return. */
792 SMB_VFS_AIO_CANCEL(fsp, &aio_ex->acb);
793 aio_ex->fsp = NULL; /* fsp will be closed when we
794 * return. */
795 }
796 }
797}
798
799#else
800bool aio_finished(void)
801{
802 return False;
803}
804
805void initialize_async_io_handler(void)
806{
807}
808
809int process_aio_queue(void)
810{
811 return False;
812}
813
814bool schedule_aio_read_and_X(connection_struct *conn,
815 struct smb_request *req,
816 files_struct *fsp, SMB_OFF_T startpos,
817 size_t smb_maxcnt)
818{
819 return False;
820}
821
822bool schedule_aio_write_and_X(connection_struct *conn,
823 struct smb_request *req,
824 files_struct *fsp, char *data,
825 SMB_OFF_T startpos,
826 size_t numtowrite)
827{
828 return False;
829}
830
831void cancel_aio_by_fsp(files_struct *fsp)
832{
833}
834
835int wait_for_aio_completion(files_struct *fsp)
836{
837 return ENOSYS;
838}
839#endif
Note: See TracBrowser for help on using the repository browser.