2 Unix SMB/Netbios implementation.
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 2 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, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 /* The signal we'll use to signify aio done. */
28 #define RT_SIGNAL_AIO (SIGRTMIN+3)
31 /****************************************************************************
32 The buffer we keep around whilst an aio request is in process.
33 *****************************************************************************/
36 struct aio_extra *next, *prev;
45 static struct aio_extra *aio_list_head;
47 /****************************************************************************
48 Create the extended aio struct we must keep around for the lifetime
50 *****************************************************************************/
52 static struct aio_extra *create_aio_ex_read(files_struct *fsp,
57 struct aio_extra *aio_ex = SMB_MALLOC_P(struct aio_extra);
63 /* The output buffer stored in the aio_ex is the start of
64 the smb return buffer. The buffer used in the acb
65 is the start of the reply data portion of that buffer. */
66 aio_ex->outbuf = SMB_MALLOC_ARRAY(char, buflen);
67 if (!aio_ex->outbuf) {
71 /* Save the first 8 bytes of inbuf for possible enc data. */
72 aio_ex->inbuf = SMB_MALLOC_ARRAY(char, 8);
74 SAFE_FREE(aio_ex->outbuf);
78 memcpy(aio_ex->inbuf, inbuf, 8);
79 DLIST_ADD(aio_list_head, aio_ex);
81 aio_ex->read_req = True;
86 /****************************************************************************
87 Create the extended aio struct we must keep around for the lifetime
88 of the aio_write call.
89 *****************************************************************************/
91 static struct aio_extra *create_aio_ex_write(files_struct *fsp,
96 struct aio_extra *aio_ex = SMB_MALLOC_P(struct aio_extra);
101 ZERO_STRUCTP(aio_ex);
103 /* We need space for an output reply of outbuflen bytes. */
104 aio_ex->outbuf = SMB_MALLOC_ARRAY(char, outbuflen);
105 if (!aio_ex->outbuf) {
110 if (!(aio_ex->inbuf = SMB_MALLOC_ARRAY(char, inbuflen))) {
111 SAFE_FREE(aio_ex->outbuf);
116 DLIST_ADD(aio_list_head, aio_ex);
118 aio_ex->read_req = False;
123 /****************************************************************************
124 Delete the extended aio struct.
125 *****************************************************************************/
127 static void delete_aio_ex(struct aio_extra *aio_ex)
129 DLIST_REMOVE(aio_list_head, aio_ex);
130 SAFE_FREE(aio_ex->inbuf);
131 SAFE_FREE(aio_ex->outbuf);
135 /****************************************************************************
136 Given the aiocb struct find the extended aio struct containing it.
137 *****************************************************************************/
139 static struct aio_extra *find_aio_ex(uint16 mid)
143 for( p = aio_list_head; p; p = p->next) {
151 /****************************************************************************
152 We can have these many aio buffers in flight.
153 *****************************************************************************/
155 #define AIO_PENDING_SIZE 10
156 static sig_atomic_t signals_received;
157 static int outstanding_aio_calls;
158 static uint16 aio_pending_array[AIO_PENDING_SIZE];
160 /****************************************************************************
161 Signal handler when an aio request completes.
162 *****************************************************************************/
164 static void signal_handler(int sig, siginfo_t *info, void *unused)
166 if (signals_received < AIO_PENDING_SIZE) {
167 aio_pending_array[signals_received] = info->si_value.sival_int;
169 } /* Else signal is lost. */
170 sys_select_signal(RT_SIGNAL_AIO);
173 /****************************************************************************
174 Is there a signal waiting ?
175 *****************************************************************************/
177 BOOL aio_finished(void)
179 return (signals_received != 0);
182 /****************************************************************************
183 Initialize the signal handler for aio read/write.
184 *****************************************************************************/
186 void initialize_async_io_handler(void)
188 struct sigaction act;
191 act.sa_sigaction = signal_handler;
192 act.sa_flags = SA_SIGINFO;
193 sigemptyset( &act.sa_mask );
194 if (sigaction(RT_SIGNAL_AIO, &act, NULL) != 0) {
195 DEBUG(0,("Failed to setup RT_SIGNAL_AIO handler\n"));
198 /* the signal can start off blocked due to a bug in bash */
199 BlockSignals(False, RT_SIGNAL_AIO);
202 /****************************************************************************
203 Set up an aio request from a SMBreadX call.
204 *****************************************************************************/
206 BOOL schedule_aio_read_and_X(connection_struct *conn,
207 char *inbuf, char *outbuf,
208 int length, int len_outbuf,
209 files_struct *fsp, SMB_OFF_T startpos,
212 struct aio_extra *aio_ex;
215 size_t min_aio_read_size = lp_aio_read_size(SNUM(conn));
217 if (!min_aio_read_size || (smb_maxcnt < min_aio_read_size)) {
218 /* Too small a read for aio request. */
219 DEBUG(10,("schedule_aio_read_and_X: read size (%u) too small "
220 "for minimum aio_read of %u\n",
221 (unsigned int)smb_maxcnt,
222 (unsigned int)min_aio_read_size ));
226 /* Only do this on non-chained and non-chaining reads not using the
228 if (chain_size !=0 || (CVAL(inbuf,smb_vwv0) != 0xFF)
229 || (lp_write_cache_size(SNUM(conn)) != 0) ) {
233 if (outstanding_aio_calls >= AIO_PENDING_SIZE) {
234 DEBUG(10,("schedule_aio_read_and_X: Already have %d aio "
235 "activities outstanding.\n",
236 outstanding_aio_calls ));
240 /* The following is safe from integer wrap as we've already
241 checked smb_maxcnt is 128k or less. */
242 bufsize = PTR_DIFF(smb_buf(outbuf),outbuf) + smb_maxcnt;
244 if ((aio_ex = create_aio_ex_read(fsp, bufsize,
245 SVAL(inbuf,smb_mid))) == NULL) {
246 DEBUG(10,("schedule_aio_read_and_X: malloc fail.\n"));
250 /* Copy the SMB header already setup in outbuf. */
251 memcpy(aio_ex->outbuf, outbuf, smb_buf(outbuf) - outbuf);
252 SCVAL(aio_ex->outbuf,smb_vwv0,0xFF); /* Never a chained reply. */
256 /* Now set up the aio record for the read call. */
258 a->aio_fildes = fsp->fh->fd;
259 a->aio_buf = smb_buf(aio_ex->outbuf);
260 a->aio_nbytes = smb_maxcnt;
261 a->aio_offset = startpos;
262 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
263 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
264 a->aio_sigevent.sigev_value.sival_int = aio_ex->mid;
266 if (SMB_VFS_AIO_READ(fsp,a) == -1) {
267 DEBUG(0,("schedule_aio_read_and_X: aio_read failed. "
268 "Error %s\n", strerror(errno) ));
269 delete_aio_ex(aio_ex);
273 DEBUG(10,("schedule_aio_read_and_X: scheduled aio_read for file %s, "
274 "offset %.0f, len = %u (mid = %u)\n",
275 fsp->fsp_name, (double)startpos, (unsigned int)smb_maxcnt,
276 (unsigned int)aio_ex->mid ));
278 srv_defer_sign_response(aio_ex->mid);
279 outstanding_aio_calls++;
283 /****************************************************************************
284 Set up an aio request from a SMBwriteX call.
285 *****************************************************************************/
287 BOOL schedule_aio_write_and_X(connection_struct *conn,
288 char *inbuf, char *outbuf,
289 int length, int len_outbuf,
290 files_struct *fsp, char *data,
294 struct aio_extra *aio_ex;
296 size_t inbufsize, outbufsize;
297 size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
299 if (!min_aio_write_size || (numtowrite < min_aio_write_size)) {
300 /* Too small a write for aio request. */
301 DEBUG(10,("schedule_aio_write_and_X: write size (%u) too "
302 "small for minimum aio_write of %u\n",
303 (unsigned int)numtowrite,
304 (unsigned int)min_aio_write_size ));
308 /* Only do this on non-chained and non-chaining reads not using the
310 if (chain_size !=0 || (CVAL(inbuf,smb_vwv0) != 0xFF)
311 || (lp_write_cache_size(SNUM(conn)) != 0) ) {
315 if (outstanding_aio_calls >= AIO_PENDING_SIZE) {
316 DEBUG(3,("schedule_aio_write_and_X: Already have %d aio "
317 "activities outstanding.\n",
318 outstanding_aio_calls ));
319 DEBUG(10,("schedule_aio_write_and_X: failed to schedule "
320 "aio_write for file %s, offset %.0f, len = %u "
322 fsp->fsp_name, (double)startpos,
323 (unsigned int)numtowrite,
324 (unsigned int)SVAL(inbuf,smb_mid) ));
328 inbufsize = smb_len(inbuf) + 4;
329 outbufsize = smb_len(outbuf) + 4;
330 if (!(aio_ex = create_aio_ex_write(fsp, inbufsize, outbufsize,
331 SVAL(inbuf,smb_mid)))) {
332 DEBUG(0,("schedule_aio_write_and_X: malloc fail.\n"));
336 /* Copy the SMB header already setup in outbuf. */
337 memcpy(aio_ex->inbuf, inbuf, inbufsize);
339 /* Copy the SMB header already setup in outbuf. */
340 memcpy(aio_ex->outbuf, outbuf, outbufsize);
341 SCVAL(aio_ex->outbuf,smb_vwv0,0xFF); /* Never a chained reply. */
345 /* Now set up the aio record for the write call. */
347 a->aio_fildes = fsp->fh->fd;
348 a->aio_buf = aio_ex->inbuf + (PTR_DIFF(data, inbuf));
349 a->aio_nbytes = numtowrite;
350 a->aio_offset = startpos;
351 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
352 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
353 a->aio_sigevent.sigev_value.sival_int = aio_ex->mid;
355 if (SMB_VFS_AIO_WRITE(fsp,a) == -1) {
356 DEBUG(3,("schedule_aio_wrote_and_X: aio_write failed. "
357 "Error %s\n", strerror(errno) ));
358 delete_aio_ex(aio_ex);
362 srv_defer_sign_response(aio_ex->mid);
363 outstanding_aio_calls++;
365 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write for file "
366 "%s, offset %.0f, len = %u (mid = %u) "
367 "outstanding_aio_calls = %d\n",
368 fsp->fsp_name, (double)startpos, (unsigned int)numtowrite,
369 (unsigned int)aio_ex->mid, outstanding_aio_calls ));
375 /****************************************************************************
376 Complete the read and return the data or error back to the client.
377 Returns errno or zero if all ok.
378 *****************************************************************************/
380 static int handle_aio_read_complete(struct aio_extra *aio_ex)
384 char *outbuf = aio_ex->outbuf;
385 char *data = smb_buf(outbuf);
386 ssize_t nread = SMB_VFS_AIO_RETURN(aio_ex->fsp,&aio_ex->acb);
389 /* We're relying here on the fact that if the fd is
390 closed then the aio will complete and aio_return
391 will return an error. Hopefully this is
394 /* If errno is ECANCELED then don't return anything to the
396 if (errno == ECANCELED) {
397 srv_cancel_sign_response(aio_ex->mid);
401 DEBUG( 3,( "handle_aio_read_complete: file %s nread == -1. "
403 aio_ex->fsp->fsp_name, strerror(errno) ));
405 outsize = (UNIXERROR(ERRDOS,ERRnoaccess));
408 outsize = set_message(outbuf,12,nread,False);
409 SSVAL(outbuf,smb_vwv2,0xFFFF); /* Remaining - must be * -1. */
410 SSVAL(outbuf,smb_vwv5,nread);
411 SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
412 SSVAL(outbuf,smb_vwv7,((nread >> 16) & 1));
413 SSVAL(smb_buf(outbuf),-2,nread);
415 DEBUG( 3, ( "handle_aio_read_complete file %s max=%d "
417 aio_ex->fsp->fsp_name,
418 aio_ex->acb.aio_nbytes, (int)nread ) );
421 smb_setlen(aio_ex->inbuf,outbuf,outsize - 4);
423 if (!send_smb(smbd_server_fd(),outbuf)) {
424 exit_server_cleanly("handle_aio_read_complete: send_smb "
428 DEBUG(10,("handle_aio_read_complete: scheduled aio_read completed "
429 "for file %s, offset %.0f, len = %u\n",
430 aio_ex->fsp->fsp_name, (double)aio_ex->acb.aio_offset,
431 (unsigned int)nread ));
436 /****************************************************************************
437 Complete the write and return the data or error back to the client.
438 Returns errno or zero if all ok.
439 *****************************************************************************/
441 static int handle_aio_write_complete(struct aio_extra *aio_ex)
444 files_struct *fsp = aio_ex->fsp;
445 char *outbuf = aio_ex->outbuf;
446 ssize_t numtowrite = aio_ex->acb.aio_nbytes;
447 ssize_t nwritten = SMB_VFS_AIO_RETURN(fsp,&aio_ex->acb);
449 /* We don't need outsize or set_message here as we've already set the
450 fixed size length when we set up the aio call. */
453 DEBUG( 3,( "handle_aio_write: file %s wanted %u bytes. "
454 "nwritten == %d. Error = %s\n",
455 fsp->fsp_name, (unsigned int)numtowrite,
456 (int)nwritten, strerror(errno) ));
458 /* If errno is ECANCELED then don't return anything to the
460 if (errno == ECANCELED) {
461 srv_cancel_sign_response(aio_ex->mid);
465 UNIXERROR(ERRHRD,ERRdiskfull);
468 BOOL write_through = BITSETW(aio_ex->inbuf+smb_vwv7,0);
470 SSVAL(outbuf,smb_vwv2,nwritten);
471 SSVAL(outbuf,smb_vwv4,(nwritten>>16)&1);
472 if (nwritten < (ssize_t)numtowrite) {
473 SCVAL(outbuf,smb_rcls,ERRHRD);
474 SSVAL(outbuf,smb_err,ERRdiskfull);
477 DEBUG(3,("handle_aio_write: fnum=%d num=%d wrote=%d\n",
478 fsp->fnum, (int)numtowrite, (int)nwritten));
479 sync_file(fsp->conn,fsp, write_through);
483 if (!send_smb(smbd_server_fd(),outbuf)) {
484 exit_server_cleanly("handle_aio_write: send_smb failed.");
487 DEBUG(10,("handle_aio_write_complete: scheduled aio_write completed "
488 "for file %s, offset %.0f, requested %u, written = %u\n",
489 fsp->fsp_name, (double)aio_ex->acb.aio_offset,
490 (unsigned int)numtowrite, (unsigned int)nwritten ));
495 /****************************************************************************
496 Handle any aio completion. Returns True if finished (and sets *perr if err
497 was non-zero), False if not.
498 *****************************************************************************/
500 static BOOL handle_aio_completed(struct aio_extra *aio_ex, int *perr)
504 /* Ensure the operation has really completed. */
505 if (SMB_VFS_AIO_ERROR(aio_ex->fsp, &aio_ex->acb) == EINPROGRESS) {
506 DEBUG(10,( "handle_aio_completed: operation mid %u still in "
507 "process for file %s\n",
508 aio_ex->mid, aio_ex->fsp->fsp_name ));
512 if (aio_ex->read_req) {
513 err = handle_aio_read_complete(aio_ex);
515 err = handle_aio_write_complete(aio_ex);
519 *perr = err; /* Only save non-zero errors. */
525 /****************************************************************************
526 Handle any aio completion inline.
527 Returns non-zero errno if fail or zero if all ok.
528 *****************************************************************************/
530 int process_aio_queue(void)
535 BlockSignals(True, RT_SIGNAL_AIO);
537 DEBUG(10,("process_aio_queue: signals_received = %d\n",
538 (int)signals_received));
539 DEBUG(10,("process_aio_queue: outstanding_aio_calls = %d\n",
540 outstanding_aio_calls));
542 if (!signals_received) {
543 BlockSignals(False, RT_SIGNAL_AIO);
547 /* Drain all the complete aio_reads. */
548 for (i = 0; i < signals_received; i++) {
549 uint16 mid = aio_pending_array[i];
550 files_struct *fsp = NULL;
551 struct aio_extra *aio_ex = find_aio_ex(mid);
554 DEBUG(3,("process_aio_queue: Can't find record to "
555 "match mid %u.\n", (unsigned int)mid));
556 srv_cancel_sign_response(mid);
562 /* file was closed whilst I/O was outstanding. Just
564 DEBUG( 3,( "process_aio_queue: file closed whilst "
565 "aio outstanding.\n"));
566 srv_cancel_sign_response(mid);
570 if (!handle_aio_completed(aio_ex, &ret)) {
574 delete_aio_ex(aio_ex);
577 outstanding_aio_calls -= signals_received;
578 signals_received = 0;
579 BlockSignals(False, RT_SIGNAL_AIO);
583 /****************************************************************************
584 Cancel any outstanding aio requests. The client doesn't care about the reply.
585 *****************************************************************************/
587 void cancel_aio_by_fsp(files_struct *fsp)
589 struct aio_extra *aio_ex;
591 for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
592 if (aio_ex->fsp == fsp) {
593 /* Don't delete the aio_extra record as we may have
594 completed and don't yet know it. Just do the
595 aio_cancel call and return. */
596 SMB_VFS_AIO_CANCEL(fsp,fsp->fh->fd, &aio_ex->acb);
597 aio_ex->fsp = NULL; /* fsp will be closed when we
604 BOOL aio_finished(void)
609 void initialize_async_io_handler(void)
613 int process_aio_queue(void)
618 BOOL schedule_aio_read_and_X(connection_struct *conn,
619 char *inbuf, char *outbuf,
620 int length, int len_outbuf,
621 files_struct *fsp, SMB_OFF_T startpos,
627 BOOL schedule_aio_write_and_X(connection_struct *conn,
628 char *inbuf, char *outbuf,
629 int length, int len_outbuf,
630 files_struct *fsp, char *data,
637 void cancel_aio_by_fsp(files_struct *fsp)