r21972: - add string_term_tdb_data() it creates a null-terminates tdb key from a...
[Samba/nascimento.git] / source3 / smbd / aio.c
bloba85cf901ae2e3bb2832dc650c05015f654523575
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, size_t buflen,
53 uint16 mid)
55 struct aio_extra *aio_ex = SMB_MALLOC_P(struct aio_extra);
57 if (!aio_ex) {
58 return NULL;
60 ZERO_STRUCTP(aio_ex);
61 /* The output buffer stored in the aio_ex is the start of
62 the smb return buffer. The buffer used in the acb
63 is the start of the reply data portion of that buffer. */
64 aio_ex->outbuf = SMB_MALLOC_ARRAY(char, buflen);
65 if (!aio_ex->outbuf) {
66 SAFE_FREE(aio_ex);
67 return NULL;
69 DLIST_ADD(aio_list_head, aio_ex);
70 aio_ex->fsp = fsp;
71 aio_ex->read_req = True;
72 aio_ex->mid = mid;
73 return aio_ex;
76 /****************************************************************************
77 Create the extended aio struct we must keep around for the lifetime
78 of the aio_write call.
79 *****************************************************************************/
81 static struct aio_extra *create_aio_ex_write(files_struct *fsp,
82 size_t inbuflen,
83 size_t outbuflen,
84 uint16 mid)
86 struct aio_extra *aio_ex = SMB_MALLOC_P(struct aio_extra);
88 if (!aio_ex) {
89 return NULL;
91 ZERO_STRUCTP(aio_ex);
93 /* We need space for an output reply of outbuflen bytes. */
94 aio_ex->outbuf = SMB_MALLOC_ARRAY(char, outbuflen);
95 if (!aio_ex->outbuf) {
96 SAFE_FREE(aio_ex);
97 return NULL;
100 if (!(aio_ex->inbuf = SMB_MALLOC_ARRAY(char, inbuflen))) {
101 SAFE_FREE(aio_ex->outbuf);
102 SAFE_FREE(aio_ex);
103 return NULL;
106 DLIST_ADD(aio_list_head, aio_ex);
107 aio_ex->fsp = fsp;
108 aio_ex->read_req = False;
109 aio_ex->mid = mid;
110 return aio_ex;
113 /****************************************************************************
114 Delete the extended aio struct.
115 *****************************************************************************/
117 static void delete_aio_ex(struct aio_extra *aio_ex)
119 DLIST_REMOVE(aio_list_head, aio_ex);
120 SAFE_FREE(aio_ex->inbuf);
121 SAFE_FREE(aio_ex->outbuf);
122 SAFE_FREE(aio_ex);
125 /****************************************************************************
126 Given the aiocb struct find the extended aio struct containing it.
127 *****************************************************************************/
129 static struct aio_extra *find_aio_ex(uint16 mid)
131 struct aio_extra *p;
133 for( p = aio_list_head; p; p = p->next) {
134 if (mid == p->mid) {
135 return p;
138 return NULL;
141 /****************************************************************************
142 We can have these many aio buffers in flight.
143 *****************************************************************************/
145 #define AIO_PENDING_SIZE 10
146 static sig_atomic_t signals_received;
147 static int outstanding_aio_calls;
148 static uint16 aio_pending_array[AIO_PENDING_SIZE];
150 /****************************************************************************
151 Signal handler when an aio request completes.
152 *****************************************************************************/
154 static void signal_handler(int sig, siginfo_t *info, void *unused)
156 if (signals_received < AIO_PENDING_SIZE) {
157 aio_pending_array[signals_received] = info->si_value.sival_int;
158 signals_received++;
159 } /* Else signal is lost. */
160 sys_select_signal(RT_SIGNAL_AIO);
163 /****************************************************************************
164 Is there a signal waiting ?
165 *****************************************************************************/
167 BOOL aio_finished(void)
169 return (signals_received != 0);
172 /****************************************************************************
173 Initialize the signal handler for aio read/write.
174 *****************************************************************************/
176 void initialize_async_io_handler(void)
178 struct sigaction act;
180 ZERO_STRUCT(act);
181 act.sa_sigaction = signal_handler;
182 act.sa_flags = SA_SIGINFO;
183 sigemptyset( &act.sa_mask );
184 if (sigaction(RT_SIGNAL_AIO, &act, NULL) != 0) {
185 DEBUG(0,("Failed to setup RT_SIGNAL_AIO handler\n"));
188 /* the signal can start off blocked due to a bug in bash */
189 BlockSignals(False, RT_SIGNAL_AIO);
192 /****************************************************************************
193 Set up an aio request from a SMBreadX call.
194 *****************************************************************************/
196 BOOL schedule_aio_read_and_X(connection_struct *conn,
197 char *inbuf, char *outbuf,
198 int length, int len_outbuf,
199 files_struct *fsp, SMB_OFF_T startpos,
200 size_t smb_maxcnt)
202 struct aio_extra *aio_ex;
203 SMB_STRUCT_AIOCB *a;
204 size_t bufsize;
205 size_t min_aio_read_size = lp_aio_read_size(SNUM(conn));
207 if (!min_aio_read_size || (smb_maxcnt < min_aio_read_size)) {
208 /* Too small a read for aio request. */
209 DEBUG(10,("schedule_aio_read_and_X: read size (%u) too small "
210 "for minimum aio_read of %u\n",
211 (unsigned int)smb_maxcnt,
212 (unsigned int)min_aio_read_size ));
213 return False;
216 /* Only do this on non-chained and non-chaining reads not using the
217 * write cache. */
218 if (chain_size !=0 || (CVAL(inbuf,smb_vwv0) != 0xFF)
219 || (lp_write_cache_size(SNUM(conn)) != 0) ) {
220 return False;
223 if (outstanding_aio_calls >= AIO_PENDING_SIZE) {
224 DEBUG(10,("schedule_aio_read_and_X: Already have %d aio "
225 "activities outstanding.\n",
226 outstanding_aio_calls ));
227 return False;
230 /* The following is safe from integer wrap as we've already
231 checked smb_maxcnt is 128k or less. */
232 bufsize = PTR_DIFF(smb_buf(outbuf),outbuf) + smb_maxcnt;
234 if ((aio_ex = create_aio_ex_read(fsp, bufsize,
235 SVAL(inbuf,smb_mid))) == NULL) {
236 DEBUG(10,("schedule_aio_read_and_X: malloc fail.\n"));
237 return False;
240 /* Copy the SMB header already setup in outbuf. */
241 memcpy(aio_ex->outbuf, outbuf, smb_buf(outbuf) - outbuf);
242 SCVAL(aio_ex->outbuf,smb_vwv0,0xFF); /* Never a chained reply. */
244 a = &aio_ex->acb;
246 /* Now set up the aio record for the read call. */
248 a->aio_fildes = fsp->fh->fd;
249 a->aio_buf = smb_buf(aio_ex->outbuf);
250 a->aio_nbytes = smb_maxcnt;
251 a->aio_offset = startpos;
252 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
253 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
254 a->aio_sigevent.sigev_value.sival_int = aio_ex->mid;
256 if (SMB_VFS_AIO_READ(fsp,a) == -1) {
257 DEBUG(0,("schedule_aio_read_and_X: aio_read failed. "
258 "Error %s\n", strerror(errno) ));
259 delete_aio_ex(aio_ex);
260 return False;
263 DEBUG(10,("schedule_aio_read_and_X: scheduled aio_read for file %s, "
264 "offset %.0f, len = %u (mid = %u)\n",
265 fsp->fsp_name, (double)startpos, (unsigned int)smb_maxcnt,
266 (unsigned int)aio_ex->mid ));
268 srv_defer_sign_response(aio_ex->mid);
269 outstanding_aio_calls++;
270 return True;
273 /****************************************************************************
274 Set up an aio request from a SMBwriteX call.
275 *****************************************************************************/
277 BOOL schedule_aio_write_and_X(connection_struct *conn,
278 char *inbuf, char *outbuf,
279 int length, int len_outbuf,
280 files_struct *fsp, char *data,
281 SMB_OFF_T startpos,
282 size_t numtowrite)
284 struct aio_extra *aio_ex;
285 SMB_STRUCT_AIOCB *a;
286 size_t inbufsize, outbufsize;
287 size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
289 if (!min_aio_write_size || (numtowrite < min_aio_write_size)) {
290 /* Too small a write for aio request. */
291 DEBUG(10,("schedule_aio_write_and_X: write size (%u) too "
292 "small for minimum aio_write of %u\n",
293 (unsigned int)numtowrite,
294 (unsigned int)min_aio_write_size ));
295 return False;
298 /* Only do this on non-chained and non-chaining reads not using the
299 * write cache. */
300 if (chain_size !=0 || (CVAL(inbuf,smb_vwv0) != 0xFF)
301 || (lp_write_cache_size(SNUM(conn)) != 0) ) {
302 return False;
305 if (outstanding_aio_calls >= AIO_PENDING_SIZE) {
306 DEBUG(3,("schedule_aio_write_and_X: Already have %d aio "
307 "activities outstanding.\n",
308 outstanding_aio_calls ));
309 DEBUG(10,("schedule_aio_write_and_X: failed to schedule "
310 "aio_write for file %s, offset %.0f, len = %u "
311 "(mid = %u)\n",
312 fsp->fsp_name, (double)startpos,
313 (unsigned int)numtowrite,
314 (unsigned int)SVAL(inbuf,smb_mid) ));
315 return False;
318 inbufsize = smb_len(inbuf) + 4;
319 outbufsize = smb_len(outbuf) + 4;
320 if (!(aio_ex = create_aio_ex_write(fsp, inbufsize, outbufsize,
321 SVAL(inbuf,smb_mid)))) {
322 DEBUG(0,("schedule_aio_write_and_X: malloc fail.\n"));
323 return False;
326 /* Copy the SMB header already setup in outbuf. */
327 memcpy(aio_ex->inbuf, inbuf, inbufsize);
329 /* Copy the SMB header already setup in outbuf. */
330 memcpy(aio_ex->outbuf, outbuf, outbufsize);
331 SCVAL(aio_ex->outbuf,smb_vwv0,0xFF); /* Never a chained reply. */
333 a = &aio_ex->acb;
335 /* Now set up the aio record for the write call. */
337 a->aio_fildes = fsp->fh->fd;
338 a->aio_buf = aio_ex->inbuf + (PTR_DIFF(data, inbuf));
339 a->aio_nbytes = numtowrite;
340 a->aio_offset = startpos;
341 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
342 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
343 a->aio_sigevent.sigev_value.sival_int = aio_ex->mid;
345 if (SMB_VFS_AIO_WRITE(fsp,a) == -1) {
346 DEBUG(3,("schedule_aio_wrote_and_X: aio_write failed. "
347 "Error %s\n", strerror(errno) ));
348 delete_aio_ex(aio_ex);
349 return False;
352 srv_defer_sign_response(aio_ex->mid);
353 outstanding_aio_calls++;
355 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write for file "
356 "%s, offset %.0f, len = %u (mid = %u) "
357 "outstanding_aio_calls = %d\n",
358 fsp->fsp_name, (double)startpos, (unsigned int)numtowrite,
359 (unsigned int)aio_ex->mid, outstanding_aio_calls ));
361 return True;
365 /****************************************************************************
366 Complete the read and return the data or error back to the client.
367 Returns errno or zero if all ok.
368 *****************************************************************************/
370 static int handle_aio_read_complete(struct aio_extra *aio_ex)
372 int ret = 0;
373 int outsize;
374 char *outbuf = aio_ex->outbuf;
375 char *data = smb_buf(outbuf);
376 ssize_t nread = SMB_VFS_AIO_RETURN(aio_ex->fsp,&aio_ex->acb);
378 if (nread < 0) {
379 /* We're relying here on the fact that if the fd is
380 closed then the aio will complete and aio_return
381 will return an error. Hopefully this is
382 true.... JRA. */
384 /* If errno is ECANCELED then don't return anything to the
385 * client. */
386 if (errno == ECANCELED) {
387 srv_cancel_sign_response(aio_ex->mid);
388 return 0;
391 DEBUG( 3,( "handle_aio_read_complete: file %s nread == -1. "
392 "Error = %s\n",
393 aio_ex->fsp->fsp_name, strerror(errno) ));
395 outsize = (UNIXERROR(ERRDOS,ERRnoaccess));
396 ret = errno;
397 } else {
398 outsize = set_message(outbuf,12,nread,False);
399 SSVAL(outbuf,smb_vwv2,0xFFFF); /* Remaining - must be * -1. */
400 SSVAL(outbuf,smb_vwv5,nread);
401 SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
402 SSVAL(outbuf,smb_vwv7,((nread >> 16) & 1));
403 SSVAL(smb_buf(outbuf),-2,nread);
405 DEBUG( 3, ( "handle_aio_read_complete file %s max=%d "
406 "nread=%d\n",
407 aio_ex->fsp->fsp_name,
408 aio_ex->acb.aio_nbytes, (int)nread ) );
411 smb_setlen(outbuf,outsize - 4);
412 show_msg(outbuf);
413 if (!send_smb(smbd_server_fd(),outbuf)) {
414 exit_server_cleanly("handle_aio_read_complete: send_smb "
415 "failed.");
418 DEBUG(10,("handle_aio_read_complete: scheduled aio_read completed "
419 "for file %s, offset %.0f, len = %u\n",
420 aio_ex->fsp->fsp_name, (double)aio_ex->acb.aio_offset,
421 (unsigned int)nread ));
423 return ret;
426 /****************************************************************************
427 Complete the write and return the data or error back to the client.
428 Returns errno or zero if all ok.
429 *****************************************************************************/
431 static int handle_aio_write_complete(struct aio_extra *aio_ex)
433 int ret = 0;
434 files_struct *fsp = aio_ex->fsp;
435 char *outbuf = aio_ex->outbuf;
436 ssize_t numtowrite = aio_ex->acb.aio_nbytes;
437 ssize_t nwritten = SMB_VFS_AIO_RETURN(fsp,&aio_ex->acb);
439 /* We don't need outsize or set_message here as we've already set the
440 fixed size length when we set up the aio call. */
442 if(nwritten == -1) {
443 DEBUG( 3,( "handle_aio_write: file %s wanted %u bytes. "
444 "nwritten == %d. Error = %s\n",
445 fsp->fsp_name, (unsigned int)numtowrite,
446 (int)nwritten, strerror(errno) ));
448 /* If errno is ECANCELED then don't return anything to the
449 * client. */
450 if (errno == ECANCELED) {
451 srv_cancel_sign_response(aio_ex->mid);
452 return 0;
455 UNIXERROR(ERRHRD,ERRdiskfull);
456 ret = errno;
457 } else {
458 BOOL write_through = BITSETW(aio_ex->inbuf+smb_vwv7,0);
460 SSVAL(outbuf,smb_vwv2,nwritten);
461 SSVAL(outbuf,smb_vwv4,(nwritten>>16)&1);
462 if (nwritten < (ssize_t)numtowrite) {
463 SCVAL(outbuf,smb_rcls,ERRHRD);
464 SSVAL(outbuf,smb_err,ERRdiskfull);
467 DEBUG(3,("handle_aio_write: fnum=%d num=%d wrote=%d\n",
468 fsp->fnum, (int)numtowrite, (int)nwritten));
469 sync_file(fsp->conn,fsp, write_through);
472 show_msg(outbuf);
473 if (!send_smb(smbd_server_fd(),outbuf)) {
474 exit_server_cleanly("handle_aio_write: send_smb failed.");
477 DEBUG(10,("handle_aio_write_complete: scheduled aio_write completed "
478 "for file %s, offset %.0f, requested %u, written = %u\n",
479 fsp->fsp_name, (double)aio_ex->acb.aio_offset,
480 (unsigned int)numtowrite, (unsigned int)nwritten ));
482 return ret;
485 /****************************************************************************
486 Handle any aio completion. Returns True if finished (and sets *perr if err
487 was non-zero), False if not.
488 *****************************************************************************/
490 static BOOL handle_aio_completed(struct aio_extra *aio_ex, int *perr)
492 int err;
494 /* Ensure the operation has really completed. */
495 if (SMB_VFS_AIO_ERROR(aio_ex->fsp, &aio_ex->acb) == EINPROGRESS) {
496 DEBUG(10,( "handle_aio_completed: operation mid %u still in "
497 "process for file %s\n",
498 aio_ex->mid, aio_ex->fsp->fsp_name ));
499 return False;
502 if (aio_ex->read_req) {
503 err = handle_aio_read_complete(aio_ex);
504 } else {
505 err = handle_aio_write_complete(aio_ex);
508 if (err) {
509 *perr = err; /* Only save non-zero errors. */
512 return True;
515 /****************************************************************************
516 Handle any aio completion inline.
517 Returns non-zero errno if fail or zero if all ok.
518 *****************************************************************************/
520 int process_aio_queue(void)
522 int i;
523 int ret = 0;
525 BlockSignals(True, RT_SIGNAL_AIO);
527 DEBUG(10,("process_aio_queue: signals_received = %d\n",
528 (int)signals_received));
529 DEBUG(10,("process_aio_queue: outstanding_aio_calls = %d\n",
530 outstanding_aio_calls));
532 if (!signals_received) {
533 BlockSignals(False, RT_SIGNAL_AIO);
534 return 0;
537 /* Drain all the complete aio_reads. */
538 for (i = 0; i < signals_received; i++) {
539 uint16 mid = aio_pending_array[i];
540 files_struct *fsp = NULL;
541 struct aio_extra *aio_ex = find_aio_ex(mid);
543 if (!aio_ex) {
544 DEBUG(3,("process_aio_queue: Can't find record to "
545 "match mid %u.\n", (unsigned int)mid));
546 srv_cancel_sign_response(mid);
547 continue;
550 fsp = aio_ex->fsp;
551 if (fsp == NULL) {
552 /* file was closed whilst I/O was outstanding. Just
553 * ignore. */
554 DEBUG( 3,( "process_aio_queue: file closed whilst "
555 "aio outstanding.\n"));
556 srv_cancel_sign_response(mid);
557 continue;
560 if (!handle_aio_completed(aio_ex, &ret)) {
561 continue;
564 delete_aio_ex(aio_ex);
567 outstanding_aio_calls -= signals_received;
568 signals_received = 0;
569 BlockSignals(False, RT_SIGNAL_AIO);
570 return ret;
573 /****************************************************************************
574 Cancel any outstanding aio requests. The client doesn't care about the reply.
575 *****************************************************************************/
577 void cancel_aio_by_fsp(files_struct *fsp)
579 struct aio_extra *aio_ex;
581 for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
582 if (aio_ex->fsp == fsp) {
583 /* Don't delete the aio_extra record as we may have
584 completed and don't yet know it. Just do the
585 aio_cancel call and return. */
586 SMB_VFS_AIO_CANCEL(fsp,fsp->fh->fd, &aio_ex->acb);
587 aio_ex->fsp = NULL; /* fsp will be closed when we
588 * return. */
593 #else
594 BOOL aio_finished(void)
596 return False;
599 void initialize_async_io_handler(void)
603 int process_aio_queue(void)
605 return False;
608 BOOL schedule_aio_read_and_X(connection_struct *conn,
609 char *inbuf, char *outbuf,
610 int length, int len_outbuf,
611 files_struct *fsp, SMB_OFF_T startpos,
612 size_t smb_maxcnt)
614 return False;
617 BOOL schedule_aio_write_and_X(connection_struct *conn,
618 char *inbuf, char *outbuf,
619 int length, int len_outbuf,
620 files_struct *fsp, char *data,
621 SMB_OFF_T startpos,
622 size_t numtowrite)
624 return False;
627 void cancel_aio_by_fsp(files_struct *fsp)
631 #endif