[745] | 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 |
|
---|
| 26 | #if defined(WITH_AIO)
|
---|
| 27 |
|
---|
| 28 | /* The signal we'll use to signify aio done. */
|
---|
| 29 | #ifndef RT_SIGNAL_AIO
|
---|
| 30 | #define RT_SIGNAL_AIO (SIGRTMIN+3)
|
---|
| 31 | #endif
|
---|
| 32 |
|
---|
| 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
|
---|
| 39 |
|
---|
| 40 | /****************************************************************************
|
---|
| 41 | The buffer we keep around whilst an aio request is in process.
|
---|
| 42 | *****************************************************************************/
|
---|
| 43 |
|
---|
| 44 | struct aio_extra {
|
---|
| 45 | struct aio_extra *next, *prev;
|
---|
| 46 | SMB_STRUCT_AIOCB acb;
|
---|
| 47 | files_struct *fsp;
|
---|
| 48 | struct smb_request *smbreq;
|
---|
| 49 | DATA_BLOB outbuf;
|
---|
| 50 | struct lock_struct lock;
|
---|
| 51 | bool write_through;
|
---|
| 52 | int (*handle_completion)(struct aio_extra *ex, int errcode);
|
---|
| 53 | };
|
---|
| 54 |
|
---|
| 55 | /****************************************************************************
|
---|
| 56 | Initialize the signal handler for aio read/write.
|
---|
| 57 | *****************************************************************************/
|
---|
| 58 |
|
---|
| 59 | static void smbd_aio_signal_handler(struct tevent_context *ev_ctx,
|
---|
| 60 | struct tevent_signal *se,
|
---|
| 61 | int signum, int count,
|
---|
| 62 | void *_info, void *private_data)
|
---|
| 63 | {
|
---|
| 64 | siginfo_t *info = (siginfo_t *)_info;
|
---|
| 65 | struct aio_extra *aio_ex = (struct aio_extra *)
|
---|
| 66 | info->si_value.sival_ptr;
|
---|
| 67 |
|
---|
| 68 | smbd_aio_complete_aio_ex(aio_ex);
|
---|
[751] | 69 | TALLOC_FREE(aio_ex);
|
---|
[745] | 70 | }
|
---|
| 71 |
|
---|
| 72 |
|
---|
| 73 | static bool initialize_async_io_handler(void)
|
---|
| 74 | {
|
---|
| 75 | static bool tried_signal_setup = false;
|
---|
| 76 |
|
---|
| 77 | if (aio_signal_event) {
|
---|
| 78 | return true;
|
---|
| 79 | }
|
---|
| 80 | if (tried_signal_setup) {
|
---|
| 81 | return false;
|
---|
| 82 | }
|
---|
| 83 | tried_signal_setup = true;
|
---|
| 84 |
|
---|
| 85 | aio_signal_event = tevent_add_signal(smbd_event_context(),
|
---|
| 86 | smbd_event_context(),
|
---|
| 87 | RT_SIGNAL_AIO, SA_SIGINFO,
|
---|
| 88 | smbd_aio_signal_handler,
|
---|
| 89 | NULL);
|
---|
| 90 | if (!aio_signal_event) {
|
---|
| 91 | DEBUG(10, ("Failed to setup RT_SIGNAL_AIO handler\n"));
|
---|
| 92 | return false;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | /* tevent supports 100 signal with SA_SIGINFO */
|
---|
| 96 | aio_pending_size = 100;
|
---|
| 97 | return true;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 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);
|
---|
| 104 |
|
---|
| 105 | static int aio_extra_destructor(struct aio_extra *aio_ex)
|
---|
| 106 | {
|
---|
| 107 | DLIST_REMOVE(aio_list_head, aio_ex);
|
---|
| 108 | return 0;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | /****************************************************************************
|
---|
| 112 | Create the extended aio struct we must keep around for the lifetime
|
---|
| 113 | of the aio call.
|
---|
| 114 | *****************************************************************************/
|
---|
| 115 |
|
---|
| 116 | static struct aio_extra *create_aio_extra(TALLOC_CTX *mem_ctx,
|
---|
| 117 | files_struct *fsp,
|
---|
| 118 | size_t buflen)
|
---|
| 119 | {
|
---|
| 120 | struct aio_extra *aio_ex = TALLOC_ZERO_P(mem_ctx, struct aio_extra);
|
---|
| 121 |
|
---|
| 122 | if (!aio_ex) {
|
---|
| 123 | return NULL;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 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. */
|
---|
| 129 |
|
---|
| 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;
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
| 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;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | /****************************************************************************
|
---|
| 144 | Set up an aio request from a SMBreadX call.
|
---|
| 145 | *****************************************************************************/
|
---|
| 146 |
|
---|
| 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)
|
---|
| 151 | {
|
---|
| 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;
|
---|
| 157 |
|
---|
| 158 | /* Ensure aio is initialized. */
|
---|
| 159 | if (!initialize_async_io_handler()) {
|
---|
| 160 | return NT_STATUS_RETRY;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 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;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 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;
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 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;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 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;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 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 */
|
---|
| 194 |
|
---|
| 195 | bufsize = smb_size + 12 * 2 + smb_maxcnt;
|
---|
| 196 |
|
---|
| 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;
|
---|
| 200 | }
|
---|
| 201 | aio_ex->handle_completion = handle_aio_read_complete;
|
---|
| 202 |
|
---|
| 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. */
|
---|
| 206 |
|
---|
| 207 | init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
|
---|
| 208 | (uint64_t)startpos, (uint64_t)smb_maxcnt, READ_LOCK,
|
---|
| 209 | &aio_ex->lock);
|
---|
| 210 |
|
---|
| 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;
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | a = &aio_ex->acb;
|
---|
| 218 |
|
---|
| 219 | /* Now set up the aio record for the read call. */
|
---|
| 220 |
|
---|
| 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;
|
---|
| 228 |
|
---|
| 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;
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | outstanding_aio_calls++;
|
---|
| 239 | aio_ex->smbreq = talloc_move(aio_ex, &smbreq);
|
---|
| 240 |
|
---|
| 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 ));
|
---|
| 245 |
|
---|
| 246 | return NT_STATUS_OK;
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | /****************************************************************************
|
---|
| 250 | Set up an aio request from a SMBwriteX call.
|
---|
| 251 | *****************************************************************************/
|
---|
| 252 |
|
---|
| 253 | NTSTATUS schedule_aio_write_and_X(connection_struct *conn,
|
---|
| 254 | struct smb_request *smbreq,
|
---|
| 255 | files_struct *fsp, char *data,
|
---|
| 256 | SMB_OFF_T startpos,
|
---|
| 257 | size_t numtowrite)
|
---|
| 258 | {
|
---|
| 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;
|
---|
| 264 |
|
---|
| 265 | /* Ensure aio is initialized. */
|
---|
| 266 | if (!initialize_async_io_handler()) {
|
---|
| 267 | return NT_STATUS_RETRY;
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 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;
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 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;
|
---|
| 284 | }
|
---|
| 285 |
|
---|
| 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;
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 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;
|
---|
| 303 | }
|
---|
| 304 |
|
---|
| 305 | bufsize = smb_size + 6*2;
|
---|
| 306 |
|
---|
| 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;
|
---|
| 310 | }
|
---|
| 311 | aio_ex->handle_completion = handle_aio_write_complete;
|
---|
| 312 | aio_ex->write_through = BITSETW(smbreq->vwv+7,0);
|
---|
| 313 |
|
---|
| 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. */
|
---|
| 317 |
|
---|
| 318 | init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
|
---|
| 319 | (uint64_t)startpos, (uint64_t)numtowrite, WRITE_LOCK,
|
---|
| 320 | &aio_ex->lock);
|
---|
| 321 |
|
---|
| 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;
|
---|
| 326 | }
|
---|
| 327 |
|
---|
| 328 | a = &aio_ex->acb;
|
---|
| 329 |
|
---|
| 330 | /* Now set up the aio record for the write call. */
|
---|
| 331 |
|
---|
| 332 | a->aio_fildes = fsp->fh->fd;
|
---|
| 333 | a->aio_buf = data;
|
---|
| 334 | a->aio_nbytes = numtowrite;
|
---|
| 335 | a->aio_offset = startpos;
|
---|
| 336 | a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
|
---|
| 337 | a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
|
---|
| 338 | a->aio_sigevent.sigev_value.sival_ptr = aio_ex;
|
---|
| 339 |
|
---|
| 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;
|
---|
| 347 | }
|
---|
| 348 |
|
---|
| 349 | outstanding_aio_calls++;
|
---|
| 350 | aio_ex->smbreq = talloc_move(aio_ex, &smbreq);
|
---|
| 351 |
|
---|
| 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);
|
---|
| 355 |
|
---|
| 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.");
|
---|
| 370 | }
|
---|
| 371 | DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write "
|
---|
| 372 | "behind for file %s\n", fsp_str_dbg(fsp)));
|
---|
| 373 | }
|
---|
| 374 |
|
---|
| 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 ));
|
---|
| 380 |
|
---|
| 381 | return NT_STATUS_OK;
|
---|
| 382 | }
|
---|
| 383 |
|
---|
| 384 | /****************************************************************************
|
---|
| 385 | Set up an aio request from a SMB2 read call.
|
---|
| 386 | *****************************************************************************/
|
---|
| 387 |
|
---|
| 388 | NTSTATUS schedule_smb2_aio_read(connection_struct *conn,
|
---|
| 389 | struct smb_request *smbreq,
|
---|
| 390 | files_struct *fsp,
|
---|
| 391 | TALLOC_CTX *ctx,
|
---|
| 392 | DATA_BLOB *preadbuf,
|
---|
| 393 | SMB_OFF_T startpos,
|
---|
| 394 | size_t smb_maxcnt)
|
---|
| 395 | {
|
---|
| 396 | struct aio_extra *aio_ex;
|
---|
| 397 | SMB_STRUCT_AIOCB *a;
|
---|
| 398 | size_t min_aio_read_size = lp_aio_read_size(SNUM(conn));
|
---|
| 399 | int ret;
|
---|
| 400 |
|
---|
| 401 | /* Ensure aio is initialized. */
|
---|
| 402 | if (!initialize_async_io_handler()) {
|
---|
| 403 | return NT_STATUS_RETRY;
|
---|
| 404 | }
|
---|
| 405 |
|
---|
| 406 | if (fsp->base_fsp != NULL) {
|
---|
| 407 | /* No AIO on streams yet */
|
---|
| 408 | DEBUG(10, ("AIO on streams not yet supported\n"));
|
---|
| 409 | return NT_STATUS_RETRY;
|
---|
| 410 | }
|
---|
| 411 |
|
---|
| 412 | if ((!min_aio_read_size || (smb_maxcnt < min_aio_read_size))
|
---|
| 413 | && !SMB_VFS_AIO_FORCE(fsp)) {
|
---|
| 414 | /* Too small a read for aio request. */
|
---|
| 415 | DEBUG(10,("smb2: read size (%u) too small "
|
---|
| 416 | "for minimum aio_read of %u\n",
|
---|
| 417 | (unsigned int)smb_maxcnt,
|
---|
| 418 | (unsigned int)min_aio_read_size ));
|
---|
| 419 | return NT_STATUS_RETRY;
|
---|
| 420 | }
|
---|
| 421 |
|
---|
| 422 | /* Only do this on reads not using the write cache. */
|
---|
| 423 | if (lp_write_cache_size(SNUM(conn)) != 0) {
|
---|
| 424 | return NT_STATUS_RETRY;
|
---|
| 425 | }
|
---|
| 426 |
|
---|
| 427 | if (outstanding_aio_calls >= aio_pending_size) {
|
---|
| 428 | DEBUG(10,("smb2: Already have %d aio "
|
---|
| 429 | "activities outstanding.\n",
|
---|
| 430 | outstanding_aio_calls ));
|
---|
| 431 | return NT_STATUS_RETRY;
|
---|
| 432 | }
|
---|
| 433 |
|
---|
| 434 | /* Create the out buffer. */
|
---|
| 435 | *preadbuf = data_blob_talloc(ctx, NULL, smb_maxcnt);
|
---|
| 436 | if (preadbuf->data == NULL) {
|
---|
| 437 | return NT_STATUS_NO_MEMORY;
|
---|
| 438 | }
|
---|
| 439 |
|
---|
| 440 | if (!(aio_ex = create_aio_extra(smbreq->smb2req, fsp, 0))) {
|
---|
| 441 | return NT_STATUS_NO_MEMORY;
|
---|
| 442 | }
|
---|
| 443 | aio_ex->handle_completion = handle_aio_smb2_read_complete;
|
---|
| 444 |
|
---|
| 445 | init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
|
---|
| 446 | (uint64_t)startpos, (uint64_t)smb_maxcnt, READ_LOCK,
|
---|
| 447 | &aio_ex->lock);
|
---|
| 448 |
|
---|
| 449 | /* Take the lock until the AIO completes. */
|
---|
| 450 | if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
|
---|
| 451 | TALLOC_FREE(aio_ex);
|
---|
| 452 | return NT_STATUS_FILE_LOCK_CONFLICT;
|
---|
| 453 | }
|
---|
| 454 |
|
---|
| 455 | a = &aio_ex->acb;
|
---|
| 456 |
|
---|
| 457 | /* Now set up the aio record for the read call. */
|
---|
| 458 |
|
---|
| 459 | a->aio_fildes = fsp->fh->fd;
|
---|
| 460 | a->aio_buf = preadbuf->data;
|
---|
| 461 | a->aio_nbytes = smb_maxcnt;
|
---|
| 462 | a->aio_offset = startpos;
|
---|
| 463 | a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
|
---|
| 464 | a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
|
---|
| 465 | a->aio_sigevent.sigev_value.sival_ptr = aio_ex;
|
---|
| 466 |
|
---|
| 467 | ret = SMB_VFS_AIO_READ(fsp, a);
|
---|
| 468 | if (ret == -1) {
|
---|
| 469 | DEBUG(0,("smb2: aio_read failed. "
|
---|
| 470 | "Error %s\n", strerror(errno) ));
|
---|
| 471 | SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
|
---|
| 472 | TALLOC_FREE(aio_ex);
|
---|
| 473 | return NT_STATUS_RETRY;
|
---|
| 474 | }
|
---|
| 475 |
|
---|
| 476 | outstanding_aio_calls++;
|
---|
| 477 | /* We don't need talloc_move here as both aio_ex and
|
---|
| 478 | * smbreq are children of smbreq->smb2req. */
|
---|
| 479 | aio_ex->smbreq = smbreq;
|
---|
| 480 |
|
---|
| 481 | DEBUG(10,("smb2: scheduled aio_read for file %s, "
|
---|
| 482 | "offset %.0f, len = %u (mid = %u)\n",
|
---|
| 483 | fsp_str_dbg(fsp), (double)startpos, (unsigned int)smb_maxcnt,
|
---|
| 484 | (unsigned int)aio_ex->smbreq->mid ));
|
---|
| 485 |
|
---|
| 486 | return NT_STATUS_OK;
|
---|
| 487 | }
|
---|
| 488 |
|
---|
| 489 | /****************************************************************************
|
---|
| 490 | Set up an aio request from a SMB2write call.
|
---|
| 491 | *****************************************************************************/
|
---|
| 492 |
|
---|
| 493 | NTSTATUS schedule_aio_smb2_write(connection_struct *conn,
|
---|
| 494 | struct smb_request *smbreq,
|
---|
| 495 | files_struct *fsp,
|
---|
| 496 | uint64_t in_offset,
|
---|
| 497 | DATA_BLOB in_data,
|
---|
| 498 | bool write_through)
|
---|
| 499 | {
|
---|
| 500 | struct aio_extra *aio_ex = NULL;
|
---|
| 501 | SMB_STRUCT_AIOCB *a = NULL;
|
---|
| 502 | size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
|
---|
| 503 | int ret;
|
---|
| 504 |
|
---|
| 505 | /* Ensure aio is initialized. */
|
---|
| 506 | if (!initialize_async_io_handler()) {
|
---|
| 507 | return NT_STATUS_RETRY;
|
---|
| 508 | }
|
---|
| 509 |
|
---|
| 510 | if (fsp->base_fsp != NULL) {
|
---|
| 511 | /* No AIO on streams yet */
|
---|
| 512 | DEBUG(10, ("AIO on streams not yet supported\n"));
|
---|
| 513 | return NT_STATUS_RETRY;
|
---|
| 514 | }
|
---|
| 515 |
|
---|
| 516 | if ((!min_aio_write_size || (in_data.length < min_aio_write_size))
|
---|
| 517 | && !SMB_VFS_AIO_FORCE(fsp)) {
|
---|
| 518 | /* Too small a write for aio request. */
|
---|
| 519 | DEBUG(10,("smb2: write size (%u) too "
|
---|
| 520 | "small for minimum aio_write of %u\n",
|
---|
| 521 | (unsigned int)in_data.length,
|
---|
| 522 | (unsigned int)min_aio_write_size ));
|
---|
| 523 | return NT_STATUS_RETRY;
|
---|
| 524 | }
|
---|
| 525 |
|
---|
| 526 | /* Only do this on writes not using the write cache. */
|
---|
| 527 | if (lp_write_cache_size(SNUM(conn)) != 0) {
|
---|
| 528 | return NT_STATUS_RETRY;
|
---|
| 529 | }
|
---|
| 530 |
|
---|
| 531 | if (outstanding_aio_calls >= aio_pending_size) {
|
---|
| 532 | DEBUG(3,("smb2: Already have %d aio "
|
---|
| 533 | "activities outstanding.\n",
|
---|
| 534 | outstanding_aio_calls ));
|
---|
| 535 | return NT_STATUS_RETRY;
|
---|
| 536 | }
|
---|
| 537 |
|
---|
| 538 | if (!(aio_ex = create_aio_extra(smbreq->smb2req, fsp, 0))) {
|
---|
| 539 | return NT_STATUS_NO_MEMORY;
|
---|
| 540 | }
|
---|
| 541 |
|
---|
| 542 | aio_ex->handle_completion = handle_aio_smb2_write_complete;
|
---|
| 543 | aio_ex->write_through = write_through;
|
---|
| 544 |
|
---|
| 545 | init_strict_lock_struct(fsp, (uint64_t)smbreq->smbpid,
|
---|
| 546 | in_offset, (uint64_t)in_data.length, WRITE_LOCK,
|
---|
| 547 | &aio_ex->lock);
|
---|
| 548 |
|
---|
| 549 | /* Take the lock until the AIO completes. */
|
---|
| 550 | if (!SMB_VFS_STRICT_LOCK(conn, fsp, &aio_ex->lock)) {
|
---|
| 551 | TALLOC_FREE(aio_ex);
|
---|
| 552 | return NT_STATUS_FILE_LOCK_CONFLICT;
|
---|
| 553 | }
|
---|
| 554 |
|
---|
| 555 | a = &aio_ex->acb;
|
---|
| 556 |
|
---|
| 557 | /* Now set up the aio record for the write call. */
|
---|
| 558 |
|
---|
| 559 | a->aio_fildes = fsp->fh->fd;
|
---|
| 560 | a->aio_buf = in_data.data;
|
---|
| 561 | a->aio_nbytes = in_data.length;
|
---|
| 562 | a->aio_offset = in_offset;
|
---|
| 563 | a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
|
---|
| 564 | a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
|
---|
| 565 | a->aio_sigevent.sigev_value.sival_ptr = aio_ex;
|
---|
| 566 |
|
---|
| 567 | ret = SMB_VFS_AIO_WRITE(fsp, a);
|
---|
| 568 | if (ret == -1) {
|
---|
| 569 | DEBUG(3,("smb2: aio_write failed. "
|
---|
| 570 | "Error %s\n", strerror(errno) ));
|
---|
| 571 | SMB_VFS_STRICT_UNLOCK(conn, fsp, &aio_ex->lock);
|
---|
| 572 | TALLOC_FREE(aio_ex);
|
---|
| 573 | return NT_STATUS_RETRY;
|
---|
| 574 | }
|
---|
| 575 |
|
---|
| 576 | outstanding_aio_calls++;
|
---|
| 577 | /* We don't need talloc_move here as both aio_ex and
|
---|
| 578 | * smbreq are children of smbreq->smb2req. */
|
---|
| 579 | aio_ex->smbreq = smbreq;
|
---|
| 580 |
|
---|
| 581 | /* This should actually be improved to span the write. */
|
---|
| 582 | contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WRITE);
|
---|
| 583 | contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WRITE);
|
---|
| 584 |
|
---|
| 585 | /*
|
---|
| 586 | * We don't want to do write behind due to ownership
|
---|
| 587 | * issues of the request structs. Maybe add it if I
|
---|
| 588 | * figure those out. JRA.
|
---|
| 589 | */
|
---|
| 590 |
|
---|
| 591 | DEBUG(10,("smb2: scheduled aio_write for file "
|
---|
| 592 | "%s, offset %.0f, len = %u (mid = %u) "
|
---|
| 593 | "outstanding_aio_calls = %d\n",
|
---|
| 594 | fsp_str_dbg(fsp),
|
---|
| 595 | (double)in_offset,
|
---|
| 596 | (unsigned int)in_data.length,
|
---|
| 597 | (unsigned int)aio_ex->smbreq->mid,
|
---|
| 598 | outstanding_aio_calls ));
|
---|
| 599 |
|
---|
| 600 | return NT_STATUS_OK;
|
---|
| 601 | }
|
---|
| 602 |
|
---|
| 603 | /****************************************************************************
|
---|
| 604 | Complete the read and return the data or error back to the client.
|
---|
| 605 | Returns errno or zero if all ok.
|
---|
| 606 | *****************************************************************************/
|
---|
| 607 |
|
---|
| 608 | static int handle_aio_read_complete(struct aio_extra *aio_ex, int errcode)
|
---|
| 609 | {
|
---|
| 610 | int outsize;
|
---|
| 611 | char *outbuf = (char *)aio_ex->outbuf.data;
|
---|
| 612 | char *data = smb_buf(outbuf);
|
---|
| 613 | ssize_t nread = SMB_VFS_AIO_RETURN(aio_ex->fsp,&aio_ex->acb);
|
---|
| 614 |
|
---|
| 615 | if (nread < 0) {
|
---|
| 616 | /* We're relying here on the fact that if the fd is
|
---|
| 617 | closed then the aio will complete and aio_return
|
---|
| 618 | will return an error. Hopefully this is
|
---|
| 619 | true.... JRA. */
|
---|
| 620 |
|
---|
| 621 | DEBUG( 3,( "handle_aio_read_complete: file %s nread == %d. "
|
---|
| 622 | "Error = %s\n",
|
---|
| 623 | fsp_str_dbg(aio_ex->fsp), (int)nread, strerror(errcode)));
|
---|
| 624 |
|
---|
| 625 | ERROR_NT(map_nt_error_from_unix(errcode));
|
---|
| 626 | outsize = srv_set_message(outbuf,0,0,true);
|
---|
| 627 | } else {
|
---|
| 628 | outsize = srv_set_message(outbuf,12,nread,False);
|
---|
| 629 | SSVAL(outbuf,smb_vwv2,0xFFFF); /* Remaining - must be * -1. */
|
---|
| 630 | SSVAL(outbuf,smb_vwv5,nread);
|
---|
| 631 | SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
|
---|
| 632 | SSVAL(outbuf,smb_vwv7,((nread >> 16) & 1));
|
---|
| 633 | SSVAL(smb_buf(outbuf),-2,nread);
|
---|
| 634 |
|
---|
| 635 | aio_ex->fsp->fh->pos = aio_ex->acb.aio_offset + nread;
|
---|
| 636 | aio_ex->fsp->fh->position_information = aio_ex->fsp->fh->pos;
|
---|
| 637 |
|
---|
| 638 | DEBUG( 3, ( "handle_aio_read_complete file %s max=%d "
|
---|
| 639 | "nread=%d\n",
|
---|
| 640 | fsp_str_dbg(aio_ex->fsp),
|
---|
| 641 | (int)aio_ex->acb.aio_nbytes, (int)nread ) );
|
---|
| 642 |
|
---|
| 643 | }
|
---|
| 644 | smb_setlen(outbuf,outsize - 4);
|
---|
| 645 | show_msg(outbuf);
|
---|
| 646 | if (!srv_send_smb(aio_ex->smbreq->sconn, outbuf,
|
---|
| 647 | true, aio_ex->smbreq->seqnum+1,
|
---|
| 648 | IS_CONN_ENCRYPTED(aio_ex->fsp->conn), NULL)) {
|
---|
| 649 | exit_server_cleanly("handle_aio_read_complete: srv_send_smb "
|
---|
| 650 | "failed.");
|
---|
| 651 | }
|
---|
| 652 |
|
---|
| 653 | DEBUG(10,("handle_aio_read_complete: scheduled aio_read completed "
|
---|
| 654 | "for file %s, offset %.0f, len = %u\n",
|
---|
| 655 | fsp_str_dbg(aio_ex->fsp), (double)aio_ex->acb.aio_offset,
|
---|
| 656 | (unsigned int)nread ));
|
---|
| 657 |
|
---|
| 658 | return errcode;
|
---|
| 659 | }
|
---|
| 660 |
|
---|
| 661 | /****************************************************************************
|
---|
| 662 | Complete the write and return the data or error back to the client.
|
---|
| 663 | Returns error code or zero if all ok.
|
---|
| 664 | *****************************************************************************/
|
---|
| 665 |
|
---|
| 666 | static int handle_aio_write_complete(struct aio_extra *aio_ex, int errcode)
|
---|
| 667 | {
|
---|
| 668 | files_struct *fsp = aio_ex->fsp;
|
---|
| 669 | char *outbuf = (char *)aio_ex->outbuf.data;
|
---|
| 670 | ssize_t numtowrite = aio_ex->acb.aio_nbytes;
|
---|
| 671 | ssize_t nwritten = SMB_VFS_AIO_RETURN(fsp,&aio_ex->acb);
|
---|
| 672 |
|
---|
| 673 | if (fsp->aio_write_behind) {
|
---|
| 674 | if (nwritten != numtowrite) {
|
---|
| 675 | if (nwritten == -1) {
|
---|
| 676 | DEBUG(5,("handle_aio_write_complete: "
|
---|
| 677 | "aio_write_behind failed ! File %s "
|
---|
| 678 | "is corrupt ! Error %s\n",
|
---|
| 679 | fsp_str_dbg(fsp), strerror(errcode)));
|
---|
| 680 | } else {
|
---|
| 681 | DEBUG(0,("handle_aio_write_complete: "
|
---|
| 682 | "aio_write_behind failed ! File %s "
|
---|
| 683 | "is corrupt ! Wanted %u bytes but "
|
---|
| 684 | "only wrote %d\n", fsp_str_dbg(fsp),
|
---|
| 685 | (unsigned int)numtowrite,
|
---|
| 686 | (int)nwritten ));
|
---|
| 687 | errcode = EIO;
|
---|
| 688 | }
|
---|
| 689 | } else {
|
---|
| 690 | DEBUG(10,("handle_aio_write_complete: "
|
---|
| 691 | "aio_write_behind completed for file %s\n",
|
---|
| 692 | fsp_str_dbg(fsp)));
|
---|
| 693 | }
|
---|
| 694 | /* TODO: should no return 0 in case of an error !!! */
|
---|
| 695 | return 0;
|
---|
| 696 | }
|
---|
| 697 |
|
---|
| 698 | /* We don't need outsize or set_message here as we've already set the
|
---|
| 699 | fixed size length when we set up the aio call. */
|
---|
| 700 |
|
---|
| 701 | if(nwritten == -1) {
|
---|
| 702 | DEBUG( 3,( "handle_aio_write: file %s wanted %u bytes. "
|
---|
| 703 | "nwritten == %d. Error = %s\n",
|
---|
| 704 | fsp_str_dbg(fsp), (unsigned int)numtowrite,
|
---|
| 705 | (int)nwritten, strerror(errcode) ));
|
---|
| 706 |
|
---|
| 707 | ERROR_NT(map_nt_error_from_unix(errcode));
|
---|
| 708 | srv_set_message(outbuf,0,0,true);
|
---|
| 709 | } else {
|
---|
| 710 | NTSTATUS status;
|
---|
| 711 |
|
---|
| 712 | SSVAL(outbuf,smb_vwv2,nwritten);
|
---|
| 713 | SSVAL(outbuf,smb_vwv4,(nwritten>>16)&1);
|
---|
| 714 | if (nwritten < (ssize_t)numtowrite) {
|
---|
| 715 | SCVAL(outbuf,smb_rcls,ERRHRD);
|
---|
| 716 | SSVAL(outbuf,smb_err,ERRdiskfull);
|
---|
| 717 | }
|
---|
| 718 |
|
---|
| 719 | DEBUG(3,("handle_aio_write: fnum=%d num=%d wrote=%d\n",
|
---|
| 720 | fsp->fnum, (int)numtowrite, (int)nwritten));
|
---|
| 721 | status = sync_file(fsp->conn,fsp, aio_ex->write_through);
|
---|
| 722 | if (!NT_STATUS_IS_OK(status)) {
|
---|
| 723 | errcode = errno;
|
---|
| 724 | ERROR_BOTH(map_nt_error_from_unix(errcode),
|
---|
| 725 | ERRHRD, ERRdiskfull);
|
---|
| 726 | srv_set_message(outbuf,0,0,true);
|
---|
| 727 | DEBUG(5,("handle_aio_write: sync_file for %s returned %s\n",
|
---|
| 728 | fsp_str_dbg(fsp), nt_errstr(status)));
|
---|
| 729 | }
|
---|
| 730 |
|
---|
| 731 | aio_ex->fsp->fh->pos = aio_ex->acb.aio_offset + nwritten;
|
---|
| 732 | }
|
---|
| 733 |
|
---|
| 734 | show_msg(outbuf);
|
---|
| 735 | if (!srv_send_smb(aio_ex->smbreq->sconn, outbuf,
|
---|
| 736 | true, aio_ex->smbreq->seqnum+1,
|
---|
| 737 | IS_CONN_ENCRYPTED(fsp->conn),
|
---|
| 738 | NULL)) {
|
---|
| 739 | exit_server_cleanly("handle_aio_write_complete: "
|
---|
| 740 | "srv_send_smb failed.");
|
---|
| 741 | }
|
---|
| 742 |
|
---|
| 743 | DEBUG(10,("handle_aio_write_complete: scheduled aio_write completed "
|
---|
| 744 | "for file %s, offset %.0f, requested %u, written = %u\n",
|
---|
| 745 | fsp_str_dbg(fsp), (double)aio_ex->acb.aio_offset,
|
---|
| 746 | (unsigned int)numtowrite, (unsigned int)nwritten ));
|
---|
| 747 |
|
---|
| 748 | return errcode;
|
---|
| 749 | }
|
---|
| 750 |
|
---|
| 751 | /****************************************************************************
|
---|
| 752 | Complete the read and return the data or error back to the client.
|
---|
| 753 | Returns errno or zero if all ok.
|
---|
| 754 | *****************************************************************************/
|
---|
| 755 |
|
---|
| 756 | static int handle_aio_smb2_read_complete(struct aio_extra *aio_ex, int errcode)
|
---|
| 757 | {
|
---|
| 758 | NTSTATUS status;
|
---|
| 759 | struct tevent_req *subreq = aio_ex->smbreq->smb2req->subreq;
|
---|
| 760 | ssize_t nread = SMB_VFS_AIO_RETURN(aio_ex->fsp,&aio_ex->acb);
|
---|
| 761 |
|
---|
| 762 | /* Common error or success code processing for async or sync
|
---|
| 763 | read returns. */
|
---|
| 764 |
|
---|
| 765 | status = smb2_read_complete(subreq, nread, errcode);
|
---|
| 766 |
|
---|
| 767 | if (nread > 0) {
|
---|
| 768 | aio_ex->fsp->fh->pos = aio_ex->acb.aio_offset + nread;
|
---|
| 769 | aio_ex->fsp->fh->position_information = aio_ex->fsp->fh->pos;
|
---|
| 770 | }
|
---|
| 771 |
|
---|
| 772 | DEBUG(10,("smb2: scheduled aio_read completed "
|
---|
| 773 | "for file %s, offset %.0f, len = %u "
|
---|
| 774 | "(errcode = %d, NTSTATUS = %s)\n",
|
---|
| 775 | fsp_str_dbg(aio_ex->fsp),
|
---|
| 776 | (double)aio_ex->acb.aio_offset,
|
---|
| 777 | (unsigned int)nread,
|
---|
| 778 | errcode,
|
---|
| 779 | nt_errstr(status) ));
|
---|
| 780 |
|
---|
| 781 | if (!NT_STATUS_IS_OK(status)) {
|
---|
| 782 | tevent_req_nterror(subreq, status);
|
---|
| 783 | return errcode;
|
---|
| 784 | }
|
---|
| 785 |
|
---|
| 786 | tevent_req_done(subreq);
|
---|
| 787 | return errcode;
|
---|
| 788 | }
|
---|
| 789 |
|
---|
| 790 | /****************************************************************************
|
---|
| 791 | Complete the SMB2 write and return the data or error back to the client.
|
---|
| 792 | Returns error code or zero if all ok.
|
---|
| 793 | *****************************************************************************/
|
---|
| 794 |
|
---|
| 795 | static int handle_aio_smb2_write_complete(struct aio_extra *aio_ex, int errcode)
|
---|
| 796 | {
|
---|
| 797 | files_struct *fsp = aio_ex->fsp;
|
---|
| 798 | ssize_t numtowrite = aio_ex->acb.aio_nbytes;
|
---|
| 799 | ssize_t nwritten = SMB_VFS_AIO_RETURN(fsp,&aio_ex->acb);
|
---|
| 800 | struct tevent_req *subreq = aio_ex->smbreq->smb2req->subreq;
|
---|
| 801 | NTSTATUS status;
|
---|
| 802 |
|
---|
| 803 | status = smb2_write_complete(subreq, nwritten, errcode);
|
---|
| 804 |
|
---|
| 805 | DEBUG(10,("smb2: scheduled aio_write completed "
|
---|
| 806 | "for file %s, offset %.0f, requested %u, "
|
---|
| 807 | "written = %u (errcode = %d, NTSTATUS = %s)\n",
|
---|
| 808 | fsp_str_dbg(fsp),
|
---|
| 809 | (double)aio_ex->acb.aio_offset,
|
---|
| 810 | (unsigned int)numtowrite,
|
---|
| 811 | (unsigned int)nwritten,
|
---|
| 812 | errcode,
|
---|
| 813 | nt_errstr(status) ));
|
---|
| 814 |
|
---|
| 815 | if (!NT_STATUS_IS_OK(status)) {
|
---|
| 816 | tevent_req_nterror(subreq, status);
|
---|
| 817 | return errcode;
|
---|
| 818 | }
|
---|
| 819 |
|
---|
| 820 | tevent_req_done(subreq);
|
---|
| 821 | return errcode;
|
---|
| 822 | }
|
---|
| 823 |
|
---|
| 824 | /****************************************************************************
|
---|
| 825 | Handle any aio completion. Returns True if finished (and sets *perr if err
|
---|
| 826 | was non-zero), False if not.
|
---|
| 827 | *****************************************************************************/
|
---|
| 828 |
|
---|
| 829 | static bool handle_aio_completed(struct aio_extra *aio_ex, int *perr)
|
---|
| 830 | {
|
---|
| 831 | files_struct *fsp = NULL;
|
---|
| 832 | int err;
|
---|
| 833 |
|
---|
| 834 | if(!aio_ex) {
|
---|
| 835 | DEBUG(3, ("handle_aio_completed: Non-existing aio_ex passed\n"));
|
---|
| 836 | return false;
|
---|
| 837 | }
|
---|
| 838 |
|
---|
| 839 | fsp = aio_ex->fsp;
|
---|
| 840 |
|
---|
| 841 | /* Ensure the operation has really completed. */
|
---|
| 842 | err = SMB_VFS_AIO_ERROR(fsp, &aio_ex->acb);
|
---|
| 843 | if (err == EINPROGRESS) {
|
---|
| 844 | DEBUG(10,( "handle_aio_completed: operation mid %llu still in "
|
---|
| 845 | "process for file %s\n",
|
---|
| 846 | (unsigned long long)aio_ex->smbreq->mid,
|
---|
| 847 | fsp_str_dbg(aio_ex->fsp)));
|
---|
| 848 | return False;
|
---|
| 849 | }
|
---|
| 850 |
|
---|
| 851 | /* Unlock now we're done. */
|
---|
| 852 | SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &aio_ex->lock);
|
---|
| 853 |
|
---|
| 854 | if (err == ECANCELED) {
|
---|
| 855 | /* If error is ECANCELED then don't return anything to the
|
---|
| 856 | * client. */
|
---|
| 857 | DEBUG(10,( "handle_aio_completed: operation mid %llu"
|
---|
| 858 | " canceled\n",
|
---|
| 859 | (unsigned long long)aio_ex->smbreq->mid));
|
---|
| 860 | return True;
|
---|
| 861 | }
|
---|
| 862 |
|
---|
| 863 | err = aio_ex->handle_completion(aio_ex, err);
|
---|
| 864 | if (err) {
|
---|
| 865 | *perr = err; /* Only save non-zero errors. */
|
---|
| 866 | }
|
---|
| 867 |
|
---|
| 868 | return True;
|
---|
| 869 | }
|
---|
| 870 |
|
---|
| 871 | /****************************************************************************
|
---|
| 872 | Handle any aio completion inline.
|
---|
| 873 | *****************************************************************************/
|
---|
| 874 |
|
---|
| 875 | void smbd_aio_complete_aio_ex(struct aio_extra *aio_ex)
|
---|
| 876 | {
|
---|
| 877 | files_struct *fsp = NULL;
|
---|
| 878 | int ret = 0;
|
---|
| 879 |
|
---|
| 880 | outstanding_aio_calls--;
|
---|
| 881 |
|
---|
| 882 | DEBUG(10,("smbd_aio_complete_mid: mid[%llu]\n",
|
---|
| 883 | (unsigned long long)aio_ex->smbreq->mid));
|
---|
| 884 |
|
---|
| 885 | fsp = aio_ex->fsp;
|
---|
| 886 | if (fsp == NULL) {
|
---|
| 887 | /* file was closed whilst I/O was outstanding. Just
|
---|
| 888 | * ignore. */
|
---|
| 889 | DEBUG( 3,( "smbd_aio_complete_mid: file closed whilst "
|
---|
| 890 | "aio outstanding (mid[%llu]).\n",
|
---|
| 891 | (unsigned long long)aio_ex->smbreq->mid));
|
---|
| 892 | return;
|
---|
| 893 | }
|
---|
| 894 |
|
---|
| 895 | if (!handle_aio_completed(aio_ex, &ret)) {
|
---|
| 896 | return;
|
---|
| 897 | }
|
---|
| 898 | }
|
---|
| 899 |
|
---|
| 900 | /****************************************************************************
|
---|
| 901 | We're doing write behind and the client closed the file. Wait up to 30
|
---|
| 902 | seconds (my arbitrary choice) for the aio to complete. Return 0 if all writes
|
---|
| 903 | completed, errno to return if not.
|
---|
| 904 | *****************************************************************************/
|
---|
| 905 |
|
---|
| 906 | #define SMB_TIME_FOR_AIO_COMPLETE_WAIT 29
|
---|
| 907 |
|
---|
| 908 | int wait_for_aio_completion(files_struct *fsp)
|
---|
| 909 | {
|
---|
| 910 | struct aio_extra *aio_ex;
|
---|
| 911 | const SMB_STRUCT_AIOCB **aiocb_list;
|
---|
| 912 | int aio_completion_count = 0;
|
---|
| 913 | time_t start_time = time_mono(NULL);
|
---|
| 914 | int seconds_left;
|
---|
| 915 |
|
---|
| 916 | for (seconds_left = SMB_TIME_FOR_AIO_COMPLETE_WAIT;
|
---|
| 917 | seconds_left >= 0;) {
|
---|
| 918 | int err = 0;
|
---|
| 919 | int i;
|
---|
| 920 | struct timespec ts;
|
---|
| 921 |
|
---|
| 922 | aio_completion_count = 0;
|
---|
| 923 | for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
|
---|
| 924 | if (aio_ex->fsp == fsp) {
|
---|
| 925 | aio_completion_count++;
|
---|
| 926 | }
|
---|
| 927 | }
|
---|
| 928 |
|
---|
| 929 | if (!aio_completion_count) {
|
---|
| 930 | return 0;
|
---|
| 931 | }
|
---|
| 932 |
|
---|
| 933 | DEBUG(3,("wait_for_aio_completion: waiting for %d aio events "
|
---|
| 934 | "to complete.\n", aio_completion_count ));
|
---|
| 935 |
|
---|
| 936 | aiocb_list = SMB_MALLOC_ARRAY(const SMB_STRUCT_AIOCB *,
|
---|
| 937 | aio_completion_count);
|
---|
| 938 | if (!aiocb_list) {
|
---|
| 939 | return ENOMEM;
|
---|
| 940 | }
|
---|
| 941 |
|
---|
| 942 | for( i = 0, aio_ex = aio_list_head;
|
---|
| 943 | aio_ex;
|
---|
| 944 | aio_ex = aio_ex->next) {
|
---|
| 945 | if (aio_ex->fsp == fsp) {
|
---|
| 946 | aiocb_list[i++] = &aio_ex->acb;
|
---|
| 947 | }
|
---|
| 948 | }
|
---|
| 949 |
|
---|
| 950 | /* Now wait up to seconds_left for completion. */
|
---|
| 951 | ts.tv_sec = seconds_left;
|
---|
| 952 | ts.tv_nsec = 0;
|
---|
| 953 |
|
---|
| 954 | DEBUG(10,("wait_for_aio_completion: %d events, doing a wait "
|
---|
| 955 | "of %d seconds.\n",
|
---|
| 956 | aio_completion_count, seconds_left ));
|
---|
| 957 |
|
---|
| 958 | err = SMB_VFS_AIO_SUSPEND(fsp, aiocb_list,
|
---|
| 959 | aio_completion_count, &ts);
|
---|
| 960 |
|
---|
| 961 | DEBUG(10,("wait_for_aio_completion: returned err = %d, "
|
---|
| 962 | "errno = %s\n", err, strerror(errno) ));
|
---|
| 963 |
|
---|
| 964 | if (err == -1 && errno == EAGAIN) {
|
---|
| 965 | DEBUG(0,("wait_for_aio_completion: aio_suspend timed "
|
---|
| 966 | "out waiting for %d events after a wait of "
|
---|
| 967 | "%d seconds\n", aio_completion_count,
|
---|
| 968 | seconds_left));
|
---|
| 969 | /* Timeout. */
|
---|
| 970 | cancel_aio_by_fsp(fsp);
|
---|
| 971 | SAFE_FREE(aiocb_list);
|
---|
| 972 | return EIO;
|
---|
| 973 | }
|
---|
| 974 |
|
---|
| 975 | /* One or more events might have completed - process them if
|
---|
| 976 | * so. */
|
---|
| 977 | for( i = 0; i < aio_completion_count; i++) {
|
---|
| 978 | aio_ex = (struct aio_extra *)aiocb_list[i]->aio_sigevent.sigev_value.sival_ptr;
|
---|
| 979 |
|
---|
| 980 | if (!handle_aio_completed(aio_ex, &err)) {
|
---|
| 981 | continue;
|
---|
| 982 | }
|
---|
| 983 | TALLOC_FREE(aio_ex);
|
---|
| 984 | }
|
---|
| 985 |
|
---|
| 986 | SAFE_FREE(aiocb_list);
|
---|
| 987 | seconds_left = SMB_TIME_FOR_AIO_COMPLETE_WAIT
|
---|
| 988 | - (time_mono(NULL) - start_time);
|
---|
| 989 | }
|
---|
| 990 |
|
---|
| 991 | /* We timed out - we don't know why. Return ret if already an error,
|
---|
| 992 | * else EIO. */
|
---|
| 993 | DEBUG(10,("wait_for_aio_completion: aio_suspend timed out waiting "
|
---|
| 994 | "for %d events\n",
|
---|
| 995 | aio_completion_count));
|
---|
| 996 |
|
---|
| 997 | return EIO;
|
---|
| 998 | }
|
---|
| 999 |
|
---|
| 1000 | /****************************************************************************
|
---|
| 1001 | Cancel any outstanding aio requests. The client doesn't care about the reply.
|
---|
| 1002 | *****************************************************************************/
|
---|
| 1003 |
|
---|
| 1004 | void cancel_aio_by_fsp(files_struct *fsp)
|
---|
| 1005 | {
|
---|
| 1006 | struct aio_extra *aio_ex;
|
---|
| 1007 |
|
---|
| 1008 | for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
|
---|
| 1009 | if (aio_ex->fsp == fsp) {
|
---|
| 1010 | /* Unlock now we're done. */
|
---|
| 1011 | SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &aio_ex->lock);
|
---|
| 1012 |
|
---|
| 1013 | /* Don't delete the aio_extra record as we may have
|
---|
| 1014 | completed and don't yet know it. Just do the
|
---|
| 1015 | aio_cancel call and return. */
|
---|
| 1016 | SMB_VFS_AIO_CANCEL(fsp, &aio_ex->acb);
|
---|
| 1017 | aio_ex->fsp = NULL; /* fsp will be closed when we
|
---|
| 1018 | * return. */
|
---|
| 1019 | }
|
---|
| 1020 | }
|
---|
| 1021 | }
|
---|
| 1022 |
|
---|
| 1023 | #else
|
---|
| 1024 | NTSTATUS schedule_aio_read_and_X(connection_struct *conn,
|
---|
| 1025 | struct smb_request *smbreq,
|
---|
| 1026 | files_struct *fsp, SMB_OFF_T startpos,
|
---|
| 1027 | size_t smb_maxcnt)
|
---|
| 1028 | {
|
---|
| 1029 | return NT_STATUS_RETRY;
|
---|
| 1030 | }
|
---|
| 1031 |
|
---|
| 1032 | NTSTATUS schedule_aio_write_and_X(connection_struct *conn,
|
---|
| 1033 | struct smb_request *smbreq,
|
---|
| 1034 | files_struct *fsp, char *data,
|
---|
| 1035 | SMB_OFF_T startpos,
|
---|
| 1036 | size_t numtowrite)
|
---|
| 1037 | {
|
---|
| 1038 | return NT_STATUS_RETRY;
|
---|
| 1039 | }
|
---|
| 1040 |
|
---|
| 1041 | NTSTATUS schedule_smb2_aio_read(connection_struct *conn,
|
---|
| 1042 | struct smb_request *smbreq,
|
---|
| 1043 | files_struct *fsp,
|
---|
| 1044 | TALLOC_CTX *ctx,
|
---|
| 1045 | DATA_BLOB *preadbuf,
|
---|
| 1046 | SMB_OFF_T startpos,
|
---|
| 1047 | size_t smb_maxcnt)
|
---|
| 1048 | {
|
---|
| 1049 | return NT_STATUS_RETRY;
|
---|
| 1050 | }
|
---|
| 1051 |
|
---|
| 1052 | NTSTATUS schedule_aio_smb2_write(connection_struct *conn,
|
---|
| 1053 | struct smb_request *smbreq,
|
---|
| 1054 | files_struct *fsp,
|
---|
| 1055 | uint64_t in_offset,
|
---|
| 1056 | DATA_BLOB in_data,
|
---|
| 1057 | bool write_through)
|
---|
| 1058 | {
|
---|
| 1059 | return NT_STATUS_RETRY;
|
---|
| 1060 | }
|
---|
| 1061 |
|
---|
| 1062 | void cancel_aio_by_fsp(files_struct *fsp)
|
---|
| 1063 | {
|
---|
| 1064 | }
|
---|
| 1065 |
|
---|
| 1066 | int wait_for_aio_completion(files_struct *fsp)
|
---|
| 1067 | {
|
---|
| 1068 | return 0;
|
---|
| 1069 | }
|
---|
| 1070 |
|
---|
| 1071 | void smbd_aio_complete_mid(uint64_t mid);
|
---|
| 1072 |
|
---|
| 1073 | #endif
|
---|