WHATSNEW: Update changes.
[Samba/gebeck_regimport.git] / source3 / smbd / aio.c
blobb0755bb7383b640e7e4acd360b115a9ca4348075
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/globals.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 #ifndef HAVE_STRUCT_SIGEVENT_SIGEV_VALUE_SIVAL_PTR
32 #ifdef HAVE_STRUCT_SIGEVENT_SIGEV_VALUE_SIGVAL_PTR
33 #define sival_int sigval_int
34 #define sival_ptr sigval_ptr
35 #endif
36 #endif
38 /****************************************************************************
39 The buffer we keep around whilst an aio request is in process.
40 *****************************************************************************/
42 struct aio_extra {
43 struct aio_extra *next, *prev;
44 SMB_STRUCT_AIOCB acb;
45 files_struct *fsp;
46 struct smb_request *smbreq;
47 DATA_BLOB outbuf;
48 struct lock_struct lock;
49 bool write_through;
50 int (*handle_completion)(struct aio_extra *ex, int errcode);
53 /****************************************************************************
54 Initialize the signal handler for aio read/write.
55 *****************************************************************************/
57 static void smbd_aio_signal_handler(struct tevent_context *ev_ctx,
58 struct tevent_signal *se,
59 int signum, int count,
60 void *_info, void *private_data)
62 siginfo_t *info = (siginfo_t *)_info;
63 struct aio_extra *aio_ex = (struct aio_extra *)
64 info->si_value.sival_ptr;
66 smbd_aio_complete_aio_ex(aio_ex);
70 static bool initialize_async_io_handler(void)
72 static bool tried_signal_setup = false;
74 if (aio_signal_event) {
75 return true;
77 if (tried_signal_setup) {
78 return false;
80 tried_signal_setup = true;
82 aio_signal_event = tevent_add_signal(smbd_event_context(),
83 smbd_event_context(),
84 RT_SIGNAL_AIO, SA_SIGINFO,
85 smbd_aio_signal_handler,
86 NULL);
87 if (!aio_signal_event) {
88 DEBUG(10, ("Failed to setup RT_SIGNAL_AIO handler\n"));
89 return false;
92 /* tevent supports 100 signal with SA_SIGINFO */
93 aio_pending_size = 100;
94 return true;
97 static int handle_aio_read_complete(struct aio_extra *aio_ex, int errcode);
98 static int handle_aio_write_complete(struct aio_extra *aio_ex, int errcode);
99 static int handle_aio_smb2_read_complete(struct aio_extra *aio_ex, int errcode);
100 static int handle_aio_smb2_write_complete(struct aio_extra *aio_ex, int errcode);
102 static int aio_extra_destructor(struct aio_extra *aio_ex)
104 DLIST_REMOVE(aio_list_head, aio_ex);
105 return 0;
108 /****************************************************************************
109 Create the extended aio struct we must keep around for the lifetime
110 of the aio call.
111 *****************************************************************************/
113 static struct aio_extra *create_aio_extra(TALLOC_CTX *mem_ctx,
114 files_struct *fsp,
115 size_t buflen)
117 struct aio_extra *aio_ex = TALLOC_ZERO_P(mem_ctx, struct aio_extra);
119 if (!aio_ex) {
120 return NULL;
123 /* The output buffer stored in the aio_ex is the start of
124 the smb return buffer. The buffer used in the acb
125 is the start of the reply data portion of that buffer. */
127 if (buflen) {
128 aio_ex->outbuf = data_blob_talloc(aio_ex, NULL, buflen);
129 if (!aio_ex->outbuf.data) {
130 TALLOC_FREE(aio_ex);
131 return NULL;
134 DLIST_ADD(aio_list_head, aio_ex);
135 talloc_set_destructor(aio_ex, aio_extra_destructor);
136 aio_ex->fsp = fsp;
137 return aio_ex;
140 /****************************************************************************
141 Set up an aio request from a SMBreadX call.
142 *****************************************************************************/
144 NTSTATUS schedule_aio_read_and_X(connection_struct *conn,
145 struct smb_request *smbreq,
146 files_struct *fsp, SMB_OFF_T startpos,
147 size_t smb_maxcnt)
149 struct aio_extra *aio_ex;
150 SMB_STRUCT_AIOCB *a;
151 size_t bufsize;
152 size_t min_aio_read_size = lp_aio_read_size(SNUM(conn));
153 int ret;
155 /* Ensure aio is initialized. */
156 if (!initialize_async_io_handler()) {
157 return NT_STATUS_RETRY;
160 if (fsp->base_fsp != NULL) {
161 /* No AIO on streams yet */
162 DEBUG(10, ("AIO on streams not yet supported\n"));
163 return NT_STATUS_RETRY;
166 if ((!min_aio_read_size || (smb_maxcnt < min_aio_read_size))
167 && !SMB_VFS_AIO_FORCE(fsp)) {
168 /* Too small a read for aio request. */
169 DEBUG(10,("schedule_aio_read_and_X: read size (%u) too small "
170 "for minimum aio_read of %u\n",
171 (unsigned int)smb_maxcnt,
172 (unsigned int)min_aio_read_size ));
173 return NT_STATUS_RETRY;
176 /* Only do this on non-chained and non-chaining reads not using the
177 * write cache. */
178 if (req_is_in_chain(smbreq) || (lp_write_cache_size(SNUM(conn)) != 0)) {
179 return NT_STATUS_RETRY;
182 if (outstanding_aio_calls >= aio_pending_size) {
183 DEBUG(10,("schedule_aio_read_and_X: Already have %d aio "
184 "activities outstanding.\n",
185 outstanding_aio_calls ));
186 return NT_STATUS_RETRY;
189 /* The following is safe from integer wrap as we've already checked
190 smb_maxcnt is 128k or less. Wct is 12 for read replies */
192 bufsize = smb_size + 12 * 2 + smb_maxcnt;
194 if ((aio_ex = create_aio_extra(NULL, fsp, bufsize)) == NULL) {
195 DEBUG(10,("schedule_aio_read_and_X: malloc fail.\n"));
196 return NT_STATUS_NO_MEMORY;
198 aio_ex->handle_completion = handle_aio_read_complete;
200 construct_reply_common_req(smbreq, (char *)aio_ex->outbuf.data);
201 srv_set_message((char *)aio_ex->outbuf.data, 12, 0, True);
202 SCVAL(aio_ex->outbuf.data,smb_vwv0,0xFF); /* Never a chained reply. */
204 init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
205 (uint64_t)startpos, (uint64_t)smb_maxcnt, READ_LOCK,
206 &aio_ex->lock);
208 /* Take the lock until the AIO completes. */
209 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
210 TALLOC_FREE(aio_ex);
211 return NT_STATUS_FILE_LOCK_CONFLICT;
214 a = &aio_ex->acb;
216 /* Now set up the aio record for the read call. */
218 a->aio_fildes = fsp->fh->fd;
219 a->aio_buf = smb_buf(aio_ex->outbuf.data);
220 a->aio_nbytes = smb_maxcnt;
221 a->aio_offset = startpos;
222 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
223 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
224 a->aio_sigevent.sigev_value.sival_ptr = aio_ex;
226 ret = SMB_VFS_AIO_READ(fsp, a);
227 if (ret == -1) {
228 DEBUG(0,("schedule_aio_read_and_X: aio_read failed. "
229 "Error %s\n", strerror(errno) ));
230 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
231 TALLOC_FREE(aio_ex);
232 return NT_STATUS_RETRY;
235 outstanding_aio_calls++;
236 aio_ex->smbreq = talloc_move(aio_ex, &smbreq);
238 DEBUG(10,("schedule_aio_read_and_X: scheduled aio_read for file %s, "
239 "offset %.0f, len = %u (mid = %u)\n",
240 fsp_str_dbg(fsp), (double)startpos, (unsigned int)smb_maxcnt,
241 (unsigned int)aio_ex->smbreq->mid ));
243 return NT_STATUS_OK;
246 /****************************************************************************
247 Set up an aio request from a SMBwriteX call.
248 *****************************************************************************/
250 NTSTATUS schedule_aio_write_and_X(connection_struct *conn,
251 struct smb_request *smbreq,
252 files_struct *fsp, char *data,
253 SMB_OFF_T startpos,
254 size_t numtowrite)
256 struct aio_extra *aio_ex;
257 SMB_STRUCT_AIOCB *a;
258 size_t bufsize;
259 size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
260 int ret;
262 /* Ensure aio is initialized. */
263 if (!initialize_async_io_handler()) {
264 return NT_STATUS_RETRY;
267 if (fsp->base_fsp != NULL) {
268 /* No AIO on streams yet */
269 DEBUG(10, ("AIO on streams not yet supported\n"));
270 return NT_STATUS_RETRY;
273 if ((!min_aio_write_size || (numtowrite < min_aio_write_size))
274 && !SMB_VFS_AIO_FORCE(fsp)) {
275 /* Too small a write for aio request. */
276 DEBUG(10,("schedule_aio_write_and_X: write size (%u) too "
277 "small for minimum aio_write of %u\n",
278 (unsigned int)numtowrite,
279 (unsigned int)min_aio_write_size ));
280 return NT_STATUS_RETRY;
283 /* Only do this on non-chained and non-chaining writes not using the
284 * write cache. */
285 if (req_is_in_chain(smbreq) || (lp_write_cache_size(SNUM(conn)) != 0)) {
286 return NT_STATUS_RETRY;
289 if (outstanding_aio_calls >= aio_pending_size) {
290 DEBUG(3,("schedule_aio_write_and_X: Already have %d aio "
291 "activities outstanding.\n",
292 outstanding_aio_calls ));
293 DEBUG(10,("schedule_aio_write_and_X: failed to schedule "
294 "aio_write for file %s, offset %.0f, len = %u "
295 "(mid = %u)\n",
296 fsp_str_dbg(fsp), (double)startpos,
297 (unsigned int)numtowrite,
298 (unsigned int)smbreq->mid ));
299 return NT_STATUS_RETRY;
302 bufsize = smb_size + 6*2;
304 if (!(aio_ex = create_aio_extra(NULL, fsp, bufsize))) {
305 DEBUG(0,("schedule_aio_write_and_X: malloc fail.\n"));
306 return NT_STATUS_NO_MEMORY;
308 aio_ex->handle_completion = handle_aio_write_complete;
309 aio_ex->write_through = BITSETW(smbreq->vwv+7,0);
311 construct_reply_common_req(smbreq, (char *)aio_ex->outbuf.data);
312 srv_set_message((char *)aio_ex->outbuf.data, 6, 0, True);
313 SCVAL(aio_ex->outbuf.data,smb_vwv0,0xFF); /* Never a chained reply. */
315 init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
316 (uint64_t)startpos, (uint64_t)numtowrite, WRITE_LOCK,
317 &aio_ex->lock);
319 /* Take the lock until the AIO completes. */
320 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
321 TALLOC_FREE(aio_ex);
322 return NT_STATUS_FILE_LOCK_CONFLICT;
325 a = &aio_ex->acb;
327 /* Now set up the aio record for the write call. */
329 a->aio_fildes = fsp->fh->fd;
330 a->aio_buf = data;
331 a->aio_nbytes = numtowrite;
332 a->aio_offset = startpos;
333 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
334 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
335 a->aio_sigevent.sigev_value.sival_ptr = aio_ex;
337 ret = SMB_VFS_AIO_WRITE(fsp, a);
338 if (ret == -1) {
339 DEBUG(3,("schedule_aio_wrote_and_X: aio_write failed. "
340 "Error %s\n", strerror(errno) ));
341 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
342 TALLOC_FREE(aio_ex);
343 return NT_STATUS_RETRY;
346 outstanding_aio_calls++;
347 aio_ex->smbreq = talloc_move(aio_ex, &smbreq);
349 /* This should actually be improved to span the write. */
350 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WRITE);
351 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WRITE);
353 if (!aio_ex->write_through && !lp_syncalways(SNUM(fsp->conn))
354 && fsp->aio_write_behind) {
355 /* Lie to the client and immediately claim we finished the
356 * write. */
357 SSVAL(aio_ex->outbuf.data,smb_vwv2,numtowrite);
358 SSVAL(aio_ex->outbuf.data,smb_vwv4,(numtowrite>>16)&1);
359 show_msg((char *)aio_ex->outbuf.data);
360 if (!srv_send_smb(smbd_server_fd(),(char *)aio_ex->outbuf.data,
361 true, aio_ex->smbreq->seqnum+1,
362 IS_CONN_ENCRYPTED(fsp->conn),
363 &aio_ex->smbreq->pcd)) {
364 exit_server_cleanly("handle_aio_write: srv_send_smb "
365 "failed.");
367 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write "
368 "behind for file %s\n", fsp_str_dbg(fsp)));
371 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write for file "
372 "%s, offset %.0f, len = %u (mid = %u) "
373 "outstanding_aio_calls = %d\n",
374 fsp_str_dbg(fsp), (double)startpos, (unsigned int)numtowrite,
375 (unsigned int)aio_ex->smbreq->mid, outstanding_aio_calls ));
377 return NT_STATUS_OK;
380 /****************************************************************************
381 Set up an aio request from a SMB2 read call.
382 *****************************************************************************/
384 NTSTATUS schedule_smb2_aio_read(connection_struct *conn,
385 struct smb_request *smbreq,
386 files_struct *fsp,
387 char *inbuf,
388 SMB_OFF_T startpos,
389 size_t smb_maxcnt)
391 struct aio_extra *aio_ex;
392 SMB_STRUCT_AIOCB *a;
393 size_t min_aio_read_size = lp_aio_read_size(SNUM(conn));
394 int ret;
396 /* Ensure aio is initialized. */
397 if (!initialize_async_io_handler()) {
398 return NT_STATUS_RETRY;
401 if (fsp->base_fsp != NULL) {
402 /* No AIO on streams yet */
403 DEBUG(10, ("AIO on streams not yet supported\n"));
404 return NT_STATUS_RETRY;
407 if ((!min_aio_read_size || (smb_maxcnt < min_aio_read_size))
408 && !SMB_VFS_AIO_FORCE(fsp)) {
409 /* Too small a read for aio request. */
410 DEBUG(10,("smb2: read size (%u) too small "
411 "for minimum aio_read of %u\n",
412 (unsigned int)smb_maxcnt,
413 (unsigned int)min_aio_read_size ));
414 return NT_STATUS_RETRY;
417 /* Only do this on reads not using the write cache. */
418 if (lp_write_cache_size(SNUM(conn)) != 0) {
419 return NT_STATUS_RETRY;
422 if (outstanding_aio_calls >= aio_pending_size) {
423 DEBUG(10,("smb2: Already have %d aio "
424 "activities outstanding.\n",
425 outstanding_aio_calls ));
426 return NT_STATUS_RETRY;
429 if (!(aio_ex = create_aio_extra(smbreq->smb2req, fsp, 0))) {
430 return NT_STATUS_NO_MEMORY;
432 aio_ex->handle_completion = handle_aio_smb2_read_complete;
434 init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
435 (uint64_t)startpos, (uint64_t)smb_maxcnt, READ_LOCK,
436 &aio_ex->lock);
438 /* Take the lock until the AIO completes. */
439 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
440 TALLOC_FREE(aio_ex);
441 return NT_STATUS_FILE_LOCK_CONFLICT;
444 a = &aio_ex->acb;
446 /* Now set up the aio record for the read call. */
448 a->aio_fildes = fsp->fh->fd;
449 a->aio_buf = inbuf;
450 a->aio_nbytes = smb_maxcnt;
451 a->aio_offset = startpos;
452 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
453 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
454 a->aio_sigevent.sigev_value.sival_ptr = aio_ex;
456 ret = SMB_VFS_AIO_READ(fsp, a);
457 if (ret == -1) {
458 DEBUG(0,("smb2: aio_read failed. "
459 "Error %s\n", strerror(errno) ));
460 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
461 TALLOC_FREE(aio_ex);
462 return NT_STATUS_RETRY;
465 outstanding_aio_calls++;
466 /* We don't need talloc_move here as both aio_ex and
467 * smbreq are children of smbreq->smb2req. */
468 aio_ex->smbreq = smbreq;
470 DEBUG(10,("smb2: scheduled aio_read for file %s, "
471 "offset %.0f, len = %u (mid = %u)\n",
472 fsp_str_dbg(fsp), (double)startpos, (unsigned int)smb_maxcnt,
473 (unsigned int)aio_ex->smbreq->mid ));
475 return NT_STATUS_OK;
478 /****************************************************************************
479 Set up an aio request from a SMB2write call.
480 *****************************************************************************/
482 NTSTATUS schedule_aio_smb2_write(connection_struct *conn,
483 struct smb_request *smbreq,
484 files_struct *fsp,
485 uint64_t in_offset,
486 DATA_BLOB in_data,
487 bool write_through)
489 struct aio_extra *aio_ex = NULL;
490 SMB_STRUCT_AIOCB *a = NULL;
491 size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
492 int ret;
494 /* Ensure aio is initialized. */
495 if (!initialize_async_io_handler()) {
496 return NT_STATUS_RETRY;
499 if (fsp->base_fsp != NULL) {
500 /* No AIO on streams yet */
501 DEBUG(10, ("AIO on streams not yet supported\n"));
502 return NT_STATUS_RETRY;
505 if ((!min_aio_write_size || (in_data.length < min_aio_write_size))
506 && !SMB_VFS_AIO_FORCE(fsp)) {
507 /* Too small a write for aio request. */
508 DEBUG(10,("smb2: write size (%u) too "
509 "small for minimum aio_write of %u\n",
510 (unsigned int)in_data.length,
511 (unsigned int)min_aio_write_size ));
512 return NT_STATUS_RETRY;
515 /* Only do this on writes not using the write cache. */
516 if (lp_write_cache_size(SNUM(conn)) != 0) {
517 return NT_STATUS_RETRY;
520 if (outstanding_aio_calls >= aio_pending_size) {
521 DEBUG(3,("smb2: Already have %d aio "
522 "activities outstanding.\n",
523 outstanding_aio_calls ));
524 return NT_STATUS_RETRY;
527 if (!(aio_ex = create_aio_extra(smbreq->smb2req, fsp, 0))) {
528 return NT_STATUS_NO_MEMORY;
531 aio_ex->handle_completion = handle_aio_smb2_write_complete;
532 aio_ex->write_through = write_through;
534 init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
535 in_offset, (uint64_t)in_data.length, WRITE_LOCK,
536 &aio_ex->lock);
538 /* Take the lock until the AIO completes. */
539 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
540 TALLOC_FREE(aio_ex);
541 return NT_STATUS_FILE_LOCK_CONFLICT;
544 a = &aio_ex->acb;
546 /* Now set up the aio record for the write call. */
548 a->aio_fildes = fsp->fh->fd;
549 a->aio_buf = in_data.data;
550 a->aio_nbytes = in_data.length;
551 a->aio_offset = in_offset;
552 a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
553 a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
554 a->aio_sigevent.sigev_value.sival_ptr = aio_ex;
556 ret = SMB_VFS_AIO_WRITE(fsp, a);
557 if (ret == -1) {
558 DEBUG(3,("smb2: aio_write failed. "
559 "Error %s\n", strerror(errno) ));
560 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
561 TALLOC_FREE(aio_ex);
562 return NT_STATUS_RETRY;
565 outstanding_aio_calls++;
566 /* We don't need talloc_move here as both aio_ex and
567 * smbreq are children of smbreq->smb2req. */
568 aio_ex->smbreq = smbreq;
570 /* This should actually be improved to span the write. */
571 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WRITE);
572 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WRITE);
575 * We don't want to do write behind due to ownership
576 * issues of the request structs. Maybe add it if I
577 * figure those out. JRA.
580 DEBUG(10,("smb2: scheduled aio_write for file "
581 "%s, offset %.0f, len = %u (mid = %u) "
582 "outstanding_aio_calls = %d\n",
583 fsp_str_dbg(fsp),
584 (double)in_offset,
585 (unsigned int)in_data.length,
586 (unsigned int)aio_ex->smbreq->mid,
587 outstanding_aio_calls ));
589 return NT_STATUS_OK;
592 /****************************************************************************
593 Complete the read and return the data or error back to the client.
594 Returns errno or zero if all ok.
595 *****************************************************************************/
597 static int handle_aio_read_complete(struct aio_extra *aio_ex, int errcode)
599 int outsize;
600 char *outbuf = (char *)aio_ex->outbuf.data;
601 char *data = smb_buf(outbuf);
602 ssize_t nread = SMB_VFS_AIO_RETURN(aio_ex->fsp,&aio_ex->acb);
604 if (nread < 0) {
605 /* We're relying here on the fact that if the fd is
606 closed then the aio will complete and aio_return
607 will return an error. Hopefully this is
608 true.... JRA. */
610 DEBUG( 3,( "handle_aio_read_complete: file %s nread == %d. "
611 "Error = %s\n",
612 fsp_str_dbg(aio_ex->fsp), (int)nread, strerror(errcode)));
614 ERROR_NT(map_nt_error_from_unix(errcode));
615 outsize = srv_set_message(outbuf,0,0,true);
616 } else {
617 outsize = srv_set_message(outbuf,12,nread,False);
618 SSVAL(outbuf,smb_vwv2,0xFFFF); /* Remaining - must be * -1. */
619 SSVAL(outbuf,smb_vwv5,nread);
620 SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
621 SSVAL(outbuf,smb_vwv7,((nread >> 16) & 1));
622 SSVAL(smb_buf(outbuf),-2,nread);
624 aio_ex->fsp->fh->pos = aio_ex->acb.aio_offset + nread;
625 aio_ex->fsp->fh->position_information = aio_ex->fsp->fh->pos;
627 DEBUG( 3, ( "handle_aio_read_complete file %s max=%d "
628 "nread=%d\n",
629 fsp_str_dbg(aio_ex->fsp),
630 (int)aio_ex->acb.aio_nbytes, (int)nread ) );
633 smb_setlen(outbuf,outsize - 4);
634 show_msg(outbuf);
635 if (!srv_send_smb(smbd_server_fd(),outbuf,
636 true, aio_ex->smbreq->seqnum+1,
637 IS_CONN_ENCRYPTED(aio_ex->fsp->conn), NULL)) {
638 exit_server_cleanly("handle_aio_read_complete: srv_send_smb "
639 "failed.");
642 DEBUG(10,("handle_aio_read_complete: scheduled aio_read completed "
643 "for file %s, offset %.0f, len = %u\n",
644 fsp_str_dbg(aio_ex->fsp), (double)aio_ex->acb.aio_offset,
645 (unsigned int)nread ));
647 return errcode;
650 /****************************************************************************
651 Complete the write and return the data or error back to the client.
652 Returns error code or zero if all ok.
653 *****************************************************************************/
655 static int handle_aio_write_complete(struct aio_extra *aio_ex, int errcode)
657 files_struct *fsp = aio_ex->fsp;
658 char *outbuf = (char *)aio_ex->outbuf.data;
659 ssize_t numtowrite = aio_ex->acb.aio_nbytes;
660 ssize_t nwritten = SMB_VFS_AIO_RETURN(fsp,&aio_ex->acb);
662 if (fsp->aio_write_behind) {
663 if (nwritten != numtowrite) {
664 if (nwritten == -1) {
665 DEBUG(5,("handle_aio_write_complete: "
666 "aio_write_behind failed ! File %s "
667 "is corrupt ! Error %s\n",
668 fsp_str_dbg(fsp), strerror(errcode)));
669 } else {
670 DEBUG(0,("handle_aio_write_complete: "
671 "aio_write_behind failed ! File %s "
672 "is corrupt ! Wanted %u bytes but "
673 "only wrote %d\n", fsp_str_dbg(fsp),
674 (unsigned int)numtowrite,
675 (int)nwritten ));
676 errcode = EIO;
678 } else {
679 DEBUG(10,("handle_aio_write_complete: "
680 "aio_write_behind completed for file %s\n",
681 fsp_str_dbg(fsp)));
683 /* TODO: should no return 0 in case of an error !!! */
684 return 0;
687 /* We don't need outsize or set_message here as we've already set the
688 fixed size length when we set up the aio call. */
690 if(nwritten == -1) {
691 DEBUG( 3,( "handle_aio_write: file %s wanted %u bytes. "
692 "nwritten == %d. Error = %s\n",
693 fsp_str_dbg(fsp), (unsigned int)numtowrite,
694 (int)nwritten, strerror(errcode) ));
696 ERROR_NT(map_nt_error_from_unix(errcode));
697 srv_set_message(outbuf,0,0,true);
698 } else {
699 NTSTATUS status;
701 SSVAL(outbuf,smb_vwv2,nwritten);
702 SSVAL(outbuf,smb_vwv4,(nwritten>>16)&1);
703 if (nwritten < (ssize_t)numtowrite) {
704 SCVAL(outbuf,smb_rcls,ERRHRD);
705 SSVAL(outbuf,smb_err,ERRdiskfull);
708 DEBUG(3,("handle_aio_write: fnum=%d num=%d wrote=%d\n",
709 fsp->fnum, (int)numtowrite, (int)nwritten));
710 status = sync_file(fsp->conn,fsp, aio_ex->write_through);
711 if (!NT_STATUS_IS_OK(status)) {
712 errcode = errno;
713 ERROR_BOTH(map_nt_error_from_unix(errcode),
714 ERRHRD, ERRdiskfull);
715 srv_set_message(outbuf,0,0,true);
716 DEBUG(5,("handle_aio_write: sync_file for %s returned %s\n",
717 fsp_str_dbg(fsp), nt_errstr(status)));
720 aio_ex->fsp->fh->pos = aio_ex->acb.aio_offset + nwritten;
723 show_msg(outbuf);
724 if (!srv_send_smb(smbd_server_fd(),outbuf,
725 true, aio_ex->smbreq->seqnum+1,
726 IS_CONN_ENCRYPTED(fsp->conn),
727 NULL)) {
728 exit_server_cleanly("handle_aio_write: srv_send_smb failed.");
731 DEBUG(10,("handle_aio_write_complete: scheduled aio_write completed "
732 "for file %s, offset %.0f, requested %u, written = %u\n",
733 fsp_str_dbg(fsp), (double)aio_ex->acb.aio_offset,
734 (unsigned int)numtowrite, (unsigned int)nwritten ));
736 return errcode;
739 /****************************************************************************
740 Complete the read and return the data or error back to the client.
741 Returns errno or zero if all ok.
742 *****************************************************************************/
744 static int handle_aio_smb2_read_complete(struct aio_extra *aio_ex, int errcode)
746 NTSTATUS status;
747 struct tevent_req *subreq = aio_ex->smbreq->smb2req->subreq;
748 ssize_t nread = SMB_VFS_AIO_RETURN(aio_ex->fsp,&aio_ex->acb);
750 /* Common error or success code processing for async or sync
751 read returns. */
753 status = smb2_read_complete(subreq, nread, errcode);
755 if (nread > 0) {
756 aio_ex->fsp->fh->pos = aio_ex->acb.aio_offset + nread;
757 aio_ex->fsp->fh->position_information = aio_ex->fsp->fh->pos;
760 DEBUG(10,("smb2: scheduled aio_read completed "
761 "for file %s, offset %.0f, len = %u "
762 "(errcode = %d, NTSTATUS = %s)\n",
763 fsp_str_dbg(aio_ex->fsp),
764 (double)aio_ex->acb.aio_offset,
765 (unsigned int)nread,
766 errcode,
767 nt_errstr(status) ));
769 if (!NT_STATUS_IS_OK(status)) {
770 tevent_req_nterror(subreq, status);
773 tevent_req_done(subreq);
774 return errcode;
777 /****************************************************************************
778 Complete the SMB2 write and return the data or error back to the client.
779 Returns error code or zero if all ok.
780 *****************************************************************************/
782 static int handle_aio_smb2_write_complete(struct aio_extra *aio_ex, int errcode)
784 files_struct *fsp = aio_ex->fsp;
785 ssize_t numtowrite = aio_ex->acb.aio_nbytes;
786 ssize_t nwritten = SMB_VFS_AIO_RETURN(fsp,&aio_ex->acb);
787 struct tevent_req *subreq = aio_ex->smbreq->smb2req->subreq;
788 NTSTATUS status;
790 status = smb2_write_complete(subreq, nwritten, errcode);
792 DEBUG(10,("smb2: scheduled aio_write completed "
793 "for file %s, offset %.0f, requested %u, "
794 "written = %u (errcode = %d, NTSTATUS = %s)\n",
795 fsp_str_dbg(fsp),
796 (double)aio_ex->acb.aio_offset,
797 (unsigned int)numtowrite,
798 (unsigned int)nwritten,
799 errcode,
800 nt_errstr(status) ));
802 if (!NT_STATUS_IS_OK(status)) {
803 tevent_req_nterror(subreq, status);
806 tevent_req_done(subreq);
807 return errcode;
810 /****************************************************************************
811 Handle any aio completion. Returns True if finished (and sets *perr if err
812 was non-zero), False if not.
813 *****************************************************************************/
815 static bool handle_aio_completed(struct aio_extra *aio_ex, int *perr)
817 files_struct *fsp = NULL;
818 int err;
820 if(!aio_ex) {
821 DEBUG(3, ("handle_aio_completed: Non-existing aio_ex passed\n"));
822 return false;
825 fsp = aio_ex->fsp;
827 /* Ensure the operation has really completed. */
828 err = SMB_VFS_AIO_ERROR(fsp, &aio_ex->acb);
829 if (err == EINPROGRESS) {
830 DEBUG(10,( "handle_aio_completed: operation mid %llu still in "
831 "process for file %s\n",
832 (unsigned long long)aio_ex->smbreq->mid,
833 fsp_str_dbg(aio_ex->fsp)));
834 return False;
837 /* Unlock now we're done. */
838 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &aio_ex->lock);
840 if (err == ECANCELED) {
841 /* If error is ECANCELED then don't return anything to the
842 * client. */
843 DEBUG(10,( "handle_aio_completed: operation mid %llu"
844 " canceled\n",
845 (unsigned long long)aio_ex->smbreq->mid));
846 return True;
849 err = aio_ex->handle_completion(aio_ex, err);
850 if (err) {
851 *perr = err; /* Only save non-zero errors. */
854 return True;
857 /****************************************************************************
858 Handle any aio completion inline.
859 *****************************************************************************/
861 void smbd_aio_complete_aio_ex(struct aio_extra *aio_ex)
863 files_struct *fsp = NULL;
864 int ret = 0;
866 outstanding_aio_calls--;
868 DEBUG(10,("smbd_aio_complete_mid: mid[%llu]\n",
869 (unsigned long long)aio_ex->smbreq->mid));
871 fsp = aio_ex->fsp;
872 if (fsp == NULL) {
873 /* file was closed whilst I/O was outstanding. Just
874 * ignore. */
875 DEBUG( 3,( "smbd_aio_complete_mid: file closed whilst "
876 "aio outstanding (mid[%llu]).\n",
877 (unsigned long long)aio_ex->smbreq->mid));
878 return;
881 if (!handle_aio_completed(aio_ex, &ret)) {
882 return;
885 TALLOC_FREE(aio_ex);
888 /****************************************************************************
889 We're doing write behind and the client closed the file. Wait up to 30
890 seconds (my arbitrary choice) for the aio to complete. Return 0 if all writes
891 completed, errno to return if not.
892 *****************************************************************************/
894 #define SMB_TIME_FOR_AIO_COMPLETE_WAIT 29
896 int wait_for_aio_completion(files_struct *fsp)
898 struct aio_extra *aio_ex;
899 const SMB_STRUCT_AIOCB **aiocb_list;
900 int aio_completion_count = 0;
901 time_t start_time = time(NULL);
902 int seconds_left;
904 for (seconds_left = SMB_TIME_FOR_AIO_COMPLETE_WAIT;
905 seconds_left >= 0;) {
906 int err = 0;
907 int i;
908 struct timespec ts;
910 aio_completion_count = 0;
911 for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
912 if (aio_ex->fsp == fsp) {
913 aio_completion_count++;
917 if (!aio_completion_count) {
918 return 0;
921 DEBUG(3,("wait_for_aio_completion: waiting for %d aio events "
922 "to complete.\n", aio_completion_count ));
924 aiocb_list = SMB_MALLOC_ARRAY(const SMB_STRUCT_AIOCB *,
925 aio_completion_count);
926 if (!aiocb_list) {
927 return ENOMEM;
930 for( i = 0, aio_ex = aio_list_head;
931 aio_ex;
932 aio_ex = aio_ex->next) {
933 if (aio_ex->fsp == fsp) {
934 aiocb_list[i++] = &aio_ex->acb;
938 /* Now wait up to seconds_left for completion. */
939 ts.tv_sec = seconds_left;
940 ts.tv_nsec = 0;
942 DEBUG(10,("wait_for_aio_completion: %d events, doing a wait "
943 "of %d seconds.\n",
944 aio_completion_count, seconds_left ));
946 err = SMB_VFS_AIO_SUSPEND(fsp, aiocb_list,
947 aio_completion_count, &ts);
949 DEBUG(10,("wait_for_aio_completion: returned err = %d, "
950 "errno = %s\n", err, strerror(errno) ));
952 if (err == -1 && errno == EAGAIN) {
953 DEBUG(0,("wait_for_aio_completion: aio_suspend timed "
954 "out waiting for %d events after a wait of "
955 "%d seconds\n", aio_completion_count,
956 seconds_left));
957 /* Timeout. */
958 cancel_aio_by_fsp(fsp);
959 SAFE_FREE(aiocb_list);
960 return EIO;
963 /* One or more events might have completed - process them if
964 * so. */
965 for( i = 0; i < aio_completion_count; i++) {
966 aio_ex = (struct aio_extra *)aiocb_list[i]->aio_sigevent.sigev_value.sival_ptr;
968 if (!handle_aio_completed(aio_ex, &err)) {
969 continue;
971 TALLOC_FREE(aio_ex);
974 SAFE_FREE(aiocb_list);
975 seconds_left = SMB_TIME_FOR_AIO_COMPLETE_WAIT
976 - (time(NULL) - start_time);
979 /* We timed out - we don't know why. Return ret if already an error,
980 * else EIO. */
981 DEBUG(10,("wait_for_aio_completion: aio_suspend timed out waiting "
982 "for %d events\n",
983 aio_completion_count));
985 return EIO;
988 /****************************************************************************
989 Cancel any outstanding aio requests. The client doesn't care about the reply.
990 *****************************************************************************/
992 void cancel_aio_by_fsp(files_struct *fsp)
994 struct aio_extra *aio_ex;
996 for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
997 if (aio_ex->fsp == fsp) {
998 /* Unlock now we're done. */
999 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &aio_ex->lock);
1001 /* Don't delete the aio_extra record as we may have
1002 completed and don't yet know it. Just do the
1003 aio_cancel call and return. */
1004 SMB_VFS_AIO_CANCEL(fsp, &aio_ex->acb);
1005 aio_ex->fsp = NULL; /* fsp will be closed when we
1006 * return. */
1011 #else
1012 NTSTATUS schedule_aio_read_and_X(connection_struct *conn,
1013 struct smb_request *smbreq,
1014 files_struct *fsp, SMB_OFF_T startpos,
1015 size_t smb_maxcnt)
1017 return NT_STATUS_RETRY;
1020 NTSTATUS schedule_aio_write_and_X(connection_struct *conn,
1021 struct smb_request *smbreq,
1022 files_struct *fsp, char *data,
1023 SMB_OFF_T startpos,
1024 size_t numtowrite)
1026 return NT_STATUS_RETRY;
1029 NTSTATUS schedule_smb2_aio_read(connection_struct *conn,
1030 struct smb_request *smbreq,
1031 files_struct *fsp,
1032 char *inbuf,
1033 SMB_OFF_T startpos,
1034 size_t smb_maxcnt)
1036 return NT_STATUS_RETRY;
1039 NTSTATUS schedule_aio_smb2_write(connection_struct *conn,
1040 struct smb_request *smbreq,
1041 files_struct *fsp,
1042 uint64_t in_offset,
1043 DATA_BLOB in_data,
1044 bool write_through)
1046 return NT_STATUS_RETRY;
1049 void cancel_aio_by_fsp(files_struct *fsp)
1053 int wait_for_aio_completion(files_struct *fsp)
1055 return 0;
1058 void smbd_aio_complete_mid(uint64_t mid);
1060 #endif