WHATSNEW: Start release notes for Samba 3.6.11.
[Samba.git] / source3 / smbd / aio.c
blobb0b90c0dfe54575eeead7ac51d81e831a5b4e7c6
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 3 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, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "smbd/smbd.h"
23 #include "smbd/globals.h"
24 #include "../lib/util/tevent_ntstatus.h"
26 #if defined(WITH_AIO)
28 /* The signal we'll use to signify aio done. */
29 #ifndef RT_SIGNAL_AIO
30 #define RT_SIGNAL_AIO (SIGRTMIN+3)
31 #endif
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
40 /****************************************************************************
41 The buffer we keep around whilst an aio request is in process.
42 *****************************************************************************/
44 struct aio_extra {
45 struct aio_extra *next, *prev;
46 SMB_STRUCT_AIOCB acb;
47 files_struct *fsp;
48 struct smb_request *smbreq;
49 DATA_BLOB outbuf;
50 struct lock_struct lock;
51 bool write_through;
52 int (*handle_completion)(struct aio_extra *ex, int errcode);
55 /****************************************************************************
56 Initialize the signal handler for aio read/write.
57 *****************************************************************************/
59 static void smbd_aio_signal_handler(struct tevent_context *ev_ctx,
60 struct tevent_signal *se,
61 int signum, int count,
62 void *_info, void *private_data)
64 siginfo_t *info = (siginfo_t *)_info;
65 struct aio_extra *aio_ex = (struct aio_extra *)
66 info->si_value.sival_ptr;
68 smbd_aio_complete_aio_ex(aio_ex);
69 TALLOC_FREE(aio_ex);
73 static bool initialize_async_io_handler(void)
75 static bool tried_signal_setup = false;
77 if (aio_signal_event) {
78 return true;
80 if (tried_signal_setup) {
81 return false;
83 tried_signal_setup = true;
85 aio_signal_event = tevent_add_signal(smbd_event_context(),
86 smbd_event_context(),
87 RT_SIGNAL_AIO, SA_SIGINFO,
88 smbd_aio_signal_handler,
89 NULL);
90 if (!aio_signal_event) {
91 DEBUG(10, ("Failed to setup RT_SIGNAL_AIO handler\n"));
92 return false;
95 /* tevent supports 100 signal with SA_SIGINFO */
96 aio_pending_size = 100;
97 return true;
100 static int handle_aio_read_complete(struct aio_extra *aio_ex, int errcode);
101 static int handle_aio_write_complete(struct aio_extra *aio_ex, int errcode);
102 static int handle_aio_smb2_read_complete(struct aio_extra *aio_ex, int errcode);
103 static int handle_aio_smb2_write_complete(struct aio_extra *aio_ex, int errcode);
105 static int aio_extra_destructor(struct aio_extra *aio_ex)
107 DLIST_REMOVE(aio_list_head, aio_ex);
108 return 0;
111 /****************************************************************************
112 Create the extended aio struct we must keep around for the lifetime
113 of the aio call.
114 *****************************************************************************/
116 static struct aio_extra *create_aio_extra(TALLOC_CTX *mem_ctx,
117 files_struct *fsp,
118 size_t buflen)
120 struct aio_extra *aio_ex = TALLOC_ZERO_P(mem_ctx, struct aio_extra);
122 if (!aio_ex) {
123 return NULL;
126 /* The output buffer stored in the aio_ex is the start of
127 the smb return buffer. The buffer used in the acb
128 is the start of the reply data portion of that buffer. */
130 if (buflen) {
131 aio_ex->outbuf = data_blob_talloc(aio_ex, NULL, buflen);
132 if (!aio_ex->outbuf.data) {
133 TALLOC_FREE(aio_ex);
134 return NULL;
137 DLIST_ADD(aio_list_head, aio_ex);
138 talloc_set_destructor(aio_ex, aio_extra_destructor);
139 aio_ex->fsp = fsp;
140 return aio_ex;
143 /****************************************************************************
144 Set up an aio request from a SMBreadX call.
145 *****************************************************************************/
147 NTSTATUS schedule_aio_read_and_X(connection_struct *conn,
148 struct smb_request *smbreq,
149 files_struct *fsp, SMB_OFF_T startpos,
150 size_t smb_maxcnt)
152 struct aio_extra *aio_ex;
153 SMB_STRUCT_AIOCB *a;
154 size_t bufsize;
155 size_t min_aio_read_size = lp_aio_read_size(SNUM(conn));
156 int ret;
158 /* Ensure aio is initialized. */
159 if (!initialize_async_io_handler()) {
160 return NT_STATUS_RETRY;
163 if (fsp->base_fsp != NULL) {
164 /* No AIO on streams yet */
165 DEBUG(10, ("AIO on streams not yet supported\n"));
166 return NT_STATUS_RETRY;
169 if ((!min_aio_read_size || (smb_maxcnt < min_aio_read_size))
170 && !SMB_VFS_AIO_FORCE(fsp)) {
171 /* Too small a read for aio request. */
172 DEBUG(10,("schedule_aio_read_and_X: read size (%u) too small "
173 "for minimum aio_read of %u\n",
174 (unsigned int)smb_maxcnt,
175 (unsigned int)min_aio_read_size ));
176 return NT_STATUS_RETRY;
179 /* Only do this on non-chained and non-chaining reads not using the
180 * write cache. */
181 if (req_is_in_chain(smbreq) || (lp_write_cache_size(SNUM(conn)) != 0)) {
182 return NT_STATUS_RETRY;
185 if (outstanding_aio_calls >= aio_pending_size) {
186 DEBUG(10,("schedule_aio_read_and_X: Already have %d aio "
187 "activities outstanding.\n",
188 outstanding_aio_calls ));
189 return NT_STATUS_RETRY;
192 /* The following is safe from integer wrap as we've already checked
193 smb_maxcnt is 128k or less. Wct is 12 for read replies */
195 bufsize = smb_size + 12 * 2 + smb_maxcnt;
197 if ((aio_ex = create_aio_extra(NULL, fsp, bufsize)) == NULL) {
198 DEBUG(10,("schedule_aio_read_and_X: malloc fail.\n"));
199 return NT_STATUS_NO_MEMORY;
201 aio_ex->handle_completion = handle_aio_read_complete;
203 construct_reply_common_req(smbreq, (char *)aio_ex->outbuf.data);
204 srv_set_message((char *)aio_ex->outbuf.data, 12, 0, True);
205 SCVAL(aio_ex->outbuf.data,smb_vwv0,0xFF); /* Never a chained reply. */
207 init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
208 (uint64_t)startpos, (uint64_t)smb_maxcnt, READ_LOCK,
209 &aio_ex->lock);
211 /* Take the lock until the AIO completes. */
212 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
213 TALLOC_FREE(aio_ex);
214 return NT_STATUS_FILE_LOCK_CONFLICT;
217 a = &aio_ex->acb;
219 /* Now set up the aio record for the read call. */
221 a->aio_fildes = fsp->fh->fd;
222 a->aio_buf = smb_buf(aio_ex->outbuf.data);
223 a->aio_nbytes = smb_maxcnt;
224 a->aio_offset = startpos;
225 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
226 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
227 a->aio_sigevent.sigev_value.sival_ptr = aio_ex;
229 ret = SMB_VFS_AIO_READ(fsp, a);
230 if (ret == -1) {
231 DEBUG(0,("schedule_aio_read_and_X: aio_read failed. "
232 "Error %s\n", strerror(errno) ));
233 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
234 TALLOC_FREE(aio_ex);
235 return NT_STATUS_RETRY;
238 outstanding_aio_calls++;
239 aio_ex->smbreq = talloc_move(aio_ex, &smbreq);
241 DEBUG(10,("schedule_aio_read_and_X: scheduled aio_read for file %s, "
242 "offset %.0f, len = %u (mid = %u)\n",
243 fsp_str_dbg(fsp), (double)startpos, (unsigned int)smb_maxcnt,
244 (unsigned int)aio_ex->smbreq->mid ));
246 return NT_STATUS_OK;
249 /****************************************************************************
250 Set up an aio request from a SMBwriteX call.
251 *****************************************************************************/
253 NTSTATUS schedule_aio_write_and_X(connection_struct *conn,
254 struct smb_request *smbreq,
255 files_struct *fsp, char *data,
256 SMB_OFF_T startpos,
257 size_t numtowrite)
259 struct aio_extra *aio_ex;
260 SMB_STRUCT_AIOCB *a;
261 size_t bufsize;
262 size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
263 int ret;
265 /* Ensure aio is initialized. */
266 if (!initialize_async_io_handler()) {
267 return NT_STATUS_RETRY;
270 if (fsp->base_fsp != NULL) {
271 /* No AIO on streams yet */
272 DEBUG(10, ("AIO on streams not yet supported\n"));
273 return NT_STATUS_RETRY;
276 if ((!min_aio_write_size || (numtowrite < min_aio_write_size))
277 && !SMB_VFS_AIO_FORCE(fsp)) {
278 /* Too small a write for aio request. */
279 DEBUG(10,("schedule_aio_write_and_X: write size (%u) too "
280 "small for minimum aio_write of %u\n",
281 (unsigned int)numtowrite,
282 (unsigned int)min_aio_write_size ));
283 return NT_STATUS_RETRY;
286 /* Only do this on non-chained and non-chaining writes not using the
287 * write cache. */
288 if (req_is_in_chain(smbreq) || (lp_write_cache_size(SNUM(conn)) != 0)) {
289 return NT_STATUS_RETRY;
292 if (outstanding_aio_calls >= aio_pending_size) {
293 DEBUG(3,("schedule_aio_write_and_X: Already have %d aio "
294 "activities outstanding.\n",
295 outstanding_aio_calls ));
296 DEBUG(10,("schedule_aio_write_and_X: failed to schedule "
297 "aio_write for file %s, offset %.0f, len = %u "
298 "(mid = %u)\n",
299 fsp_str_dbg(fsp), (double)startpos,
300 (unsigned int)numtowrite,
301 (unsigned int)smbreq->mid ));
302 return NT_STATUS_RETRY;
305 bufsize = smb_size + 6*2;
307 if (!(aio_ex = create_aio_extra(NULL, fsp, bufsize))) {
308 DEBUG(0,("schedule_aio_write_and_X: malloc fail.\n"));
309 return NT_STATUS_NO_MEMORY;
311 aio_ex->handle_completion = handle_aio_write_complete;
312 aio_ex->write_through = BITSETW(smbreq->vwv+7,0);
314 construct_reply_common_req(smbreq, (char *)aio_ex->outbuf.data);
315 srv_set_message((char *)aio_ex->outbuf.data, 6, 0, True);
316 SCVAL(aio_ex->outbuf.data,smb_vwv0,0xFF); /* Never a chained reply. */
318 init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
319 (uint64_t)startpos, (uint64_t)numtowrite, WRITE_LOCK,
320 &aio_ex->lock);
322 /* Take the lock until the AIO completes. */
323 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
324 TALLOC_FREE(aio_ex);
325 return NT_STATUS_FILE_LOCK_CONFLICT;
328 a = &aio_ex->acb;
330 /* Now set up the aio record for the write call. */
332 a->aio_fildes = fsp->fh->fd;
333 a->aio_buf = data;
334 a->aio_nbytes = numtowrite;
335 a->aio_offset = startpos;
336 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
337 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
338 a->aio_sigevent.sigev_value.sival_ptr = aio_ex;
340 ret = SMB_VFS_AIO_WRITE(fsp, a);
341 if (ret == -1) {
342 DEBUG(3,("schedule_aio_wrote_and_X: aio_write failed. "
343 "Error %s\n", strerror(errno) ));
344 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
345 TALLOC_FREE(aio_ex);
346 return NT_STATUS_RETRY;
349 outstanding_aio_calls++;
350 aio_ex->smbreq = talloc_move(aio_ex, &smbreq);
352 /* This should actually be improved to span the write. */
353 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WRITE);
354 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WRITE);
356 if (!aio_ex->write_through && !lp_syncalways(SNUM(fsp->conn))
357 && fsp->aio_write_behind) {
358 /* Lie to the client and immediately claim we finished the
359 * write. */
360 SSVAL(aio_ex->outbuf.data,smb_vwv2,numtowrite);
361 SSVAL(aio_ex->outbuf.data,smb_vwv4,(numtowrite>>16)&1);
362 show_msg((char *)aio_ex->outbuf.data);
363 if (!srv_send_smb(aio_ex->smbreq->sconn,
364 (char *)aio_ex->outbuf.data,
365 true, aio_ex->smbreq->seqnum+1,
366 IS_CONN_ENCRYPTED(fsp->conn),
367 &aio_ex->smbreq->pcd)) {
368 exit_server_cleanly("schedule_aio_write_and_X: "
369 "srv_send_smb failed.");
371 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write "
372 "behind for file %s\n", fsp_str_dbg(fsp)));
375 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write for file "
376 "%s, offset %.0f, len = %u (mid = %u) "
377 "outstanding_aio_calls = %d\n",
378 fsp_str_dbg(fsp), (double)startpos, (unsigned int)numtowrite,
379 (unsigned int)aio_ex->smbreq->mid, outstanding_aio_calls ));
381 return NT_STATUS_OK;
384 /****************************************************************************
385 Set up an aio request from a SMB2 read call.
386 *****************************************************************************/
388 NTSTATUS schedule_smb2_aio_read(connection_struct *conn,
389 struct smb_request *smbreq,
390 files_struct *fsp,
391 TALLOC_CTX *ctx,
392 DATA_BLOB *preadbuf,
393 SMB_OFF_T startpos,
394 size_t smb_maxcnt)
396 struct aio_extra *aio_ex;
397 SMB_STRUCT_AIOCB *a;
398 size_t min_aio_read_size = lp_aio_read_size(SNUM(conn));
399 int ret;
401 /* Ensure aio is initialized. */
402 if (!initialize_async_io_handler()) {
403 return NT_STATUS_RETRY;
406 if (fsp->base_fsp != NULL) {
407 /* No AIO on streams yet */
408 DEBUG(10, ("AIO on streams not yet supported\n"));
409 return NT_STATUS_RETRY;
412 if ((!min_aio_read_size || (smb_maxcnt < min_aio_read_size))
413 && !SMB_VFS_AIO_FORCE(fsp)) {
414 /* Too small a read for aio request. */
415 DEBUG(10,("smb2: read size (%u) too small "
416 "for minimum aio_read of %u\n",
417 (unsigned int)smb_maxcnt,
418 (unsigned int)min_aio_read_size ));
419 return NT_STATUS_RETRY;
422 /* Only do this on reads not using the write cache. */
423 if (lp_write_cache_size(SNUM(conn)) != 0) {
424 return NT_STATUS_RETRY;
427 if (outstanding_aio_calls >= aio_pending_size) {
428 DEBUG(10,("smb2: Already have %d aio "
429 "activities outstanding.\n",
430 outstanding_aio_calls ));
431 return NT_STATUS_RETRY;
434 /* Create the out buffer. */
435 *preadbuf = data_blob_talloc(ctx, NULL, smb_maxcnt);
436 if (preadbuf->data == NULL) {
437 return NT_STATUS_NO_MEMORY;
440 if (!(aio_ex = create_aio_extra(smbreq->smb2req, fsp, 0))) {
441 return NT_STATUS_NO_MEMORY;
443 aio_ex->handle_completion = handle_aio_smb2_read_complete;
445 init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
446 (uint64_t)startpos, (uint64_t)smb_maxcnt, READ_LOCK,
447 &aio_ex->lock);
449 /* Take the lock until the AIO completes. */
450 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
451 TALLOC_FREE(aio_ex);
452 return NT_STATUS_FILE_LOCK_CONFLICT;
455 a = &aio_ex->acb;
457 /* Now set up the aio record for the read call. */
459 a->aio_fildes = fsp->fh->fd;
460 a->aio_buf = preadbuf->data;
461 a->aio_nbytes = smb_maxcnt;
462 a->aio_offset = startpos;
463 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
464 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
465 a->aio_sigevent.sigev_value.sival_ptr = aio_ex;
467 ret = SMB_VFS_AIO_READ(fsp, a);
468 if (ret == -1) {
469 DEBUG(0,("smb2: aio_read failed. "
470 "Error %s\n", strerror(errno) ));
471 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
472 TALLOC_FREE(aio_ex);
473 return NT_STATUS_RETRY;
476 outstanding_aio_calls++;
477 /* We don't need talloc_move here as both aio_ex and
478 * smbreq are children of smbreq->smb2req. */
479 aio_ex->smbreq = smbreq;
481 DEBUG(10,("smb2: scheduled aio_read for file %s, "
482 "offset %.0f, len = %u (mid = %u)\n",
483 fsp_str_dbg(fsp), (double)startpos, (unsigned int)smb_maxcnt,
484 (unsigned int)aio_ex->smbreq->mid ));
486 return NT_STATUS_OK;
489 /****************************************************************************
490 Set up an aio request from a SMB2write call.
491 *****************************************************************************/
493 NTSTATUS schedule_aio_smb2_write(connection_struct *conn,
494 struct smb_request *smbreq,
495 files_struct *fsp,
496 uint64_t in_offset,
497 DATA_BLOB in_data,
498 bool write_through)
500 struct aio_extra *aio_ex = NULL;
501 SMB_STRUCT_AIOCB *a = NULL;
502 size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
503 int ret;
505 /* Ensure aio is initialized. */
506 if (!initialize_async_io_handler()) {
507 return NT_STATUS_RETRY;
510 if (fsp->base_fsp != NULL) {
511 /* No AIO on streams yet */
512 DEBUG(10, ("AIO on streams not yet supported\n"));
513 return NT_STATUS_RETRY;
516 if ((!min_aio_write_size || (in_data.length < min_aio_write_size))
517 && !SMB_VFS_AIO_FORCE(fsp)) {
518 /* Too small a write for aio request. */
519 DEBUG(10,("smb2: write size (%u) too "
520 "small for minimum aio_write of %u\n",
521 (unsigned int)in_data.length,
522 (unsigned int)min_aio_write_size ));
523 return NT_STATUS_RETRY;
526 /* Only do this on writes not using the write cache. */
527 if (lp_write_cache_size(SNUM(conn)) != 0) {
528 return NT_STATUS_RETRY;
531 if (outstanding_aio_calls >= aio_pending_size) {
532 DEBUG(3,("smb2: Already have %d aio "
533 "activities outstanding.\n",
534 outstanding_aio_calls ));
535 return NT_STATUS_RETRY;
538 if (!(aio_ex = create_aio_extra(smbreq->smb2req, fsp, 0))) {
539 return NT_STATUS_NO_MEMORY;
542 aio_ex->handle_completion = handle_aio_smb2_write_complete;
543 aio_ex->write_through = write_through;
545 init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
546 in_offset, (uint64_t)in_data.length, WRITE_LOCK,
547 &aio_ex->lock);
549 /* Take the lock until the AIO completes. */
550 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
551 TALLOC_FREE(aio_ex);
552 return NT_STATUS_FILE_LOCK_CONFLICT;
555 a = &aio_ex->acb;
557 /* Now set up the aio record for the write call. */
559 a->aio_fildes = fsp->fh->fd;
560 a->aio_buf = in_data.data;
561 a->aio_nbytes = in_data.length;
562 a->aio_offset = in_offset;
563 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
564 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
565 a->aio_sigevent.sigev_value.sival_ptr = aio_ex;
567 ret = SMB_VFS_AIO_WRITE(fsp, a);
568 if (ret == -1) {
569 DEBUG(3,("smb2: aio_write failed. "
570 "Error %s\n", strerror(errno) ));
571 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
572 TALLOC_FREE(aio_ex);
573 return NT_STATUS_RETRY;
576 outstanding_aio_calls++;
577 /* We don't need talloc_move here as both aio_ex and
578 * smbreq are children of smbreq->smb2req. */
579 aio_ex->smbreq = smbreq;
581 /* This should actually be improved to span the write. */
582 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WRITE);
583 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WRITE);
586 * We don't want to do write behind due to ownership
587 * issues of the request structs. Maybe add it if I
588 * figure those out. JRA.
591 DEBUG(10,("smb2: scheduled aio_write for file "
592 "%s, offset %.0f, len = %u (mid = %u) "
593 "outstanding_aio_calls = %d\n",
594 fsp_str_dbg(fsp),
595 (double)in_offset,
596 (unsigned int)in_data.length,
597 (unsigned int)aio_ex->smbreq->mid,
598 outstanding_aio_calls ));
600 return NT_STATUS_OK;
603 /****************************************************************************
604 Complete the read and return the data or error back to the client.
605 Returns errno or zero if all ok.
606 *****************************************************************************/
608 static int handle_aio_read_complete(struct aio_extra *aio_ex, int errcode)
610 int outsize;
611 char *outbuf = (char *)aio_ex->outbuf.data;
612 char *data = smb_buf(outbuf);
613 ssize_t nread = SMB_VFS_AIO_RETURN(aio_ex->fsp,&aio_ex->acb);
615 if (nread < 0) {
616 /* We're relying here on the fact that if the fd is
617 closed then the aio will complete and aio_return
618 will return an error. Hopefully this is
619 true.... JRA. */
621 DEBUG( 3,( "handle_aio_read_complete: file %s nread == %d. "
622 "Error = %s\n",
623 fsp_str_dbg(aio_ex->fsp), (int)nread, strerror(errcode)));
625 ERROR_NT(map_nt_error_from_unix(errcode));
626 outsize = srv_set_message(outbuf,0,0,true);
627 } else {
628 outsize = srv_set_message(outbuf,12,nread,False);
629 SSVAL(outbuf,smb_vwv2,0xFFFF); /* Remaining - must be * -1. */
630 SSVAL(outbuf,smb_vwv5,nread);
631 SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
632 SSVAL(outbuf,smb_vwv7,((nread >> 16) & 1));
633 SSVAL(smb_buf(outbuf),-2,nread);
635 aio_ex->fsp->fh->pos = aio_ex->acb.aio_offset + nread;
636 aio_ex->fsp->fh->position_information = aio_ex->fsp->fh->pos;
638 DEBUG( 3, ( "handle_aio_read_complete file %s max=%d "
639 "nread=%d\n",
640 fsp_str_dbg(aio_ex->fsp),
641 (int)aio_ex->acb.aio_nbytes, (int)nread ) );
644 smb_setlen(outbuf,outsize - 4);
645 show_msg(outbuf);
646 if (!srv_send_smb(aio_ex->smbreq->sconn, outbuf,
647 true, aio_ex->smbreq->seqnum+1,
648 IS_CONN_ENCRYPTED(aio_ex->fsp->conn), NULL)) {
649 exit_server_cleanly("handle_aio_read_complete: srv_send_smb "
650 "failed.");
653 DEBUG(10,("handle_aio_read_complete: scheduled aio_read completed "
654 "for file %s, offset %.0f, len = %u\n",
655 fsp_str_dbg(aio_ex->fsp), (double)aio_ex->acb.aio_offset,
656 (unsigned int)nread ));
658 return errcode;
661 /****************************************************************************
662 Complete the write and return the data or error back to the client.
663 Returns error code or zero if all ok.
664 *****************************************************************************/
666 static int handle_aio_write_complete(struct aio_extra *aio_ex, int errcode)
668 files_struct *fsp = aio_ex->fsp;
669 char *outbuf = (char *)aio_ex->outbuf.data;
670 ssize_t numtowrite = aio_ex->acb.aio_nbytes;
671 ssize_t nwritten = SMB_VFS_AIO_RETURN(fsp,&aio_ex->acb);
673 if (fsp->aio_write_behind) {
674 if (nwritten != numtowrite) {
675 if (nwritten == -1) {
676 DEBUG(5,("handle_aio_write_complete: "
677 "aio_write_behind failed ! File %s "
678 "is corrupt ! Error %s\n",
679 fsp_str_dbg(fsp), strerror(errcode)));
680 } else {
681 DEBUG(0,("handle_aio_write_complete: "
682 "aio_write_behind failed ! File %s "
683 "is corrupt ! Wanted %u bytes but "
684 "only wrote %d\n", fsp_str_dbg(fsp),
685 (unsigned int)numtowrite,
686 (int)nwritten ));
687 errcode = EIO;
689 } else {
690 DEBUG(10,("handle_aio_write_complete: "
691 "aio_write_behind completed for file %s\n",
692 fsp_str_dbg(fsp)));
694 /* TODO: should no return 0 in case of an error !!! */
695 return 0;
698 /* We don't need outsize or set_message here as we've already set the
699 fixed size length when we set up the aio call. */
701 if(nwritten == -1) {
702 DEBUG( 3,( "handle_aio_write: file %s wanted %u bytes. "
703 "nwritten == %d. Error = %s\n",
704 fsp_str_dbg(fsp), (unsigned int)numtowrite,
705 (int)nwritten, strerror(errcode) ));
707 ERROR_NT(map_nt_error_from_unix(errcode));
708 srv_set_message(outbuf,0,0,true);
709 } else {
710 NTSTATUS status;
712 SSVAL(outbuf,smb_vwv2,nwritten);
713 SSVAL(outbuf,smb_vwv4,(nwritten>>16)&1);
714 if (nwritten < (ssize_t)numtowrite) {
715 SCVAL(outbuf,smb_rcls,ERRHRD);
716 SSVAL(outbuf,smb_err,ERRdiskfull);
719 DEBUG(3,("handle_aio_write: fnum=%d num=%d wrote=%d\n",
720 fsp->fnum, (int)numtowrite, (int)nwritten));
721 status = sync_file(fsp->conn,fsp, aio_ex->write_through);
722 if (!NT_STATUS_IS_OK(status)) {
723 errcode = errno;
724 ERROR_BOTH(map_nt_error_from_unix(errcode),
725 ERRHRD, ERRdiskfull);
726 srv_set_message(outbuf,0,0,true);
727 DEBUG(5,("handle_aio_write: sync_file for %s returned %s\n",
728 fsp_str_dbg(fsp), nt_errstr(status)));
731 aio_ex->fsp->fh->pos = aio_ex->acb.aio_offset + nwritten;
734 show_msg(outbuf);
735 if (!srv_send_smb(aio_ex->smbreq->sconn, outbuf,
736 true, aio_ex->smbreq->seqnum+1,
737 IS_CONN_ENCRYPTED(fsp->conn),
738 NULL)) {
739 exit_server_cleanly("handle_aio_write_complete: "
740 "srv_send_smb failed.");
743 DEBUG(10,("handle_aio_write_complete: scheduled aio_write completed "
744 "for file %s, offset %.0f, requested %u, written = %u\n",
745 fsp_str_dbg(fsp), (double)aio_ex->acb.aio_offset,
746 (unsigned int)numtowrite, (unsigned int)nwritten ));
748 return errcode;
751 /****************************************************************************
752 Complete the read and return the data or error back to the client.
753 Returns errno or zero if all ok.
754 *****************************************************************************/
756 static int handle_aio_smb2_read_complete(struct aio_extra *aio_ex, int errcode)
758 NTSTATUS status;
759 struct tevent_req *subreq = aio_ex->smbreq->smb2req->subreq;
760 ssize_t nread = SMB_VFS_AIO_RETURN(aio_ex->fsp,&aio_ex->acb);
762 /* Common error or success code processing for async or sync
763 read returns. */
765 status = smb2_read_complete(subreq, nread, errcode);
767 if (nread > 0) {
768 aio_ex->fsp->fh->pos = aio_ex->acb.aio_offset + nread;
769 aio_ex->fsp->fh->position_information = aio_ex->fsp->fh->pos;
772 DEBUG(10,("smb2: scheduled aio_read completed "
773 "for file %s, offset %.0f, len = %u "
774 "(errcode = %d, NTSTATUS = %s)\n",
775 fsp_str_dbg(aio_ex->fsp),
776 (double)aio_ex->acb.aio_offset,
777 (unsigned int)nread,
778 errcode,
779 nt_errstr(status) ));
781 if (!NT_STATUS_IS_OK(status)) {
782 tevent_req_nterror(subreq, status);
783 return errcode;
786 tevent_req_done(subreq);
787 return errcode;
790 /****************************************************************************
791 Complete the SMB2 write and return the data or error back to the client.
792 Returns error code or zero if all ok.
793 *****************************************************************************/
795 static int handle_aio_smb2_write_complete(struct aio_extra *aio_ex, int errcode)
797 files_struct *fsp = aio_ex->fsp;
798 ssize_t numtowrite = aio_ex->acb.aio_nbytes;
799 ssize_t nwritten = SMB_VFS_AIO_RETURN(fsp,&aio_ex->acb);
800 struct tevent_req *subreq = aio_ex->smbreq->smb2req->subreq;
801 NTSTATUS status;
803 status = smb2_write_complete(subreq, nwritten, errcode);
805 DEBUG(10,("smb2: scheduled aio_write completed "
806 "for file %s, offset %.0f, requested %u, "
807 "written = %u (errcode = %d, NTSTATUS = %s)\n",
808 fsp_str_dbg(fsp),
809 (double)aio_ex->acb.aio_offset,
810 (unsigned int)numtowrite,
811 (unsigned int)nwritten,
812 errcode,
813 nt_errstr(status) ));
815 if (!NT_STATUS_IS_OK(status)) {
816 tevent_req_nterror(subreq, status);
817 return errcode;
820 tevent_req_done(subreq);
821 return errcode;
824 /****************************************************************************
825 Handle any aio completion. Returns True if finished (and sets *perr if err
826 was non-zero), False if not.
827 *****************************************************************************/
829 static bool handle_aio_completed(struct aio_extra *aio_ex, int *perr)
831 files_struct *fsp = NULL;
832 int err;
834 if(!aio_ex) {
835 DEBUG(3, ("handle_aio_completed: Non-existing aio_ex passed\n"));
836 return false;
839 fsp = aio_ex->fsp;
841 /* Ensure the operation has really completed. */
842 err = SMB_VFS_AIO_ERROR(fsp, &aio_ex->acb);
843 if (err == EINPROGRESS) {
844 DEBUG(10,( "handle_aio_completed: operation mid %llu still in "
845 "process for file %s\n",
846 (unsigned long long)aio_ex->smbreq->mid,
847 fsp_str_dbg(aio_ex->fsp)));
848 return False;
851 /* Unlock now we're done. */
852 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &aio_ex->lock);
854 if (err == ECANCELED) {
855 /* If error is ECANCELED then don't return anything to the
856 * client. */
857 DEBUG(10,( "handle_aio_completed: operation mid %llu"
858 " canceled\n",
859 (unsigned long long)aio_ex->smbreq->mid));
860 return True;
863 err = aio_ex->handle_completion(aio_ex, err);
864 if (err) {
865 *perr = err; /* Only save non-zero errors. */
868 return True;
871 /****************************************************************************
872 Handle any aio completion inline.
873 *****************************************************************************/
875 void smbd_aio_complete_aio_ex(struct aio_extra *aio_ex)
877 files_struct *fsp = NULL;
878 int ret = 0;
880 outstanding_aio_calls--;
882 DEBUG(10,("smbd_aio_complete_mid: mid[%llu]\n",
883 (unsigned long long)aio_ex->smbreq->mid));
885 fsp = aio_ex->fsp;
886 if (fsp == NULL) {
887 /* file was closed whilst I/O was outstanding. Just
888 * ignore. */
889 DEBUG( 3,( "smbd_aio_complete_mid: file closed whilst "
890 "aio outstanding (mid[%llu]).\n",
891 (unsigned long long)aio_ex->smbreq->mid));
892 return;
895 if (!handle_aio_completed(aio_ex, &ret)) {
896 return;
900 /****************************************************************************
901 We're doing write behind and the client closed the file. Wait up to 30
902 seconds (my arbitrary choice) for the aio to complete. Return 0 if all writes
903 completed, errno to return if not.
904 *****************************************************************************/
906 #define SMB_TIME_FOR_AIO_COMPLETE_WAIT 29
908 int wait_for_aio_completion(files_struct *fsp)
910 struct aio_extra *aio_ex;
911 const SMB_STRUCT_AIOCB **aiocb_list;
912 int aio_completion_count = 0;
913 time_t start_time = time_mono(NULL);
914 int seconds_left;
916 for (seconds_left = SMB_TIME_FOR_AIO_COMPLETE_WAIT;
917 seconds_left >= 0;) {
918 int err = 0;
919 int i;
920 struct timespec ts;
922 aio_completion_count = 0;
923 for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
924 if (aio_ex->fsp == fsp) {
925 aio_completion_count++;
929 if (!aio_completion_count) {
930 return 0;
933 DEBUG(3,("wait_for_aio_completion: waiting for %d aio events "
934 "to complete.\n", aio_completion_count ));
936 aiocb_list = SMB_MALLOC_ARRAY(const SMB_STRUCT_AIOCB *,
937 aio_completion_count);
938 if (!aiocb_list) {
939 return ENOMEM;
942 for( i = 0, aio_ex = aio_list_head;
943 aio_ex;
944 aio_ex = aio_ex->next) {
945 if (aio_ex->fsp == fsp) {
946 aiocb_list[i++] = &aio_ex->acb;
950 /* Now wait up to seconds_left for completion. */
951 ts.tv_sec = seconds_left;
952 ts.tv_nsec = 0;
954 DEBUG(10,("wait_for_aio_completion: %d events, doing a wait "
955 "of %d seconds.\n",
956 aio_completion_count, seconds_left ));
958 err = SMB_VFS_AIO_SUSPEND(fsp, aiocb_list,
959 aio_completion_count, &ts);
961 DEBUG(10,("wait_for_aio_completion: returned err = %d, "
962 "errno = %s\n", err, strerror(errno) ));
964 if (err == -1 && errno == EAGAIN) {
965 DEBUG(0,("wait_for_aio_completion: aio_suspend timed "
966 "out waiting for %d events after a wait of "
967 "%d seconds\n", aio_completion_count,
968 seconds_left));
969 /* Timeout. */
970 cancel_aio_by_fsp(fsp);
971 SAFE_FREE(aiocb_list);
972 return EIO;
975 /* One or more events might have completed - process them if
976 * so. */
977 for( i = 0; i < aio_completion_count; i++) {
978 aio_ex = (struct aio_extra *)aiocb_list[i]->aio_sigevent.sigev_value.sival_ptr;
980 if (!handle_aio_completed(aio_ex, &err)) {
981 continue;
983 TALLOC_FREE(aio_ex);
986 SAFE_FREE(aiocb_list);
987 seconds_left = SMB_TIME_FOR_AIO_COMPLETE_WAIT
988 - (time_mono(NULL) - start_time);
991 /* We timed out - we don't know why. Return ret if already an error,
992 * else EIO. */
993 DEBUG(10,("wait_for_aio_completion: aio_suspend timed out waiting "
994 "for %d events\n",
995 aio_completion_count));
997 return EIO;
1000 /****************************************************************************
1001 Cancel any outstanding aio requests. The client doesn't care about the reply.
1002 *****************************************************************************/
1004 void cancel_aio_by_fsp(files_struct *fsp)
1006 struct aio_extra *aio_ex;
1008 for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
1009 if (aio_ex->fsp == fsp) {
1010 /* Unlock now we're done. */
1011 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &aio_ex->lock);
1013 /* Don't delete the aio_extra record as we may have
1014 completed and don't yet know it. Just do the
1015 aio_cancel call and return. */
1016 SMB_VFS_AIO_CANCEL(fsp, &aio_ex->acb);
1017 aio_ex->fsp = NULL; /* fsp will be closed when we
1018 * return. */
1023 #else
1024 NTSTATUS schedule_aio_read_and_X(connection_struct *conn,
1025 struct smb_request *smbreq,
1026 files_struct *fsp, SMB_OFF_T startpos,
1027 size_t smb_maxcnt)
1029 return NT_STATUS_RETRY;
1032 NTSTATUS schedule_aio_write_and_X(connection_struct *conn,
1033 struct smb_request *smbreq,
1034 files_struct *fsp, char *data,
1035 SMB_OFF_T startpos,
1036 size_t numtowrite)
1038 return NT_STATUS_RETRY;
1041 NTSTATUS schedule_smb2_aio_read(connection_struct *conn,
1042 struct smb_request *smbreq,
1043 files_struct *fsp,
1044 TALLOC_CTX *ctx,
1045 DATA_BLOB *preadbuf,
1046 SMB_OFF_T startpos,
1047 size_t smb_maxcnt)
1049 return NT_STATUS_RETRY;
1052 NTSTATUS schedule_aio_smb2_write(connection_struct *conn,
1053 struct smb_request *smbreq,
1054 files_struct *fsp,
1055 uint64_t in_offset,
1056 DATA_BLOB in_data,
1057 bool write_through)
1059 return NT_STATUS_RETRY;
1062 void cancel_aio_by_fsp(files_struct *fsp)
1066 int wait_for_aio_completion(files_struct *fsp)
1068 return 0;
1071 void smbd_aio_complete_mid(uint64_t mid);
1073 #endif