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
), inbuf
)) == 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 *inbuf
= aio_ex
->inbuf
;
386 char *data
= smb_buf(outbuf
);
387 ssize_t nread
= SMB_VFS_AIO_RETURN(aio_ex
->fsp
,&aio_ex
->acb
);
390 /* We're relying here on the fact that if the fd is
391 closed then the aio will complete and aio_return
392 will return an error. Hopefully this is
395 /* If errno is ECANCELED then don't return anything to the
397 if (errno
== ECANCELED
) {
398 srv_cancel_sign_response(aio_ex
->mid
);
402 DEBUG( 3,( "handle_aio_read_complete: file %s nread == -1. "
404 aio_ex
->fsp
->fsp_name
, strerror(errno
) ));
406 outsize
= (UNIXERROR(ERRDOS
,ERRnoaccess
));
409 outsize
= set_message(inbuf
,outbuf
,12,nread
,False
);
410 SSVAL(outbuf
,smb_vwv2
,0xFFFF); /* Remaining - must be * -1. */
411 SSVAL(outbuf
,smb_vwv5
,nread
);
412 SSVAL(outbuf
,smb_vwv6
,smb_offset(data
,outbuf
));
413 SSVAL(outbuf
,smb_vwv7
,((nread
>> 16) & 1));
414 SSVAL(smb_buf(outbuf
),-2,nread
);
416 DEBUG( 3, ( "handle_aio_read_complete file %s max=%d "
418 aio_ex
->fsp
->fsp_name
,
419 aio_ex
->acb
.aio_nbytes
, (int)nread
) );
422 smb_setlen(inbuf
,outbuf
,outsize
- 4);
424 if (!send_smb(smbd_server_fd(),outbuf
)) {
425 exit_server_cleanly("handle_aio_read_complete: send_smb "
429 DEBUG(10,("handle_aio_read_complete: scheduled aio_read completed "
430 "for file %s, offset %.0f, len = %u\n",
431 aio_ex
->fsp
->fsp_name
, (double)aio_ex
->acb
.aio_offset
,
432 (unsigned int)nread
));
437 /****************************************************************************
438 Complete the write and return the data or error back to the client.
439 Returns errno or zero if all ok.
440 *****************************************************************************/
442 static int handle_aio_write_complete(struct aio_extra
*aio_ex
)
445 files_struct
*fsp
= aio_ex
->fsp
;
446 char *outbuf
= aio_ex
->outbuf
;
447 char *inbuf
= aio_ex
->inbuf
;
448 ssize_t numtowrite
= aio_ex
->acb
.aio_nbytes
;
449 ssize_t nwritten
= SMB_VFS_AIO_RETURN(fsp
,&aio_ex
->acb
);
451 /* We don't need outsize or set_message here as we've already set the
452 fixed size length when we set up the aio call. */
455 DEBUG( 3,( "handle_aio_write: file %s wanted %u bytes. "
456 "nwritten == %d. Error = %s\n",
457 fsp
->fsp_name
, (unsigned int)numtowrite
,
458 (int)nwritten
, strerror(errno
) ));
460 /* If errno is ECANCELED then don't return anything to the
462 if (errno
== ECANCELED
) {
463 srv_cancel_sign_response(aio_ex
->mid
);
467 UNIXERROR(ERRHRD
,ERRdiskfull
);
470 BOOL write_through
= BITSETW(aio_ex
->inbuf
+smb_vwv7
,0);
472 SSVAL(outbuf
,smb_vwv2
,nwritten
);
473 SSVAL(outbuf
,smb_vwv4
,(nwritten
>>16)&1);
474 if (nwritten
< (ssize_t
)numtowrite
) {
475 SCVAL(outbuf
,smb_rcls
,ERRHRD
);
476 SSVAL(outbuf
,smb_err
,ERRdiskfull
);
479 DEBUG(3,("handle_aio_write: fnum=%d num=%d wrote=%d\n",
480 fsp
->fnum
, (int)numtowrite
, (int)nwritten
));
481 sync_file(fsp
->conn
,fsp
, write_through
);
485 if (!send_smb(smbd_server_fd(),outbuf
)) {
486 exit_server_cleanly("handle_aio_write: send_smb failed.");
489 DEBUG(10,("handle_aio_write_complete: scheduled aio_write completed "
490 "for file %s, offset %.0f, requested %u, written = %u\n",
491 fsp
->fsp_name
, (double)aio_ex
->acb
.aio_offset
,
492 (unsigned int)numtowrite
, (unsigned int)nwritten
));
497 /****************************************************************************
498 Handle any aio completion. Returns True if finished (and sets *perr if err
499 was non-zero), False if not.
500 *****************************************************************************/
502 static BOOL
handle_aio_completed(struct aio_extra
*aio_ex
, int *perr
)
506 /* Ensure the operation has really completed. */
507 if (SMB_VFS_AIO_ERROR(aio_ex
->fsp
, &aio_ex
->acb
) == EINPROGRESS
) {
508 DEBUG(10,( "handle_aio_completed: operation mid %u still in "
509 "process for file %s\n",
510 aio_ex
->mid
, aio_ex
->fsp
->fsp_name
));
514 if (aio_ex
->read_req
) {
515 err
= handle_aio_read_complete(aio_ex
);
517 err
= handle_aio_write_complete(aio_ex
);
521 *perr
= err
; /* Only save non-zero errors. */
527 /****************************************************************************
528 Handle any aio completion inline.
529 Returns non-zero errno if fail or zero if all ok.
530 *****************************************************************************/
532 int process_aio_queue(void)
537 BlockSignals(True
, RT_SIGNAL_AIO
);
539 DEBUG(10,("process_aio_queue: signals_received = %d\n",
540 (int)signals_received
));
541 DEBUG(10,("process_aio_queue: outstanding_aio_calls = %d\n",
542 outstanding_aio_calls
));
544 if (!signals_received
) {
545 BlockSignals(False
, RT_SIGNAL_AIO
);
549 /* Drain all the complete aio_reads. */
550 for (i
= 0; i
< signals_received
; i
++) {
551 uint16 mid
= aio_pending_array
[i
];
552 files_struct
*fsp
= NULL
;
553 struct aio_extra
*aio_ex
= find_aio_ex(mid
);
556 DEBUG(3,("process_aio_queue: Can't find record to "
557 "match mid %u.\n", (unsigned int)mid
));
558 srv_cancel_sign_response(mid
);
564 /* file was closed whilst I/O was outstanding. Just
566 DEBUG( 3,( "process_aio_queue: file closed whilst "
567 "aio outstanding.\n"));
568 srv_cancel_sign_response(mid
);
572 if (!handle_aio_completed(aio_ex
, &ret
)) {
576 delete_aio_ex(aio_ex
);
579 outstanding_aio_calls
-= signals_received
;
580 signals_received
= 0;
581 BlockSignals(False
, RT_SIGNAL_AIO
);
585 /****************************************************************************
586 Cancel any outstanding aio requests. The client doesn't care about the reply.
587 *****************************************************************************/
589 void cancel_aio_by_fsp(files_struct
*fsp
)
591 struct aio_extra
*aio_ex
;
593 for( aio_ex
= aio_list_head
; aio_ex
; aio_ex
= aio_ex
->next
) {
594 if (aio_ex
->fsp
== fsp
) {
595 /* Don't delete the aio_extra record as we may have
596 completed and don't yet know it. Just do the
597 aio_cancel call and return. */
598 SMB_VFS_AIO_CANCEL(fsp
,fsp
->fh
->fd
, &aio_ex
->acb
);
599 aio_ex
->fsp
= NULL
; /* fsp will be closed when we
606 BOOL
aio_finished(void)
611 void initialize_async_io_handler(void)
615 int process_aio_queue(void)
620 BOOL
schedule_aio_read_and_X(connection_struct
*conn
,
621 char *inbuf
, char *outbuf
,
622 int length
, int len_outbuf
,
623 files_struct
*fsp
, SMB_OFF_T startpos
,
629 BOOL
schedule_aio_write_and_X(connection_struct
*conn
,
630 char *inbuf
, char *outbuf
,
631 int length
, int len_outbuf
,
632 files_struct
*fsp
, char *data
,
639 void cancel_aio_by_fsp(files_struct
*fsp
)