s3: smbd: Fix schedule_aio_smb2_write() to allow the last write in a compound to...
[Samba.git] / source3 / smbd / smb2_aio.c
blob6b04063ba30d6f1f0ebdbd9fb9da64272aa45657
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"
25 #include "../lib/util/tevent_unix.h"
27 /****************************************************************************
28 Accessor function to return write_through state.
29 *****************************************************************************/
31 bool aio_write_through_requested(struct aio_extra *aio_ex)
33 return aio_ex->write_through;
36 /****************************************************************************
37 Create the extended aio struct we must keep around for the lifetime
38 of the aio call.
39 *****************************************************************************/
41 struct aio_extra *create_aio_extra(TALLOC_CTX *mem_ctx,
42 files_struct *fsp,
43 size_t buflen)
45 struct aio_extra *aio_ex = talloc_zero(mem_ctx, struct aio_extra);
47 if (!aio_ex) {
48 return NULL;
51 /* The output buffer stored in the aio_ex is the start of
52 the smb return buffer. The buffer used in the acb
53 is the start of the reply data portion of that buffer. */
55 if (buflen) {
56 aio_ex->outbuf = data_blob_talloc(aio_ex, NULL, buflen);
57 if (!aio_ex->outbuf.data) {
58 TALLOC_FREE(aio_ex);
59 return NULL;
62 aio_ex->fsp = fsp;
63 return aio_ex;
66 struct aio_req_fsp_link {
67 files_struct *fsp;
68 struct tevent_req *req;
71 static int aio_del_req_from_fsp(struct aio_req_fsp_link *lnk)
73 unsigned i;
74 files_struct *fsp = lnk->fsp;
75 struct tevent_req *req = lnk->req;
77 for (i=0; i<fsp->num_aio_requests; i++) {
78 if (fsp->aio_requests[i] == req) {
79 break;
82 if (i == fsp->num_aio_requests) {
83 DEBUG(1, ("req %p not found in fsp %p\n", req, fsp));
84 return 0;
86 fsp->num_aio_requests -= 1;
87 fsp->aio_requests[i] = fsp->aio_requests[fsp->num_aio_requests];
89 if (fsp->num_aio_requests == 0) {
90 TALLOC_FREE(fsp->aio_requests);
92 return 0;
95 bool aio_add_req_to_fsp(files_struct *fsp, struct tevent_req *req)
97 size_t array_len;
98 struct aio_req_fsp_link *lnk;
100 lnk = talloc(req, struct aio_req_fsp_link);
101 if (lnk == NULL) {
102 return false;
105 array_len = talloc_array_length(fsp->aio_requests);
106 if (array_len <= fsp->num_aio_requests) {
107 struct tevent_req **tmp;
109 if (fsp->num_aio_requests + 10 < 10) {
110 /* Integer wrap. */
111 TALLOC_FREE(lnk);
112 return false;
116 * Allocate in blocks of 10 so we don't allocate
117 * on every aio request.
119 tmp = talloc_realloc(
120 fsp, fsp->aio_requests, struct tevent_req *,
121 fsp->num_aio_requests+10);
122 if (tmp == NULL) {
123 TALLOC_FREE(lnk);
124 return false;
126 fsp->aio_requests = tmp;
128 fsp->aio_requests[fsp->num_aio_requests] = req;
129 fsp->num_aio_requests += 1;
131 lnk->fsp = fsp;
132 lnk->req = req;
133 talloc_set_destructor(lnk, aio_del_req_from_fsp);
135 return true;
138 struct pwrite_fsync_state {
139 struct tevent_context *ev;
140 files_struct *fsp;
141 bool write_through;
142 ssize_t nwritten;
145 static void pwrite_fsync_write_done(struct tevent_req *subreq);
146 static void pwrite_fsync_sync_done(struct tevent_req *subreq);
148 struct tevent_req *pwrite_fsync_send(TALLOC_CTX *mem_ctx,
149 struct tevent_context *ev,
150 struct files_struct *fsp,
151 const void *data,
152 size_t n, off_t offset,
153 bool write_through)
155 struct tevent_req *req, *subreq;
156 struct pwrite_fsync_state *state;
157 bool ok;
159 req = tevent_req_create(mem_ctx, &state, struct pwrite_fsync_state);
160 if (req == NULL) {
161 return NULL;
163 state->ev = ev;
164 state->fsp = fsp;
165 state->write_through = write_through;
167 ok = vfs_valid_pwrite_range(offset, n);
168 if (!ok) {
169 tevent_req_error(req, EINVAL);
170 return tevent_req_post(req, ev);
173 if (n == 0) {
174 tevent_req_done(req);
175 return tevent_req_post(req, ev);
178 subreq = SMB_VFS_PWRITE_SEND(state, ev, fsp, data, n, offset);
179 if (tevent_req_nomem(subreq, req)) {
180 return tevent_req_post(req, ev);
182 tevent_req_set_callback(subreq, pwrite_fsync_write_done, req);
183 return req;
186 static void pwrite_fsync_write_done(struct tevent_req *subreq)
188 struct tevent_req *req = tevent_req_callback_data(
189 subreq, struct tevent_req);
190 struct pwrite_fsync_state *state = tevent_req_data(
191 req, struct pwrite_fsync_state);
192 connection_struct *conn = state->fsp->conn;
193 bool do_sync;
194 struct vfs_aio_state vfs_aio_state;
196 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &vfs_aio_state);
197 TALLOC_FREE(subreq);
198 if (state->nwritten == -1) {
199 tevent_req_error(req, vfs_aio_state.error);
200 return;
203 do_sync = (lp_strict_sync(SNUM(conn)) &&
204 (lp_sync_always(SNUM(conn)) || state->write_through));
205 if (!do_sync) {
206 tevent_req_done(req);
207 return;
210 subreq = SMB_VFS_FSYNC_SEND(state, state->ev, state->fsp);
211 if (tevent_req_nomem(subreq, req)) {
212 return;
214 tevent_req_set_callback(subreq, pwrite_fsync_sync_done, req);
217 static void pwrite_fsync_sync_done(struct tevent_req *subreq)
219 struct tevent_req *req = tevent_req_callback_data(
220 subreq, struct tevent_req);
221 int ret;
222 struct vfs_aio_state vfs_aio_state;
224 ret = SMB_VFS_FSYNC_RECV(subreq, &vfs_aio_state);
225 TALLOC_FREE(subreq);
226 if (ret == -1) {
227 tevent_req_error(req, vfs_aio_state.error);
228 return;
230 tevent_req_done(req);
233 ssize_t pwrite_fsync_recv(struct tevent_req *req, int *perr)
235 struct pwrite_fsync_state *state = tevent_req_data(
236 req, struct pwrite_fsync_state);
238 if (tevent_req_is_unix_error(req, perr)) {
239 return -1;
241 return state->nwritten;
244 bool cancel_smb2_aio(struct smb_request *smbreq)
246 struct smbd_smb2_request *smb2req = smbreq->smb2req;
247 struct aio_extra *aio_ex = NULL;
249 if (smb2req) {
250 aio_ex = talloc_get_type(smbreq->async_priv,
251 struct aio_extra);
254 if (aio_ex == NULL) {
255 return false;
258 if (aio_ex->fsp == NULL) {
259 return false;
263 * We let the aio request run and don't try to cancel it which means
264 * processing of the SMB2 request must continue as normal, cf MS-SMB2
265 * 3.3.5.16:
267 * If the target request is not successfully canceled, processing of
268 * the target request MUST continue and no response is sent to the
269 * cancel request.
272 return false;
275 static void aio_pread_smb2_done(struct tevent_req *req);
277 /****************************************************************************
278 Set up an aio request from a SMB2 read call.
279 *****************************************************************************/
281 NTSTATUS schedule_smb2_aio_read(connection_struct *conn,
282 struct smb_request *smbreq,
283 files_struct *fsp,
284 TALLOC_CTX *ctx,
285 DATA_BLOB *preadbuf,
286 off_t startpos,
287 size_t smb_maxcnt)
289 struct aio_extra *aio_ex;
290 size_t min_aio_read_size = lp_aio_read_size(SNUM(conn));
291 struct tevent_req *req;
292 bool ok;
294 ok = vfs_valid_pread_range(startpos, smb_maxcnt);
295 if (!ok) {
296 return NT_STATUS_INVALID_PARAMETER;
299 if (fsp_is_alternate_stream(fsp)) {
300 DEBUG(10, ("AIO on streams not yet supported\n"));
301 return NT_STATUS_RETRY;
304 if (fsp->op == NULL) {
305 /* No AIO on internal opens. */
306 return NT_STATUS_RETRY;
309 if ((!min_aio_read_size || (smb_maxcnt < min_aio_read_size))
310 && !SMB_VFS_AIO_FORCE(fsp)) {
311 /* Too small a read for aio request. */
312 DEBUG(10,("smb2: read size (%u) too small "
313 "for minimum aio_read of %u\n",
314 (unsigned int)smb_maxcnt,
315 (unsigned int)min_aio_read_size ));
316 return NT_STATUS_RETRY;
319 if (smbd_smb2_is_compound(smbreq->smb2req)) {
320 return NT_STATUS_RETRY;
323 /* Create the out buffer. */
324 *preadbuf = data_blob_talloc(ctx, NULL, smb_maxcnt);
325 if (preadbuf->data == NULL) {
326 return NT_STATUS_NO_MEMORY;
329 if (!(aio_ex = create_aio_extra(smbreq->smb2req, fsp, 0))) {
330 return NT_STATUS_NO_MEMORY;
333 init_strict_lock_struct(fsp,
334 fsp->op->global->open_persistent_id,
335 (uint64_t)startpos,
336 (uint64_t)smb_maxcnt,
337 READ_LOCK,
338 lp_posix_cifsu_locktype(fsp),
339 &aio_ex->lock);
341 /* Take the lock until the AIO completes. */
342 if (!SMB_VFS_STRICT_LOCK_CHECK(conn, fsp, &aio_ex->lock)) {
343 TALLOC_FREE(aio_ex);
344 return NT_STATUS_FILE_LOCK_CONFLICT;
347 aio_ex->nbyte = smb_maxcnt;
348 aio_ex->offset = startpos;
350 req = SMB_VFS_PREAD_SEND(aio_ex, fsp->conn->sconn->ev_ctx, fsp,
351 preadbuf->data, smb_maxcnt, startpos);
352 if (req == NULL) {
353 DEBUG(0, ("smb2: SMB_VFS_PREAD_SEND failed. "
354 "Error %s\n", strerror(errno)));
355 TALLOC_FREE(aio_ex);
356 return NT_STATUS_RETRY;
358 tevent_req_set_callback(req, aio_pread_smb2_done, aio_ex);
360 if (!aio_add_req_to_fsp(fsp, req)) {
361 DEBUG(1, ("Could not add req to fsp\n"));
362 TALLOC_FREE(aio_ex);
363 return NT_STATUS_RETRY;
366 /* We don't need talloc_move here as both aio_ex and
367 * smbreq are children of smbreq->smb2req. */
368 aio_ex->smbreq = smbreq;
369 smbreq->async_priv = aio_ex;
371 DEBUG(10,("smb2: scheduled aio_read for file %s, "
372 "offset %.0f, len = %u (mid = %u)\n",
373 fsp_str_dbg(fsp), (double)startpos, (unsigned int)smb_maxcnt,
374 (unsigned int)aio_ex->smbreq->mid ));
376 return NT_STATUS_OK;
379 static void aio_pread_smb2_done(struct tevent_req *req)
381 struct aio_extra *aio_ex = tevent_req_callback_data(
382 req, struct aio_extra);
383 struct tevent_req *subreq = aio_ex->smbreq->smb2req->subreq;
384 files_struct *fsp = aio_ex->fsp;
385 NTSTATUS status;
386 ssize_t nread;
387 struct vfs_aio_state vfs_aio_state = { 0 };
389 nread = SMB_VFS_PREAD_RECV(req, &vfs_aio_state);
390 TALLOC_FREE(req);
392 DEBUG(10, ("pread_recv returned %d, err = %s\n", (int)nread,
393 (nread == -1) ? strerror(vfs_aio_state.error) : "no error"));
395 /* Common error or success code processing for async or sync
396 read returns. */
398 status = smb2_read_complete(subreq, nread, vfs_aio_state.error);
400 if (nread > 0) {
401 fh_set_pos(fsp->fh, aio_ex->offset + nread);
402 fh_set_position_information(fsp->fh,
403 fh_get_pos(fsp->fh));
406 DEBUG(10, ("smb2: scheduled aio_read completed "
407 "for file %s, offset %.0f, len = %u "
408 "(errcode = %d, NTSTATUS = %s)\n",
409 fsp_str_dbg(aio_ex->fsp),
410 (double)aio_ex->offset,
411 (unsigned int)nread,
412 vfs_aio_state.error, nt_errstr(status)));
414 if (tevent_req_nterror(subreq, status)) {
415 return;
417 tevent_req_done(subreq);
420 static void aio_pwrite_smb2_done(struct tevent_req *req);
422 /****************************************************************************
423 Set up an aio request from a SMB2write call.
424 *****************************************************************************/
426 NTSTATUS schedule_aio_smb2_write(connection_struct *conn,
427 struct smb_request *smbreq,
428 files_struct *fsp,
429 uint64_t in_offset,
430 DATA_BLOB in_data,
431 bool write_through)
433 struct aio_extra *aio_ex = NULL;
434 size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
435 struct tevent_req *req;
436 bool is_compound = false;
437 bool is_last_in_compound = false;
439 if (fsp_is_alternate_stream(fsp)) {
440 /* No AIO on streams yet */
441 DEBUG(10, ("AIO on streams not yet supported\n"));
442 return NT_STATUS_RETRY;
445 if (fsp->op == NULL) {
446 /* No AIO on internal opens. */
447 return NT_STATUS_RETRY;
450 if ((!min_aio_write_size || (in_data.length < min_aio_write_size))
451 && !SMB_VFS_AIO_FORCE(fsp)) {
452 /* Too small a write for aio request. */
453 DEBUG(10,("smb2: write size (%u) too "
454 "small for minimum aio_write of %u\n",
455 (unsigned int)in_data.length,
456 (unsigned int)min_aio_write_size ));
457 return NT_STATUS_RETRY;
460 is_compound = smbd_smb2_is_compound(smbreq->smb2req);
461 is_last_in_compound = smbd_smb2_is_last_in_compound(smbreq->smb2req);
463 if (is_compound && !is_last_in_compound) {
465 * Only allow going async if this is the last
466 * request in a compound.
468 return NT_STATUS_RETRY;
471 if (smbreq->unread_bytes) {
472 /* Can't do async with recvfile. */
473 return NT_STATUS_RETRY;
476 if (!(aio_ex = create_aio_extra(smbreq->smb2req, fsp, 0))) {
477 return NT_STATUS_NO_MEMORY;
480 aio_ex->write_through = write_through;
482 init_strict_lock_struct(fsp,
483 fsp->op->global->open_persistent_id,
484 in_offset,
485 (uint64_t)in_data.length,
486 WRITE_LOCK,
487 lp_posix_cifsu_locktype(fsp),
488 &aio_ex->lock);
490 /* Take the lock until the AIO completes. */
491 if (!SMB_VFS_STRICT_LOCK_CHECK(conn, fsp, &aio_ex->lock)) {
492 TALLOC_FREE(aio_ex);
493 return NT_STATUS_FILE_LOCK_CONFLICT;
496 aio_ex->nbyte = in_data.length;
497 aio_ex->offset = in_offset;
499 req = pwrite_fsync_send(aio_ex, fsp->conn->sconn->ev_ctx, fsp,
500 in_data.data, in_data.length, in_offset,
501 write_through);
502 if (req == NULL) {
503 DEBUG(3, ("smb2: SMB_VFS_PWRITE_SEND failed. "
504 "Error %s\n", strerror(errno)));
505 TALLOC_FREE(aio_ex);
506 return NT_STATUS_RETRY;
508 tevent_req_set_callback(req, aio_pwrite_smb2_done, aio_ex);
510 if (!aio_add_req_to_fsp(fsp, req)) {
511 DEBUG(1, ("Could not add req to fsp\n"));
512 TALLOC_FREE(aio_ex);
513 return NT_STATUS_RETRY;
516 /* We don't need talloc_move here as both aio_ex and
517 * smbreq are children of smbreq->smb2req. */
518 aio_ex->smbreq = smbreq;
519 smbreq->async_priv = aio_ex;
521 /* This should actually be improved to span the write. */
522 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WRITE);
523 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WRITE);
526 * We don't want to do write behind due to ownership
527 * issues of the request structs. Maybe add it if I
528 * figure those out. JRA.
531 DEBUG(10,("smb2: scheduled aio_write for file "
532 "%s, offset %.0f, len = %u (mid = %u)\n",
533 fsp_str_dbg(fsp),
534 (double)in_offset,
535 (unsigned int)in_data.length,
536 (unsigned int)aio_ex->smbreq->mid));
538 return NT_STATUS_OK;
541 static void aio_pwrite_smb2_done(struct tevent_req *req)
543 struct aio_extra *aio_ex = tevent_req_callback_data(
544 req, struct aio_extra);
545 ssize_t numtowrite = aio_ex->nbyte;
546 struct tevent_req *subreq = aio_ex->smbreq->smb2req->subreq;
547 files_struct *fsp = aio_ex->fsp;
548 NTSTATUS status;
549 ssize_t nwritten;
550 int err = 0;
552 nwritten = pwrite_fsync_recv(req, &err);
553 TALLOC_FREE(req);
555 DEBUG(10, ("pwrite_recv returned %d, err = %s\n", (int)nwritten,
556 (nwritten == -1) ? strerror(err) : "no error"));
558 mark_file_modified(fsp);
560 status = smb2_write_complete_nosync(subreq, nwritten, err);
562 DEBUG(10, ("smb2: scheduled aio_write completed "
563 "for file %s, offset %.0f, requested %u, "
564 "written = %u (errcode = %d, NTSTATUS = %s)\n",
565 fsp_str_dbg(fsp),
566 (double)aio_ex->offset,
567 (unsigned int)numtowrite,
568 (unsigned int)nwritten,
569 err, nt_errstr(status)));
571 if (tevent_req_nterror(subreq, status)) {
572 return;
574 tevent_req_done(subreq);