source: vendor/current/source3/smbd/aio.c

Last change on this file was 989, checked in by Silvan Scherrer, 9 years ago

Samba Server: update vendor to version 4.4.7

File size: 26.9 KB
Line 
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.
6
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.
11
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.
16
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/>.
19*/
20
21#include "includes.h"
22#include "smbd/smbd.h"
23#include "smbd/globals.h"
24#include "../lib/util/tevent_ntstatus.h"
25#include "../lib/util/tevent_unix.h"
26#include "lib/tevent_wait.h"
27
28/****************************************************************************
29 Statics plus accessor functions.
30*****************************************************************************/
31
32static int outstanding_aio_calls;
33
34int get_outstanding_aio_calls(void)
35{
36 return outstanding_aio_calls;
37}
38
39void increment_outstanding_aio_calls(void)
40{
41 outstanding_aio_calls++;
42}
43
44void decrement_outstanding_aio_calls(void)
45{
46 outstanding_aio_calls--;
47}
48
49/****************************************************************************
50 The buffer we keep around whilst an aio request is in process.
51*****************************************************************************/
52
53struct aio_extra {
54 files_struct *fsp;
55 struct smb_request *smbreq;
56 DATA_BLOB outbuf;
57 struct lock_struct lock;
58 size_t nbyte;
59 off_t offset;
60 bool write_through;
61};
62
63/****************************************************************************
64 Accessor function to return write_through state.
65*****************************************************************************/
66
67bool aio_write_through_requested(struct aio_extra *aio_ex)
68{
69 return aio_ex->write_through;
70}
71
72static int aio_extra_destructor(struct aio_extra *aio_ex)
73{
74 decrement_outstanding_aio_calls();
75 return 0;
76}
77
78/****************************************************************************
79 Create the extended aio struct we must keep around for the lifetime
80 of the aio call.
81*****************************************************************************/
82
83static struct aio_extra *create_aio_extra(TALLOC_CTX *mem_ctx,
84 files_struct *fsp,
85 size_t buflen)
86{
87 struct aio_extra *aio_ex = talloc_zero(mem_ctx, struct aio_extra);
88
89 if (!aio_ex) {
90 return NULL;
91 }
92
93 /* The output buffer stored in the aio_ex is the start of
94 the smb return buffer. The buffer used in the acb
95 is the start of the reply data portion of that buffer. */
96
97 if (buflen) {
98 aio_ex->outbuf = data_blob_talloc(aio_ex, NULL, buflen);
99 if (!aio_ex->outbuf.data) {
100 TALLOC_FREE(aio_ex);
101 return NULL;
102 }
103 }
104 talloc_set_destructor(aio_ex, aio_extra_destructor);
105 aio_ex->fsp = fsp;
106 increment_outstanding_aio_calls();
107 return aio_ex;
108}
109
110struct aio_req_fsp_link {
111 files_struct *fsp;
112 struct tevent_req *req;
113};
114
115static int aio_del_req_from_fsp(struct aio_req_fsp_link *lnk)
116{
117 unsigned i;
118 files_struct *fsp = lnk->fsp;
119 struct tevent_req *req = lnk->req;
120
121 for (i=0; i<fsp->num_aio_requests; i++) {
122 if (fsp->aio_requests[i] == req) {
123 break;
124 }
125 }
126 if (i == fsp->num_aio_requests) {
127 DEBUG(1, ("req %p not found in fsp %p\n", req, fsp));
128 return 0;
129 }
130 fsp->num_aio_requests -= 1;
131 fsp->aio_requests[i] = fsp->aio_requests[fsp->num_aio_requests];
132
133 if (fsp->num_aio_requests == 0) {
134 tevent_wait_done(fsp->deferred_close);
135 }
136 return 0;
137}
138
139bool aio_add_req_to_fsp(files_struct *fsp, struct tevent_req *req)
140{
141 size_t array_len;
142 struct aio_req_fsp_link *lnk;
143
144 lnk = talloc(req, struct aio_req_fsp_link);
145 if (lnk == NULL) {
146 return false;
147 }
148
149 array_len = talloc_array_length(fsp->aio_requests);
150 if (array_len <= fsp->num_aio_requests) {
151 struct tevent_req **tmp;
152
153 tmp = talloc_realloc(
154 fsp, fsp->aio_requests, struct tevent_req *,
155 fsp->num_aio_requests+1);
156 if (tmp == NULL) {
157 TALLOC_FREE(lnk);
158 return false;
159 }
160 fsp->aio_requests = tmp;
161 }
162 fsp->aio_requests[fsp->num_aio_requests] = req;
163 fsp->num_aio_requests += 1;
164
165 lnk->fsp = fsp;
166 lnk->req = req;
167 talloc_set_destructor(lnk, aio_del_req_from_fsp);
168
169 return true;
170}
171
172static void aio_pread_smb1_done(struct tevent_req *req);
173
174/****************************************************************************
175 Set up an aio request from a SMBreadX call.
176*****************************************************************************/
177
178NTSTATUS schedule_aio_read_and_X(connection_struct *conn,
179 struct smb_request *smbreq,
180 files_struct *fsp, off_t startpos,
181 size_t smb_maxcnt)
182{
183 struct aio_extra *aio_ex;
184 size_t bufsize;
185 size_t min_aio_read_size = lp_aio_read_size(SNUM(conn));
186 struct tevent_req *req;
187
188 if (fsp->base_fsp != NULL) {
189 /* No AIO on streams yet */
190 DEBUG(10, ("AIO on streams not yet supported\n"));
191 return NT_STATUS_RETRY;
192 }
193
194 if ((!min_aio_read_size || (smb_maxcnt < min_aio_read_size))
195 && !SMB_VFS_AIO_FORCE(fsp)) {
196 /* Too small a read for aio request. */
197 DEBUG(10,("schedule_aio_read_and_X: read size (%u) too small "
198 "for minimum aio_read of %u\n",
199 (unsigned int)smb_maxcnt,
200 (unsigned int)min_aio_read_size ));
201 return NT_STATUS_RETRY;
202 }
203
204 /* Only do this on non-chained and non-chaining reads not using the
205 * write cache. */
206 if (req_is_in_chain(smbreq) || (lp_write_cache_size(SNUM(conn)) != 0)) {
207 return NT_STATUS_RETRY;
208 }
209
210 /* The following is safe from integer wrap as we've already checked
211 smb_maxcnt is 128k or less. Wct is 12 for read replies */
212
213 bufsize = smb_size + 12 * 2 + smb_maxcnt + 1 /* padding byte */;
214
215 if ((aio_ex = create_aio_extra(NULL, fsp, bufsize)) == NULL) {
216 DEBUG(10,("schedule_aio_read_and_X: malloc fail.\n"));
217 return NT_STATUS_NO_MEMORY;
218 }
219
220 construct_reply_common_req(smbreq, (char *)aio_ex->outbuf.data);
221 srv_set_message((char *)aio_ex->outbuf.data, 12, 0, True);
222 SCVAL(aio_ex->outbuf.data,smb_vwv0,0xFF); /* Never a chained reply. */
223 SCVAL(smb_buf(aio_ex->outbuf.data), 0, 0); /* padding byte */
224
225 init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
226 (uint64_t)startpos, (uint64_t)smb_maxcnt, READ_LOCK,
227 &aio_ex->lock);
228
229 /* Take the lock until the AIO completes. */
230 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
231 TALLOC_FREE(aio_ex);
232 return NT_STATUS_FILE_LOCK_CONFLICT;
233 }
234
235 aio_ex->nbyte = smb_maxcnt;
236 aio_ex->offset = startpos;
237
238 req = SMB_VFS_PREAD_SEND(aio_ex, fsp->conn->sconn->ev_ctx,
239 fsp,
240 smb_buf(aio_ex->outbuf.data) + 1 /* pad */,
241 smb_maxcnt, startpos);
242 if (req == NULL) {
243 DEBUG(0,("schedule_aio_read_and_X: aio_read failed. "
244 "Error %s\n", strerror(errno) ));
245 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
246 TALLOC_FREE(aio_ex);
247 return NT_STATUS_RETRY;
248 }
249 tevent_req_set_callback(req, aio_pread_smb1_done, aio_ex);
250
251 if (!aio_add_req_to_fsp(fsp, req)) {
252 DEBUG(1, ("Could not add req to fsp\n"));
253 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
254 TALLOC_FREE(aio_ex);
255 return NT_STATUS_RETRY;
256 }
257
258 aio_ex->smbreq = talloc_move(aio_ex, &smbreq);
259
260 DEBUG(10,("schedule_aio_read_and_X: scheduled aio_read for file %s, "
261 "offset %.0f, len = %u (mid = %u)\n",
262 fsp_str_dbg(fsp), (double)startpos, (unsigned int)smb_maxcnt,
263 (unsigned int)aio_ex->smbreq->mid ));
264
265 return NT_STATUS_OK;
266}
267
268static void aio_pread_smb1_done(struct tevent_req *req)
269{
270 struct aio_extra *aio_ex = tevent_req_callback_data(
271 req, struct aio_extra);
272 files_struct *fsp = aio_ex->fsp;
273 int outsize;
274 char *outbuf = (char *)aio_ex->outbuf.data;
275 ssize_t nread;
276 int err;
277
278 nread = SMB_VFS_PREAD_RECV(req, &err);
279 TALLOC_FREE(req);
280
281 DEBUG(10, ("pread_recv returned %d, err = %s\n", (int)nread,
282 (nread == -1) ? strerror(err) : "no error"));
283
284 if (fsp == NULL) {
285 DEBUG( 3, ("aio_pread_smb1_done: file closed whilst "
286 "aio outstanding (mid[%llu]).\n",
287 (unsigned long long)aio_ex->smbreq->mid));
288 TALLOC_FREE(aio_ex);
289 return;
290 }
291
292 /* Unlock now we're done. */
293 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &aio_ex->lock);
294
295 if (nread < 0) {
296 DEBUG( 3, ("handle_aio_read_complete: file %s nread == %d. "
297 "Error = %s\n", fsp_str_dbg(fsp), (int)nread,
298 strerror(err)));
299
300 ERROR_NT(map_nt_error_from_unix(err));
301 outsize = srv_set_message(outbuf,0,0,true);
302 } else {
303 outsize = setup_readX_header(outbuf, nread);
304
305 aio_ex->fsp->fh->pos = aio_ex->offset + nread;
306 aio_ex->fsp->fh->position_information = aio_ex->fsp->fh->pos;
307
308 DEBUG( 3, ("handle_aio_read_complete file %s max=%d "
309 "nread=%d\n", fsp_str_dbg(fsp),
310 (int)aio_ex->nbyte, (int)nread ) );
311
312 }
313 smb_setlen(outbuf, outsize - 4);
314 show_msg(outbuf);
315 if (!srv_send_smb(aio_ex->smbreq->xconn, outbuf,
316 true, aio_ex->smbreq->seqnum+1,
317 IS_CONN_ENCRYPTED(fsp->conn), NULL)) {
318 exit_server_cleanly("handle_aio_read_complete: srv_send_smb "
319 "failed.");
320 }
321
322 DEBUG(10, ("handle_aio_read_complete: scheduled aio_read completed "
323 "for file %s, offset %.0f, len = %u\n",
324 fsp_str_dbg(fsp), (double)aio_ex->offset,
325 (unsigned int)nread));
326
327 TALLOC_FREE(aio_ex);
328}
329
330struct pwrite_fsync_state {
331 struct tevent_context *ev;
332 files_struct *fsp;
333 bool write_through;
334 ssize_t nwritten;
335};
336
337static void pwrite_fsync_write_done(struct tevent_req *subreq);
338static void pwrite_fsync_sync_done(struct tevent_req *subreq);
339
340static struct tevent_req *pwrite_fsync_send(TALLOC_CTX *mem_ctx,
341 struct tevent_context *ev,
342 struct files_struct *fsp,
343 const void *data,
344 size_t n, off_t offset,
345 bool write_through)
346{
347 struct tevent_req *req, *subreq;
348 struct pwrite_fsync_state *state;
349
350 req = tevent_req_create(mem_ctx, &state, struct pwrite_fsync_state);
351 if (req == NULL) {
352 return NULL;
353 }
354 state->ev = ev;
355 state->fsp = fsp;
356 state->write_through = write_through;
357
358 subreq = SMB_VFS_PWRITE_SEND(state, ev, fsp, data, n, offset);
359 if (tevent_req_nomem(subreq, req)) {
360 return tevent_req_post(req, ev);
361 }
362 tevent_req_set_callback(subreq, pwrite_fsync_write_done, req);
363 return req;
364}
365
366static void pwrite_fsync_write_done(struct tevent_req *subreq)
367{
368 struct tevent_req *req = tevent_req_callback_data(
369 subreq, struct tevent_req);
370 struct pwrite_fsync_state *state = tevent_req_data(
371 req, struct pwrite_fsync_state);
372 connection_struct *conn = state->fsp->conn;
373 int err;
374 bool do_sync;
375
376 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &err);
377 TALLOC_FREE(subreq);
378 if (state->nwritten == -1) {
379 tevent_req_error(req, err);
380 return;
381 }
382
383 do_sync = (lp_strict_sync(SNUM(conn)) &&
384 (lp_sync_always(SNUM(conn)) || state->write_through));
385 if (!do_sync) {
386 tevent_req_done(req);
387 return;
388 }
389
390 subreq = SMB_VFS_FSYNC_SEND(state, state->ev, state->fsp);
391 if (tevent_req_nomem(subreq, req)) {
392 return;
393 }
394 tevent_req_set_callback(subreq, pwrite_fsync_sync_done, req);
395}
396
397static void pwrite_fsync_sync_done(struct tevent_req *subreq)
398{
399 struct tevent_req *req = tevent_req_callback_data(
400 subreq, struct tevent_req);
401 int ret, err;
402
403 ret = SMB_VFS_FSYNC_RECV(subreq, &err);
404 TALLOC_FREE(subreq);
405 if (ret == -1) {
406 tevent_req_error(req, err);
407 return;
408 }
409 tevent_req_done(req);
410}
411
412static ssize_t pwrite_fsync_recv(struct tevent_req *req, int *perr)
413{
414 struct pwrite_fsync_state *state = tevent_req_data(
415 req, struct pwrite_fsync_state);
416
417 if (tevent_req_is_unix_error(req, perr)) {
418 return -1;
419 }
420 return state->nwritten;
421}
422
423static void aio_pwrite_smb1_done(struct tevent_req *req);
424
425/****************************************************************************
426 Set up an aio request from a SMBwriteX call.
427*****************************************************************************/
428
429NTSTATUS schedule_aio_write_and_X(connection_struct *conn,
430 struct smb_request *smbreq,
431 files_struct *fsp, const char *data,
432 off_t startpos,
433 size_t numtowrite)
434{
435 struct aio_extra *aio_ex;
436 size_t bufsize;
437 size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
438 struct tevent_req *req;
439
440 if (fsp->base_fsp != NULL) {
441 /* No AIO on streams yet */
442 DEBUG(10, ("AIO on streams not yet supported\n"));
443 return NT_STATUS_RETRY;
444 }
445
446 if ((!min_aio_write_size || (numtowrite < min_aio_write_size))
447 && !SMB_VFS_AIO_FORCE(fsp)) {
448 /* Too small a write for aio request. */
449 DEBUG(10,("schedule_aio_write_and_X: write size (%u) too "
450 "small for minimum aio_write of %u\n",
451 (unsigned int)numtowrite,
452 (unsigned int)min_aio_write_size ));
453 return NT_STATUS_RETRY;
454 }
455
456 /* Only do this on non-chained and non-chaining writes not using the
457 * write cache. */
458 if (req_is_in_chain(smbreq) || (lp_write_cache_size(SNUM(conn)) != 0)) {
459 return NT_STATUS_RETRY;
460 }
461
462 bufsize = smb_size + 6*2;
463
464 if (!(aio_ex = create_aio_extra(NULL, fsp, bufsize))) {
465 DEBUG(0,("schedule_aio_write_and_X: malloc fail.\n"));
466 return NT_STATUS_NO_MEMORY;
467 }
468 aio_ex->write_through = BITSETW(smbreq->vwv+7,0);
469
470 construct_reply_common_req(smbreq, (char *)aio_ex->outbuf.data);
471 srv_set_message((char *)aio_ex->outbuf.data, 6, 0, True);
472 SCVAL(aio_ex->outbuf.data,smb_vwv0,0xFF); /* Never a chained reply. */
473
474 init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
475 (uint64_t)startpos, (uint64_t)numtowrite, WRITE_LOCK,
476 &aio_ex->lock);
477
478 /* Take the lock until the AIO completes. */
479 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
480 TALLOC_FREE(aio_ex);
481 return NT_STATUS_FILE_LOCK_CONFLICT;
482 }
483
484 aio_ex->nbyte = numtowrite;
485 aio_ex->offset = startpos;
486
487 req = pwrite_fsync_send(aio_ex, fsp->conn->sconn->ev_ctx, fsp,
488 data, numtowrite, startpos,
489 aio_ex->write_through);
490 if (req == NULL) {
491 DEBUG(3,("schedule_aio_wrote_and_X: aio_write failed. "
492 "Error %s\n", strerror(errno) ));
493 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
494 TALLOC_FREE(aio_ex);
495 return NT_STATUS_RETRY;
496 }
497 tevent_req_set_callback(req, aio_pwrite_smb1_done, aio_ex);
498
499 if (!aio_add_req_to_fsp(fsp, req)) {
500 DEBUG(1, ("Could not add req to fsp\n"));
501 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
502 TALLOC_FREE(aio_ex);
503 return NT_STATUS_RETRY;
504 }
505
506 aio_ex->smbreq = talloc_move(aio_ex, &smbreq);
507
508 /* This should actually be improved to span the write. */
509 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WRITE);
510 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WRITE);
511
512 if (!aio_ex->write_through && !lp_sync_always(SNUM(fsp->conn))
513 && fsp->aio_write_behind) {
514 /* Lie to the client and immediately claim we finished the
515 * write. */
516 SSVAL(aio_ex->outbuf.data,smb_vwv2,numtowrite);
517 SSVAL(aio_ex->outbuf.data,smb_vwv4,(numtowrite>>16)&1);
518 show_msg((char *)aio_ex->outbuf.data);
519 if (!srv_send_smb(aio_ex->smbreq->xconn,
520 (char *)aio_ex->outbuf.data,
521 true, aio_ex->smbreq->seqnum+1,
522 IS_CONN_ENCRYPTED(fsp->conn),
523 &aio_ex->smbreq->pcd)) {
524 exit_server_cleanly("schedule_aio_write_and_X: "
525 "srv_send_smb failed.");
526 }
527 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write "
528 "behind for file %s\n", fsp_str_dbg(fsp)));
529 }
530
531 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write for file "
532 "%s, offset %.0f, len = %u (mid = %u) "
533 "outstanding_aio_calls = %d\n",
534 fsp_str_dbg(fsp), (double)startpos, (unsigned int)numtowrite,
535 (unsigned int)aio_ex->smbreq->mid,
536 get_outstanding_aio_calls() ));
537
538 return NT_STATUS_OK;
539}
540
541static void aio_pwrite_smb1_done(struct tevent_req *req)
542{
543 struct aio_extra *aio_ex = tevent_req_callback_data(
544 req, struct aio_extra);
545 files_struct *fsp = aio_ex->fsp;
546 char *outbuf = (char *)aio_ex->outbuf.data;
547 ssize_t numtowrite = aio_ex->nbyte;
548 ssize_t nwritten;
549 int err;
550
551 nwritten = pwrite_fsync_recv(req, &err);
552 TALLOC_FREE(req);
553
554 DEBUG(10, ("pwrite_recv returned %d, err = %s\n", (int)nwritten,
555 (nwritten == -1) ? strerror(err) : "no error"));
556
557 if (fsp == NULL) {
558 DEBUG( 3, ("aio_pwrite_smb1_done: file closed whilst "
559 "aio outstanding (mid[%llu]).\n",
560 (unsigned long long)aio_ex->smbreq->mid));
561 TALLOC_FREE(aio_ex);
562 return;
563 }
564
565 /* Unlock now we're done. */
566 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &aio_ex->lock);
567
568 mark_file_modified(fsp);
569
570 if (fsp->aio_write_behind) {
571
572 if (nwritten != numtowrite) {
573 if (nwritten == -1) {
574 DEBUG(5,("handle_aio_write_complete: "
575 "aio_write_behind failed ! File %s "
576 "is corrupt ! Error %s\n",
577 fsp_str_dbg(fsp), strerror(err)));
578 } else {
579 DEBUG(0,("handle_aio_write_complete: "
580 "aio_write_behind failed ! File %s "
581 "is corrupt ! Wanted %u bytes but "
582 "only wrote %d\n", fsp_str_dbg(fsp),
583 (unsigned int)numtowrite,
584 (int)nwritten ));
585 }
586 } else {
587 DEBUG(10,("handle_aio_write_complete: "
588 "aio_write_behind completed for file %s\n",
589 fsp_str_dbg(fsp)));
590 }
591 /* TODO: should no return success in case of an error !!! */
592 TALLOC_FREE(aio_ex);
593 return;
594 }
595
596 /* We don't need outsize or set_message here as we've already set the
597 fixed size length when we set up the aio call. */
598
599 if (nwritten == -1) {
600 DEBUG(3, ("handle_aio_write: file %s wanted %u bytes. "
601 "nwritten == %d. Error = %s\n",
602 fsp_str_dbg(fsp), (unsigned int)numtowrite,
603 (int)nwritten, strerror(err)));
604
605 ERROR_NT(map_nt_error_from_unix(err));
606 srv_set_message(outbuf,0,0,true);
607 } else {
608 SSVAL(outbuf,smb_vwv2,nwritten);
609 SSVAL(outbuf,smb_vwv4,(nwritten>>16)&1);
610 if (nwritten < (ssize_t)numtowrite) {
611 SCVAL(outbuf,smb_rcls,ERRHRD);
612 SSVAL(outbuf,smb_err,ERRdiskfull);
613 }
614
615 DEBUG(3,("handle_aio_write: %s, num=%d wrote=%d\n",
616 fsp_fnum_dbg(fsp), (int)numtowrite, (int)nwritten));
617
618 aio_ex->fsp->fh->pos = aio_ex->offset + nwritten;
619 }
620
621 show_msg(outbuf);
622 if (!srv_send_smb(aio_ex->smbreq->xconn, outbuf,
623 true, aio_ex->smbreq->seqnum+1,
624 IS_CONN_ENCRYPTED(fsp->conn),
625 NULL)) {
626 exit_server_cleanly("handle_aio_write_complete: "
627 "srv_send_smb failed.");
628 }
629
630 DEBUG(10, ("handle_aio_write_complete: scheduled aio_write completed "
631 "for file %s, offset %.0f, requested %u, written = %u\n",
632 fsp_str_dbg(fsp), (double)aio_ex->offset,
633 (unsigned int)numtowrite, (unsigned int)nwritten));
634
635 TALLOC_FREE(aio_ex);
636}
637
638bool cancel_smb2_aio(struct smb_request *smbreq)
639{
640 struct smbd_smb2_request *smb2req = smbreq->smb2req;
641 struct aio_extra *aio_ex = NULL;
642
643 if (smb2req) {
644 aio_ex = talloc_get_type(smbreq->async_priv,
645 struct aio_extra);
646 }
647
648 if (aio_ex == NULL) {
649 return false;
650 }
651
652 if (aio_ex->fsp == NULL) {
653 return false;
654 }
655
656 /*
657 * We let the aio request run. Setting fsp to NULL has the
658 * effect that the _done routines don't send anything out.
659 */
660
661 aio_ex->fsp = NULL;
662 return true;
663}
664
665static void aio_pread_smb2_done(struct tevent_req *req);
666
667/****************************************************************************
668 Set up an aio request from a SMB2 read call.
669*****************************************************************************/
670
671NTSTATUS schedule_smb2_aio_read(connection_struct *conn,
672 struct smb_request *smbreq,
673 files_struct *fsp,
674 TALLOC_CTX *ctx,
675 DATA_BLOB *preadbuf,
676 off_t startpos,
677 size_t smb_maxcnt)
678{
679 struct aio_extra *aio_ex;
680 size_t min_aio_read_size = lp_aio_read_size(SNUM(conn));
681 struct tevent_req *req;
682
683 if (fsp->base_fsp != NULL) {
684 /* No AIO on streams yet */
685 DEBUG(10, ("AIO on streams not yet supported\n"));
686 return NT_STATUS_RETRY;
687 }
688
689 if (fsp->op == NULL) {
690 /* No AIO on internal opens. */
691 return NT_STATUS_RETRY;
692 }
693
694 if ((!min_aio_read_size || (smb_maxcnt < min_aio_read_size))
695 && !SMB_VFS_AIO_FORCE(fsp)) {
696 /* Too small a read for aio request. */
697 DEBUG(10,("smb2: read size (%u) too small "
698 "for minimum aio_read of %u\n",
699 (unsigned int)smb_maxcnt,
700 (unsigned int)min_aio_read_size ));
701 return NT_STATUS_RETRY;
702 }
703
704 /* Only do this on reads not using the write cache. */
705 if (lp_write_cache_size(SNUM(conn)) != 0) {
706 return NT_STATUS_RETRY;
707 }
708
709 /* Create the out buffer. */
710 *preadbuf = data_blob_talloc(ctx, NULL, smb_maxcnt);
711 if (preadbuf->data == NULL) {
712 return NT_STATUS_NO_MEMORY;
713 }
714
715 if (!(aio_ex = create_aio_extra(smbreq->smb2req, fsp, 0))) {
716 return NT_STATUS_NO_MEMORY;
717 }
718
719 init_strict_lock_struct(fsp, fsp->op->global->open_persistent_id,
720 (uint64_t)startpos, (uint64_t)smb_maxcnt, READ_LOCK,
721 &aio_ex->lock);
722
723 /* Take the lock until the AIO completes. */
724 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
725 TALLOC_FREE(aio_ex);
726 return NT_STATUS_FILE_LOCK_CONFLICT;
727 }
728
729 aio_ex->nbyte = smb_maxcnt;
730 aio_ex->offset = startpos;
731
732 req = SMB_VFS_PREAD_SEND(aio_ex, fsp->conn->sconn->ev_ctx, fsp,
733 preadbuf->data, smb_maxcnt, startpos);
734 if (req == NULL) {
735 DEBUG(0, ("smb2: SMB_VFS_PREAD_SEND failed. "
736 "Error %s\n", strerror(errno)));
737 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
738 TALLOC_FREE(aio_ex);
739 return NT_STATUS_RETRY;
740 }
741 tevent_req_set_callback(req, aio_pread_smb2_done, aio_ex);
742
743 if (!aio_add_req_to_fsp(fsp, req)) {
744 DEBUG(1, ("Could not add req to fsp\n"));
745 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
746 TALLOC_FREE(aio_ex);
747 return NT_STATUS_RETRY;
748 }
749
750 /* We don't need talloc_move here as both aio_ex and
751 * smbreq are children of smbreq->smb2req. */
752 aio_ex->smbreq = smbreq;
753 smbreq->async_priv = aio_ex;
754
755 DEBUG(10,("smb2: scheduled aio_read for file %s, "
756 "offset %.0f, len = %u (mid = %u)\n",
757 fsp_str_dbg(fsp), (double)startpos, (unsigned int)smb_maxcnt,
758 (unsigned int)aio_ex->smbreq->mid ));
759
760 return NT_STATUS_OK;
761}
762
763static void aio_pread_smb2_done(struct tevent_req *req)
764{
765 struct aio_extra *aio_ex = tevent_req_callback_data(
766 req, struct aio_extra);
767 struct tevent_req *subreq = aio_ex->smbreq->smb2req->subreq;
768 files_struct *fsp = aio_ex->fsp;
769 NTSTATUS status;
770 ssize_t nread;
771 int err = 0;
772
773 nread = SMB_VFS_PREAD_RECV(req, &err);
774 TALLOC_FREE(req);
775
776 DEBUG(10, ("pread_recv returned %d, err = %s\n", (int)nread,
777 (nread == -1) ? strerror(err) : "no error"));
778
779 if (fsp == NULL) {
780 DEBUG(3, ("%s: request cancelled (mid[%ju])\n",
781 __func__, (uintmax_t)aio_ex->smbreq->mid));
782 TALLOC_FREE(aio_ex);
783 tevent_req_nterror(subreq, NT_STATUS_INTERNAL_ERROR);
784 return;
785 }
786
787 /* Unlock now we're done. */
788 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &aio_ex->lock);
789
790 /* Common error or success code processing for async or sync
791 read returns. */
792
793 status = smb2_read_complete(subreq, nread, err);
794
795 if (nread > 0) {
796 fsp->fh->pos = aio_ex->offset + nread;
797 fsp->fh->position_information = fsp->fh->pos;
798 }
799
800 DEBUG(10, ("smb2: scheduled aio_read completed "
801 "for file %s, offset %.0f, len = %u "
802 "(errcode = %d, NTSTATUS = %s)\n",
803 fsp_str_dbg(aio_ex->fsp),
804 (double)aio_ex->offset,
805 (unsigned int)nread,
806 err, nt_errstr(status)));
807
808 if (!NT_STATUS_IS_OK(status)) {
809 tevent_req_nterror(subreq, status);
810 return;
811 }
812 tevent_req_done(subreq);
813}
814
815static void aio_pwrite_smb2_done(struct tevent_req *req);
816
817/****************************************************************************
818 Set up an aio request from a SMB2write call.
819*****************************************************************************/
820
821NTSTATUS schedule_aio_smb2_write(connection_struct *conn,
822 struct smb_request *smbreq,
823 files_struct *fsp,
824 uint64_t in_offset,
825 DATA_BLOB in_data,
826 bool write_through)
827{
828 struct aio_extra *aio_ex = NULL;
829 size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
830 struct tevent_req *req;
831
832 if (fsp->base_fsp != NULL) {
833 /* No AIO on streams yet */
834 DEBUG(10, ("AIO on streams not yet supported\n"));
835 return NT_STATUS_RETRY;
836 }
837
838 if (fsp->op == NULL) {
839 /* No AIO on internal opens. */
840 return NT_STATUS_RETRY;
841 }
842
843 if ((!min_aio_write_size || (in_data.length < min_aio_write_size))
844 && !SMB_VFS_AIO_FORCE(fsp)) {
845 /* Too small a write for aio request. */
846 DEBUG(10,("smb2: write size (%u) too "
847 "small for minimum aio_write of %u\n",
848 (unsigned int)in_data.length,
849 (unsigned int)min_aio_write_size ));
850 return NT_STATUS_RETRY;
851 }
852
853 /* Only do this on writes not using the write cache. */
854 if (lp_write_cache_size(SNUM(conn)) != 0) {
855 return NT_STATUS_RETRY;
856 }
857
858 if (smbreq->unread_bytes) {
859 /* Can't do async with recvfile. */
860 return NT_STATUS_RETRY;
861 }
862
863 if (!(aio_ex = create_aio_extra(smbreq->smb2req, fsp, 0))) {
864 return NT_STATUS_NO_MEMORY;
865 }
866
867 aio_ex->write_through = write_through;
868
869 init_strict_lock_struct(fsp, fsp->op->global->open_persistent_id,
870 in_offset, (uint64_t)in_data.length, WRITE_LOCK,
871 &aio_ex->lock);
872
873 /* Take the lock until the AIO completes. */
874 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
875 TALLOC_FREE(aio_ex);
876 return NT_STATUS_FILE_LOCK_CONFLICT;
877 }
878
879 aio_ex->nbyte = in_data.length;
880 aio_ex->offset = in_offset;
881
882 req = pwrite_fsync_send(aio_ex, fsp->conn->sconn->ev_ctx, fsp,
883 in_data.data, in_data.length, in_offset,
884 write_through);
885 if (req == NULL) {
886 DEBUG(3, ("smb2: SMB_VFS_PWRITE_SEND failed. "
887 "Error %s\n", strerror(errno)));
888 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
889 TALLOC_FREE(aio_ex);
890 return NT_STATUS_RETRY;
891 }
892 tevent_req_set_callback(req, aio_pwrite_smb2_done, aio_ex);
893
894 if (!aio_add_req_to_fsp(fsp, req)) {
895 DEBUG(1, ("Could not add req to fsp\n"));
896 SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
897 TALLOC_FREE(aio_ex);
898 return NT_STATUS_RETRY;
899 }
900
901 /* We don't need talloc_move here as both aio_ex and
902 * smbreq are children of smbreq->smb2req. */
903 aio_ex->smbreq = smbreq;
904 smbreq->async_priv = aio_ex;
905
906 /* This should actually be improved to span the write. */
907 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WRITE);
908 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WRITE);
909
910 /*
911 * We don't want to do write behind due to ownership
912 * issues of the request structs. Maybe add it if I
913 * figure those out. JRA.
914 */
915
916 DEBUG(10,("smb2: scheduled aio_write for file "
917 "%s, offset %.0f, len = %u (mid = %u) "
918 "outstanding_aio_calls = %d\n",
919 fsp_str_dbg(fsp),
920 (double)in_offset,
921 (unsigned int)in_data.length,
922 (unsigned int)aio_ex->smbreq->mid,
923 get_outstanding_aio_calls() ));
924
925 return NT_STATUS_OK;
926}
927
928static void aio_pwrite_smb2_done(struct tevent_req *req)
929{
930 struct aio_extra *aio_ex = tevent_req_callback_data(
931 req, struct aio_extra);
932 ssize_t numtowrite = aio_ex->nbyte;
933 struct tevent_req *subreq = aio_ex->smbreq->smb2req->subreq;
934 files_struct *fsp = aio_ex->fsp;
935 NTSTATUS status;
936 ssize_t nwritten;
937 int err = 0;
938
939 nwritten = pwrite_fsync_recv(req, &err);
940 TALLOC_FREE(req);
941
942 DEBUG(10, ("pwrite_recv returned %d, err = %s\n", (int)nwritten,
943 (nwritten == -1) ? strerror(err) : "no error"));
944
945 if (fsp == NULL) {
946 DEBUG(3, ("%s: request cancelled (mid[%ju])\n",
947 __func__, (uintmax_t)aio_ex->smbreq->mid));
948 TALLOC_FREE(aio_ex);
949 tevent_req_nterror(subreq, NT_STATUS_INTERNAL_ERROR);
950 return;
951 }
952
953 /* Unlock now we're done. */
954 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &aio_ex->lock);
955
956 mark_file_modified(fsp);
957
958 status = smb2_write_complete_nosync(subreq, nwritten, err);
959
960 DEBUG(10, ("smb2: scheduled aio_write completed "
961 "for file %s, offset %.0f, requested %u, "
962 "written = %u (errcode = %d, NTSTATUS = %s)\n",
963 fsp_str_dbg(fsp),
964 (double)aio_ex->offset,
965 (unsigned int)numtowrite,
966 (unsigned int)nwritten,
967 err, nt_errstr(status)));
968
969 if (!NT_STATUS_IS_OK(status)) {
970 tevent_req_nterror(subreq, status);
971 return;
972 }
973 tevent_req_done(subreq);
974}
Note: See TracBrowser for help on using the repository browser.