s4-smbtorture: add ndr test for nbt_netlogon_packet to avoid future regressions.
[Samba/gebeck_regimport.git] / source3 / smbd / aio.c
blob07b8388fba0692600e4c07c6c8d4e915d30d9e95
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 bool pass_cancel;
53 int (*handle_completion)(struct aio_extra *ex, int errcode);
56 /****************************************************************************
57 Initialize the signal handler for aio read/write.
58 *****************************************************************************/
60 static void smbd_aio_signal_handler(struct tevent_context *ev_ctx,
61 struct tevent_signal *se,
62 int signum, int count,
63 void *_info, void *private_data)
65 siginfo_t *info = (siginfo_t *)_info;
66 struct aio_extra *aio_ex = (struct aio_extra *)
67 info->si_value.sival_ptr;
69 smbd_aio_complete_aio_ex(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(server_event_context(),
86 server_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(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, const 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 = discard_const_p(char, 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 bool cancel_smb2_aio(struct smb_request *smbreq)
386 struct smbd_smb2_request *smb2req = smbreq->smb2req;
387 struct aio_extra *aio_ex = NULL;
388 int ret;
390 if (smb2req) {
391 aio_ex = talloc_get_type(smbreq->async_priv,
392 struct aio_extra);
395 if (aio_ex == NULL) {
396 return false;
399 if (aio_ex->fsp == NULL) {
400 return false;
403 ret = SMB_VFS_AIO_CANCEL(aio_ex->fsp, &aio_ex->acb);
404 if (ret != AIO_CANCELED) {
405 return false;
408 return true;
411 /****************************************************************************
412 Set up an aio request from a SMB2 read call.
413 *****************************************************************************/
415 NTSTATUS schedule_smb2_aio_read(connection_struct *conn,
416 struct smb_request *smbreq,
417 files_struct *fsp,
418 TALLOC_CTX *ctx,
419 DATA_BLOB *preadbuf,
420 SMB_OFF_T startpos,
421 size_t smb_maxcnt)
423 struct aio_extra *aio_ex;
424 SMB_STRUCT_AIOCB *a;
425 size_t min_aio_read_size = lp_aio_read_size(SNUM(conn));
426 int ret;
428 /* Ensure aio is initialized. */
429 if (!initialize_async_io_handler()) {
430 return NT_STATUS_RETRY;
433 if (fsp->base_fsp != NULL) {
434 /* No AIO on streams yet */
435 DEBUG(10, ("AIO on streams not yet supported\n"));
436 return NT_STATUS_RETRY;
439 if ((!min_aio_read_size || (smb_maxcnt < min_aio_read_size))
440 && !SMB_VFS_AIO_FORCE(fsp)) {
441 /* Too small a read for aio request. */
442 DEBUG(10,("smb2: read size (%u) too small "
443 "for minimum aio_read of %u\n",
444 (unsigned int)smb_maxcnt,
445 (unsigned int)min_aio_read_size ));
446 return NT_STATUS_RETRY;
449 /* Only do this on reads not using the write cache. */
450 if (lp_write_cache_size(SNUM(conn)) != 0) {
451 return NT_STATUS_RETRY;
454 if (outstanding_aio_calls >= aio_pending_size) {
455 DEBUG(10,("smb2: Already have %d aio "
456 "activities outstanding.\n",
457 outstanding_aio_calls ));
458 return NT_STATUS_RETRY;
461 /* Create the out buffer. */
462 *preadbuf = data_blob_talloc(ctx, NULL, smb_maxcnt);
463 if (preadbuf->data == NULL) {
464 return NT_STATUS_NO_MEMORY;
467 if (!(aio_ex = create_aio_extra(smbreq->smb2req, fsp, 0))) {
468 return NT_STATUS_NO_MEMORY;
470 aio_ex->handle_completion = handle_aio_smb2_read_complete;
471 aio_ex->pass_cancel = true;
473 init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
474 (uint64_t)startpos, (uint64_t)smb_maxcnt, READ_LOCK,
475 &aio_ex->lock);
477 /* Take the lock until the AIO completes. */
478 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
479 TALLOC_FREE(aio_ex);
480 return NT_STATUS_FILE_LOCK_CONFLICT;
483 a = &aio_ex->acb;
485 /* Now set up the aio record for the read call. */
487 a->aio_fildes = fsp->fh->fd;
488 a->aio_buf = preadbuf->data;
489 a->aio_nbytes = smb_maxcnt;
490 a->aio_offset = startpos;
491 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
492 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
493 a->aio_sigevent.sigev_value.sival_ptr = aio_ex;
495 ret = SMB_VFS_AIO_READ(fsp, a);
496 if (ret == -1) {
497 DEBUG(0,("smb2: aio_read failed. "
498 "Error %s\n", strerror(errno) ));
499 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
500 TALLOC_FREE(aio_ex);
501 return NT_STATUS_RETRY;
504 outstanding_aio_calls++;
505 /* We don't need talloc_move here as both aio_ex and
506 * smbreq are children of smbreq->smb2req. */
507 aio_ex->smbreq = smbreq;
508 smbreq->async_priv = aio_ex;
510 DEBUG(10,("smb2: scheduled aio_read for file %s, "
511 "offset %.0f, len = %u (mid = %u)\n",
512 fsp_str_dbg(fsp), (double)startpos, (unsigned int)smb_maxcnt,
513 (unsigned int)aio_ex->smbreq->mid ));
515 return NT_STATUS_OK;
518 /****************************************************************************
519 Set up an aio request from a SMB2write call.
520 *****************************************************************************/
522 NTSTATUS schedule_aio_smb2_write(connection_struct *conn,
523 struct smb_request *smbreq,
524 files_struct *fsp,
525 uint64_t in_offset,
526 DATA_BLOB in_data,
527 bool write_through)
529 struct aio_extra *aio_ex = NULL;
530 SMB_STRUCT_AIOCB *a = NULL;
531 size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
532 int ret;
534 /* Ensure aio is initialized. */
535 if (!initialize_async_io_handler()) {
536 return NT_STATUS_RETRY;
539 if (fsp->base_fsp != NULL) {
540 /* No AIO on streams yet */
541 DEBUG(10, ("AIO on streams not yet supported\n"));
542 return NT_STATUS_RETRY;
545 if ((!min_aio_write_size || (in_data.length < min_aio_write_size))
546 && !SMB_VFS_AIO_FORCE(fsp)) {
547 /* Too small a write for aio request. */
548 DEBUG(10,("smb2: write size (%u) too "
549 "small for minimum aio_write of %u\n",
550 (unsigned int)in_data.length,
551 (unsigned int)min_aio_write_size ));
552 return NT_STATUS_RETRY;
555 /* Only do this on writes not using the write cache. */
556 if (lp_write_cache_size(SNUM(conn)) != 0) {
557 return NT_STATUS_RETRY;
560 if (outstanding_aio_calls >= aio_pending_size) {
561 DEBUG(3,("smb2: Already have %d aio "
562 "activities outstanding.\n",
563 outstanding_aio_calls ));
564 return NT_STATUS_RETRY;
567 if (!(aio_ex = create_aio_extra(smbreq->smb2req, fsp, 0))) {
568 return NT_STATUS_NO_MEMORY;
571 aio_ex->handle_completion = handle_aio_smb2_write_complete;
572 aio_ex->write_through = write_through;
573 aio_ex->pass_cancel = true;
575 init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
576 in_offset, (uint64_t)in_data.length, WRITE_LOCK,
577 &aio_ex->lock);
579 /* Take the lock until the AIO completes. */
580 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
581 TALLOC_FREE(aio_ex);
582 return NT_STATUS_FILE_LOCK_CONFLICT;
585 a = &aio_ex->acb;
587 /* Now set up the aio record for the write call. */
589 a->aio_fildes = fsp->fh->fd;
590 a->aio_buf = in_data.data;
591 a->aio_nbytes = in_data.length;
592 a->aio_offset = in_offset;
593 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
594 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
595 a->aio_sigevent.sigev_value.sival_ptr = aio_ex;
597 ret = SMB_VFS_AIO_WRITE(fsp, a);
598 if (ret == -1) {
599 DEBUG(3,("smb2: aio_write failed. "
600 "Error %s\n", strerror(errno) ));
601 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
602 TALLOC_FREE(aio_ex);
603 return NT_STATUS_RETRY;
606 outstanding_aio_calls++;
607 /* We don't need talloc_move here as both aio_ex and
608 * smbreq are children of smbreq->smb2req. */
609 aio_ex->smbreq = smbreq;
610 smbreq->async_priv = aio_ex;
612 /* This should actually be improved to span the write. */
613 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WRITE);
614 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WRITE);
617 * We don't want to do write behind due to ownership
618 * issues of the request structs. Maybe add it if I
619 * figure those out. JRA.
622 DEBUG(10,("smb2: scheduled aio_write for file "
623 "%s, offset %.0f, len = %u (mid = %u) "
624 "outstanding_aio_calls = %d\n",
625 fsp_str_dbg(fsp),
626 (double)in_offset,
627 (unsigned int)in_data.length,
628 (unsigned int)aio_ex->smbreq->mid,
629 outstanding_aio_calls ));
631 return NT_STATUS_OK;
634 /****************************************************************************
635 Complete the read and return the data or error back to the client.
636 Returns errno or zero if all ok.
637 *****************************************************************************/
639 static int handle_aio_read_complete(struct aio_extra *aio_ex, int errcode)
641 int outsize;
642 char *outbuf = (char *)aio_ex->outbuf.data;
643 char *data = smb_buf(outbuf);
644 ssize_t nread = SMB_VFS_AIO_RETURN(aio_ex->fsp,&aio_ex->acb);
646 if (nread < 0) {
647 /* We're relying here on the fact that if the fd is
648 closed then the aio will complete and aio_return
649 will return an error. Hopefully this is
650 true.... JRA. */
652 DEBUG( 3,( "handle_aio_read_complete: file %s nread == %d. "
653 "Error = %s\n",
654 fsp_str_dbg(aio_ex->fsp), (int)nread, strerror(errcode)));
656 ERROR_NT(map_nt_error_from_unix(errcode));
657 outsize = srv_set_message(outbuf,0,0,true);
658 } else {
659 outsize = srv_set_message(outbuf,12,nread,False);
660 SSVAL(outbuf,smb_vwv2,0xFFFF); /* Remaining - must be * -1. */
661 SSVAL(outbuf,smb_vwv5,nread);
662 SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
663 SSVAL(outbuf,smb_vwv7,((nread >> 16) & 1));
664 SSVAL(smb_buf(outbuf),-2,nread);
666 aio_ex->fsp->fh->pos = aio_ex->acb.aio_offset + nread;
667 aio_ex->fsp->fh->position_information = aio_ex->fsp->fh->pos;
669 DEBUG( 3, ( "handle_aio_read_complete file %s max=%d "
670 "nread=%d\n",
671 fsp_str_dbg(aio_ex->fsp),
672 (int)aio_ex->acb.aio_nbytes, (int)nread ) );
675 smb_setlen(outbuf,outsize - 4);
676 show_msg(outbuf);
677 if (!srv_send_smb(aio_ex->smbreq->sconn, outbuf,
678 true, aio_ex->smbreq->seqnum+1,
679 IS_CONN_ENCRYPTED(aio_ex->fsp->conn), NULL)) {
680 exit_server_cleanly("handle_aio_read_complete: srv_send_smb "
681 "failed.");
684 DEBUG(10,("handle_aio_read_complete: scheduled aio_read completed "
685 "for file %s, offset %.0f, len = %u\n",
686 fsp_str_dbg(aio_ex->fsp), (double)aio_ex->acb.aio_offset,
687 (unsigned int)nread ));
689 return errcode;
692 /****************************************************************************
693 Complete the write and return the data or error back to the client.
694 Returns error code or zero if all ok.
695 *****************************************************************************/
697 static int handle_aio_write_complete(struct aio_extra *aio_ex, int errcode)
699 files_struct *fsp = aio_ex->fsp;
700 char *outbuf = (char *)aio_ex->outbuf.data;
701 ssize_t numtowrite = aio_ex->acb.aio_nbytes;
702 ssize_t nwritten = SMB_VFS_AIO_RETURN(fsp,&aio_ex->acb);
704 if (fsp->aio_write_behind) {
705 if (nwritten != numtowrite) {
706 if (nwritten == -1) {
707 DEBUG(5,("handle_aio_write_complete: "
708 "aio_write_behind failed ! File %s "
709 "is corrupt ! Error %s\n",
710 fsp_str_dbg(fsp), strerror(errcode)));
711 } else {
712 DEBUG(0,("handle_aio_write_complete: "
713 "aio_write_behind failed ! File %s "
714 "is corrupt ! Wanted %u bytes but "
715 "only wrote %d\n", fsp_str_dbg(fsp),
716 (unsigned int)numtowrite,
717 (int)nwritten ));
718 errcode = EIO;
720 } else {
721 DEBUG(10,("handle_aio_write_complete: "
722 "aio_write_behind completed for file %s\n",
723 fsp_str_dbg(fsp)));
725 /* TODO: should no return 0 in case of an error !!! */
726 return 0;
729 /* We don't need outsize or set_message here as we've already set the
730 fixed size length when we set up the aio call. */
732 if(nwritten == -1) {
733 DEBUG( 3,( "handle_aio_write: file %s wanted %u bytes. "
734 "nwritten == %d. Error = %s\n",
735 fsp_str_dbg(fsp), (unsigned int)numtowrite,
736 (int)nwritten, strerror(errcode) ));
738 ERROR_NT(map_nt_error_from_unix(errcode));
739 srv_set_message(outbuf,0,0,true);
740 } else {
741 NTSTATUS status;
743 SSVAL(outbuf,smb_vwv2,nwritten);
744 SSVAL(outbuf,smb_vwv4,(nwritten>>16)&1);
745 if (nwritten < (ssize_t)numtowrite) {
746 SCVAL(outbuf,smb_rcls,ERRHRD);
747 SSVAL(outbuf,smb_err,ERRdiskfull);
750 DEBUG(3,("handle_aio_write: fnum=%d num=%d wrote=%d\n",
751 fsp->fnum, (int)numtowrite, (int)nwritten));
752 status = sync_file(fsp->conn,fsp, aio_ex->write_through);
753 if (!NT_STATUS_IS_OK(status)) {
754 errcode = errno;
755 ERROR_BOTH(map_nt_error_from_unix(errcode),
756 ERRHRD, ERRdiskfull);
757 srv_set_message(outbuf,0,0,true);
758 DEBUG(5,("handle_aio_write: sync_file for %s returned %s\n",
759 fsp_str_dbg(fsp), nt_errstr(status)));
762 aio_ex->fsp->fh->pos = aio_ex->acb.aio_offset + nwritten;
765 show_msg(outbuf);
766 if (!srv_send_smb(aio_ex->smbreq->sconn, outbuf,
767 true, aio_ex->smbreq->seqnum+1,
768 IS_CONN_ENCRYPTED(fsp->conn),
769 NULL)) {
770 exit_server_cleanly("handle_aio_write_complete: "
771 "srv_send_smb failed.");
774 DEBUG(10,("handle_aio_write_complete: scheduled aio_write completed "
775 "for file %s, offset %.0f, requested %u, written = %u\n",
776 fsp_str_dbg(fsp), (double)aio_ex->acb.aio_offset,
777 (unsigned int)numtowrite, (unsigned int)nwritten ));
779 return errcode;
782 /****************************************************************************
783 Complete the read and return the data or error back to the client.
784 Returns errno or zero if all ok.
785 *****************************************************************************/
787 static int handle_aio_smb2_read_complete(struct aio_extra *aio_ex, int errcode)
789 NTSTATUS status;
790 struct tevent_req *subreq = aio_ex->smbreq->smb2req->subreq;
791 ssize_t nread = SMB_VFS_AIO_RETURN(aio_ex->fsp,&aio_ex->acb);
793 /* Common error or success code processing for async or sync
794 read returns. */
796 status = smb2_read_complete(subreq, nread, errcode);
798 if (nread > 0) {
799 aio_ex->fsp->fh->pos = aio_ex->acb.aio_offset + nread;
800 aio_ex->fsp->fh->position_information = aio_ex->fsp->fh->pos;
803 DEBUG(10,("smb2: scheduled aio_read completed "
804 "for file %s, offset %.0f, len = %u "
805 "(errcode = %d, NTSTATUS = %s)\n",
806 fsp_str_dbg(aio_ex->fsp),
807 (double)aio_ex->acb.aio_offset,
808 (unsigned int)nread,
809 errcode,
810 nt_errstr(status) ));
812 if (!NT_STATUS_IS_OK(status)) {
813 tevent_req_nterror(subreq, status);
814 return errcode;
817 tevent_req_done(subreq);
818 return errcode;
821 /****************************************************************************
822 Complete the SMB2 write and return the data or error back to the client.
823 Returns error code or zero if all ok.
824 *****************************************************************************/
826 static int handle_aio_smb2_write_complete(struct aio_extra *aio_ex, int errcode)
828 files_struct *fsp = aio_ex->fsp;
829 ssize_t numtowrite = aio_ex->acb.aio_nbytes;
830 ssize_t nwritten = SMB_VFS_AIO_RETURN(fsp,&aio_ex->acb);
831 struct tevent_req *subreq = aio_ex->smbreq->smb2req->subreq;
832 NTSTATUS status;
834 status = smb2_write_complete(subreq, nwritten, errcode);
836 DEBUG(10,("smb2: scheduled aio_write completed "
837 "for file %s, offset %.0f, requested %u, "
838 "written = %u (errcode = %d, NTSTATUS = %s)\n",
839 fsp_str_dbg(fsp),
840 (double)aio_ex->acb.aio_offset,
841 (unsigned int)numtowrite,
842 (unsigned int)nwritten,
843 errcode,
844 nt_errstr(status) ));
846 if (!NT_STATUS_IS_OK(status)) {
847 tevent_req_nterror(subreq, status);
848 return errcode;
851 tevent_req_done(subreq);
852 return errcode;
855 /****************************************************************************
856 Handle any aio completion. Returns True if finished (and sets *perr if err
857 was non-zero), False if not.
858 *****************************************************************************/
860 static bool handle_aio_completed(struct aio_extra *aio_ex, int *perr)
862 files_struct *fsp = NULL;
863 int err;
865 if(!aio_ex) {
866 DEBUG(3, ("handle_aio_completed: Non-existing aio_ex passed\n"));
867 return false;
870 if (!aio_ex->fsp) {
871 DEBUG(3, ("handle_aio_completed: aio_ex->fsp == NULL\n"));
872 return false;
875 fsp = aio_ex->fsp;
877 /* Ensure the operation has really completed. */
878 err = SMB_VFS_AIO_ERROR(fsp, &aio_ex->acb);
879 if (err == EINPROGRESS) {
880 DEBUG(10,( "handle_aio_completed: operation mid %llu still in "
881 "process for file %s\n",
882 (unsigned long long)aio_ex->smbreq->mid,
883 fsp_str_dbg(aio_ex->fsp)));
884 return False;
887 /* Unlock now we're done. */
888 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &aio_ex->lock);
890 if (!aio_ex->pass_cancel && err == ECANCELED) {
891 /* If error is ECANCELED then don't return anything to the
892 * client. */
893 DEBUG(10,( "handle_aio_completed: operation mid %llu"
894 " canceled\n",
895 (unsigned long long)aio_ex->smbreq->mid));
896 return True;
899 err = aio_ex->handle_completion(aio_ex, err);
900 if (err) {
901 *perr = err; /* Only save non-zero errors. */
904 return True;
907 /****************************************************************************
908 Handle any aio completion inline.
909 *****************************************************************************/
911 void smbd_aio_complete_aio_ex(struct aio_extra *aio_ex)
913 files_struct *fsp = NULL;
914 int ret = 0;
916 outstanding_aio_calls--;
918 DEBUG(10,("smbd_aio_complete_mid: mid[%llu]\n",
919 (unsigned long long)aio_ex->smbreq->mid));
921 fsp = aio_ex->fsp;
922 if (fsp == NULL) {
923 /* file was closed whilst I/O was outstanding. Just
924 * ignore. */
925 DEBUG( 3,( "smbd_aio_complete_mid: file closed whilst "
926 "aio outstanding (mid[%llu]).\n",
927 (unsigned long long)aio_ex->smbreq->mid));
928 return;
931 if (!handle_aio_completed(aio_ex, &ret)) {
932 return;
935 TALLOC_FREE(aio_ex);
938 /****************************************************************************
939 We're doing write behind and the client closed the file. Wait up to 30
940 seconds (my arbitrary choice) for the aio to complete. Return 0 if all writes
941 completed, errno to return if not.
942 *****************************************************************************/
944 #define SMB_TIME_FOR_AIO_COMPLETE_WAIT 29
946 int wait_for_aio_completion(files_struct *fsp)
948 struct aio_extra *aio_ex;
949 const SMB_STRUCT_AIOCB **aiocb_list;
950 int aio_completion_count = 0;
951 time_t start_time = time_mono(NULL);
952 int seconds_left;
954 for (seconds_left = SMB_TIME_FOR_AIO_COMPLETE_WAIT;
955 seconds_left >= 0;) {
956 int err = 0;
957 int i;
958 struct timespec ts;
960 aio_completion_count = 0;
961 for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
962 if (aio_ex->fsp == fsp) {
963 aio_completion_count++;
967 if (!aio_completion_count) {
968 return 0;
971 DEBUG(3,("wait_for_aio_completion: waiting for %d aio events "
972 "to complete.\n", aio_completion_count ));
974 aiocb_list = SMB_MALLOC_ARRAY(const SMB_STRUCT_AIOCB *,
975 aio_completion_count);
976 if (!aiocb_list) {
977 return ENOMEM;
980 for( i = 0, aio_ex = aio_list_head;
981 aio_ex;
982 aio_ex = aio_ex->next) {
983 if (aio_ex->fsp == fsp) {
984 aiocb_list[i++] = &aio_ex->acb;
988 /* Now wait up to seconds_left for completion. */
989 ts.tv_sec = seconds_left;
990 ts.tv_nsec = 0;
992 DEBUG(10,("wait_for_aio_completion: %d events, doing a wait "
993 "of %d seconds.\n",
994 aio_completion_count, seconds_left ));
996 err = SMB_VFS_AIO_SUSPEND(fsp, aiocb_list,
997 aio_completion_count, &ts);
999 DEBUG(10,("wait_for_aio_completion: returned err = %d, "
1000 "errno = %s\n", err, strerror(errno) ));
1002 if (err == -1 && errno == EAGAIN) {
1003 DEBUG(0,("wait_for_aio_completion: aio_suspend timed "
1004 "out waiting for %d events after a wait of "
1005 "%d seconds\n", aio_completion_count,
1006 seconds_left));
1007 /* Timeout. */
1008 cancel_aio_by_fsp(fsp);
1009 SAFE_FREE(aiocb_list);
1010 return EIO;
1013 /* One or more events might have completed - process them if
1014 * so. */
1015 for( i = 0; i < aio_completion_count; i++) {
1016 aio_ex = (struct aio_extra *)aiocb_list[i]->aio_sigevent.sigev_value.sival_ptr;
1018 if (!handle_aio_completed(aio_ex, &err)) {
1019 continue;
1021 TALLOC_FREE(aio_ex);
1024 SAFE_FREE(aiocb_list);
1025 seconds_left = SMB_TIME_FOR_AIO_COMPLETE_WAIT
1026 - (time_mono(NULL) - start_time);
1029 /* We timed out - we don't know why. Return ret if already an error,
1030 * else EIO. */
1031 DEBUG(10,("wait_for_aio_completion: aio_suspend timed out waiting "
1032 "for %d events\n",
1033 aio_completion_count));
1035 return EIO;
1038 /****************************************************************************
1039 Cancel any outstanding aio requests. The client doesn't care about the reply.
1040 *****************************************************************************/
1042 void cancel_aio_by_fsp(files_struct *fsp)
1044 struct aio_extra *aio_ex;
1046 for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
1047 if (aio_ex->fsp == fsp) {
1048 /* Unlock now we're done. */
1049 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &aio_ex->lock);
1051 /* Don't delete the aio_extra record as we may have
1052 completed and don't yet know it. Just do the
1053 aio_cancel call and return. */
1054 SMB_VFS_AIO_CANCEL(fsp, &aio_ex->acb);
1055 aio_ex->fsp = NULL; /* fsp will be closed when we
1056 * return. */
1061 #else
1062 NTSTATUS schedule_aio_read_and_X(connection_struct *conn,
1063 struct smb_request *smbreq,
1064 files_struct *fsp, SMB_OFF_T startpos,
1065 size_t smb_maxcnt)
1067 return NT_STATUS_RETRY;
1070 NTSTATUS schedule_aio_write_and_X(connection_struct *conn,
1071 struct smb_request *smbreq,
1072 files_struct *fsp, const char *data,
1073 SMB_OFF_T startpos,
1074 size_t numtowrite)
1076 return NT_STATUS_RETRY;
1079 bool cancel_smb2_aio(struct smb_request *smbreq)
1081 return false;
1084 NTSTATUS schedule_smb2_aio_read(connection_struct *conn,
1085 struct smb_request *smbreq,
1086 files_struct *fsp,
1087 TALLOC_CTX *ctx,
1088 DATA_BLOB *preadbuf,
1089 SMB_OFF_T startpos,
1090 size_t smb_maxcnt)
1092 return NT_STATUS_RETRY;
1095 NTSTATUS schedule_aio_smb2_write(connection_struct *conn,
1096 struct smb_request *smbreq,
1097 files_struct *fsp,
1098 uint64_t in_offset,
1099 DATA_BLOB in_data,
1100 bool write_through)
1102 return NT_STATUS_RETRY;
1105 void cancel_aio_by_fsp(files_struct *fsp)
1109 int wait_for_aio_completion(files_struct *fsp)
1111 return 0;
1114 void smbd_aio_complete_mid(uint64_t mid);
1116 #endif