r23041: Remainder of fix for 4630: fix special case of unix_to_nt_time() for
[Samba/nascimento.git] / source3 / smbd / aio.c
blobbee6ebc89a9292381a974fee090fe9db4f439411
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 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.
22 #include "includes.h"
24 #if defined(WITH_AIO)
26 /* The signal we'll use to signify aio done. */
27 #ifndef RT_SIGNAL_AIO
28 #define RT_SIGNAL_AIO (SIGRTMIN+3)
29 #endif
31 /****************************************************************************
32 The buffer we keep around whilst an aio request is in process.
33 *****************************************************************************/
35 struct aio_extra {
36 struct aio_extra *next, *prev;
37 SMB_STRUCT_AIOCB acb;
38 files_struct *fsp;
39 BOOL read_req;
40 uint16 mid;
41 char *inbuf;
42 char *outbuf;
45 static struct aio_extra *aio_list_head;
47 /****************************************************************************
48 Create the extended aio struct we must keep around for the lifetime
49 of the aio_read call.
50 *****************************************************************************/
52 static struct aio_extra *create_aio_ex_read(files_struct *fsp,
53 size_t buflen,
54 uint16 mid,
55 const char *inbuf)
57 struct aio_extra *aio_ex = SMB_MALLOC_P(struct aio_extra);
59 if (!aio_ex) {
60 return NULL;
62 ZERO_STRUCTP(aio_ex);
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) {
68 SAFE_FREE(aio_ex);
69 return NULL;
71 /* Save the first 8 bytes of inbuf for possible enc data. */
72 aio_ex->inbuf = SMB_MALLOC_ARRAY(char, 8);
73 if (!aio_ex->inbuf) {
74 SAFE_FREE(aio_ex->outbuf);
75 SAFE_FREE(aio_ex);
76 return NULL;
78 memcpy(aio_ex->inbuf, inbuf, 8);
79 DLIST_ADD(aio_list_head, aio_ex);
80 aio_ex->fsp = fsp;
81 aio_ex->read_req = True;
82 aio_ex->mid = mid;
83 return aio_ex;
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,
92 size_t inbuflen,
93 size_t outbuflen,
94 uint16 mid)
96 struct aio_extra *aio_ex = SMB_MALLOC_P(struct aio_extra);
98 if (!aio_ex) {
99 return NULL;
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) {
106 SAFE_FREE(aio_ex);
107 return NULL;
110 if (!(aio_ex->inbuf = SMB_MALLOC_ARRAY(char, inbuflen))) {
111 SAFE_FREE(aio_ex->outbuf);
112 SAFE_FREE(aio_ex);
113 return NULL;
116 DLIST_ADD(aio_list_head, aio_ex);
117 aio_ex->fsp = fsp;
118 aio_ex->read_req = False;
119 aio_ex->mid = mid;
120 return aio_ex;
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);
132 SAFE_FREE(aio_ex);
135 /****************************************************************************
136 Given the aiocb struct find the extended aio struct containing it.
137 *****************************************************************************/
139 static struct aio_extra *find_aio_ex(uint16 mid)
141 struct aio_extra *p;
143 for( p = aio_list_head; p; p = p->next) {
144 if (mid == p->mid) {
145 return p;
148 return NULL;
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;
168 signals_received++;
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;
190 ZERO_STRUCT(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,
210 size_t smb_maxcnt)
212 struct aio_extra *aio_ex;
213 SMB_STRUCT_AIOCB *a;
214 size_t bufsize;
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 ));
223 return False;
226 /* Only do this on non-chained and non-chaining reads not using the
227 * write cache. */
228 if (chain_size !=0 || (CVAL(inbuf,smb_vwv0) != 0xFF)
229 || (lp_write_cache_size(SNUM(conn)) != 0) ) {
230 return False;
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 ));
237 return False;
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"));
247 return False;
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. */
254 a = &aio_ex->acb;
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);
270 return False;
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++;
280 return True;
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,
291 SMB_OFF_T startpos,
292 size_t numtowrite)
294 struct aio_extra *aio_ex;
295 SMB_STRUCT_AIOCB *a;
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 ));
305 return False;
308 /* Only do this on non-chained and non-chaining reads not using the
309 * write cache. */
310 if (chain_size !=0 || (CVAL(inbuf,smb_vwv0) != 0xFF)
311 || (lp_write_cache_size(SNUM(conn)) != 0) ) {
312 return False;
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 "
321 "(mid = %u)\n",
322 fsp->fsp_name, (double)startpos,
323 (unsigned int)numtowrite,
324 (unsigned int)SVAL(inbuf,smb_mid) ));
325 return False;
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"));
333 return False;
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. */
343 a = &aio_ex->acb;
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);
359 return False;
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 ));
371 return True;
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)
382 int ret = 0;
383 int outsize;
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);
389 if (nread < 0) {
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
393 true.... JRA. */
395 /* If errno is ECANCELED then don't return anything to the
396 * client. */
397 if (errno == ECANCELED) {
398 srv_cancel_sign_response(aio_ex->mid);
399 return 0;
402 DEBUG( 3,( "handle_aio_read_complete: file %s nread == -1. "
403 "Error = %s\n",
404 aio_ex->fsp->fsp_name, strerror(errno) ));
406 outsize = (UNIXERROR(ERRDOS,ERRnoaccess));
407 ret = errno;
408 } else {
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 "
417 "nread=%d\n",
418 aio_ex->fsp->fsp_name,
419 aio_ex->acb.aio_nbytes, (int)nread ) );
422 smb_setlen(inbuf,outbuf,outsize - 4);
423 show_msg(outbuf);
424 if (!send_smb(smbd_server_fd(),outbuf)) {
425 exit_server_cleanly("handle_aio_read_complete: send_smb "
426 "failed.");
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 ));
434 return ret;
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)
444 int ret = 0;
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. */
454 if(nwritten == -1) {
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
461 * client. */
462 if (errno == ECANCELED) {
463 srv_cancel_sign_response(aio_ex->mid);
464 return 0;
467 UNIXERROR(ERRHRD,ERRdiskfull);
468 ret = errno;
469 } else {
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);
484 show_msg(outbuf);
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 ));
494 return ret;
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)
504 int err;
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 ));
511 return False;
514 if (aio_ex->read_req) {
515 err = handle_aio_read_complete(aio_ex);
516 } else {
517 err = handle_aio_write_complete(aio_ex);
520 if (err) {
521 *perr = err; /* Only save non-zero errors. */
524 return True;
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)
534 int i;
535 int ret = 0;
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);
546 return 0;
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);
555 if (!aio_ex) {
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);
559 continue;
562 fsp = aio_ex->fsp;
563 if (fsp == NULL) {
564 /* file was closed whilst I/O was outstanding. Just
565 * ignore. */
566 DEBUG( 3,( "process_aio_queue: file closed whilst "
567 "aio outstanding.\n"));
568 srv_cancel_sign_response(mid);
569 continue;
572 if (!handle_aio_completed(aio_ex, &ret)) {
573 continue;
576 delete_aio_ex(aio_ex);
579 outstanding_aio_calls -= signals_received;
580 signals_received = 0;
581 BlockSignals(False, RT_SIGNAL_AIO);
582 return ret;
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
600 * return. */
605 #else
606 BOOL aio_finished(void)
608 return False;
611 void initialize_async_io_handler(void)
615 int process_aio_queue(void)
617 return False;
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,
624 size_t smb_maxcnt)
626 return False;
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,
633 SMB_OFF_T startpos,
634 size_t numtowrite)
636 return False;
639 void cancel_aio_by_fsp(files_struct *fsp)
643 #endif