| 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/globals.h"
|
|---|
| 23 |
|
|---|
| 24 | #if defined(WITH_AIO)
|
|---|
| 25 |
|
|---|
| 26 | /* The signal we'll use to signify aio done. */
|
|---|
| 27 | #ifndef RT_SIGNAL_AIO
|
|---|
| 28 | #define RT_SIGNAL_AIO (SIGRTMIN+3)
|
|---|
| 29 | #endif
|
|---|
| 30 |
|
|---|
| 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
|
|---|
| 37 |
|
|---|
| 38 | /****************************************************************************
|
|---|
| 39 | The buffer we keep around whilst an aio request is in process.
|
|---|
| 40 | *****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | struct aio_extra {
|
|---|
| 43 | struct aio_extra *next, *prev;
|
|---|
| 44 | SMB_STRUCT_AIOCB acb;
|
|---|
| 45 | files_struct *fsp;
|
|---|
| 46 | struct smb_request *req;
|
|---|
| 47 | char *outbuf;
|
|---|
| 48 | int (*handle_completion)(struct aio_extra *ex, int errcode);
|
|---|
| 49 | };
|
|---|
| 50 |
|
|---|
| 51 | static int handle_aio_read_complete(struct aio_extra *aio_ex, int errcode);
|
|---|
| 52 | static int handle_aio_write_complete(struct aio_extra *aio_ex, int errcode);
|
|---|
| 53 |
|
|---|
| 54 | static int aio_extra_destructor(struct aio_extra *aio_ex)
|
|---|
| 55 | {
|
|---|
| 56 | DLIST_REMOVE(aio_list_head, aio_ex);
|
|---|
| 57 | return 0;
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | /****************************************************************************
|
|---|
| 61 | Create the extended aio struct we must keep around for the lifetime
|
|---|
| 62 | of the aio call.
|
|---|
| 63 | *****************************************************************************/
|
|---|
| 64 |
|
|---|
| 65 | static struct aio_extra *create_aio_extra(files_struct *fsp, size_t buflen)
|
|---|
| 66 | {
|
|---|
| 67 | struct aio_extra *aio_ex = TALLOC_ZERO_P(NULL, struct aio_extra);
|
|---|
| 68 |
|
|---|
| 69 | if (!aio_ex) {
|
|---|
| 70 | return NULL;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | /* The output buffer stored in the aio_ex is the start of
|
|---|
| 74 | the smb return buffer. The buffer used in the acb
|
|---|
| 75 | is the start of the reply data portion of that buffer. */
|
|---|
| 76 |
|
|---|
| 77 | aio_ex->outbuf = TALLOC_ARRAY(aio_ex, char, buflen);
|
|---|
| 78 | if (!aio_ex->outbuf) {
|
|---|
| 79 | TALLOC_FREE(aio_ex);
|
|---|
| 80 | return NULL;
|
|---|
| 81 | }
|
|---|
| 82 | DLIST_ADD(aio_list_head, aio_ex);
|
|---|
| 83 | talloc_set_destructor(aio_ex, aio_extra_destructor);
|
|---|
| 84 | aio_ex->fsp = fsp;
|
|---|
| 85 | return aio_ex;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | /****************************************************************************
|
|---|
| 89 | Given the mid find the extended aio struct containing it.
|
|---|
| 90 | *****************************************************************************/
|
|---|
| 91 |
|
|---|
| 92 | static struct aio_extra *find_aio_ex(uint16 mid)
|
|---|
| 93 | {
|
|---|
| 94 | struct aio_extra *p;
|
|---|
| 95 |
|
|---|
| 96 | for( p = aio_list_head; p; p = p->next) {
|
|---|
| 97 | if (mid == p->req->mid) {
|
|---|
| 98 | return p;
|
|---|
| 99 | }
|
|---|
| 100 | }
|
|---|
| 101 | return NULL;
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | /****************************************************************************
|
|---|
| 105 | We can have these many aio buffers in flight.
|
|---|
| 106 | *****************************************************************************/
|
|---|
| 107 |
|
|---|
| 108 | /****************************************************************************
|
|---|
| 109 | Set up an aio request from a SMBreadX call.
|
|---|
| 110 | *****************************************************************************/
|
|---|
| 111 |
|
|---|
| 112 | bool schedule_aio_read_and_X(connection_struct *conn,
|
|---|
| 113 | struct smb_request *req,
|
|---|
| 114 | files_struct *fsp, SMB_OFF_T startpos,
|
|---|
| 115 | size_t smb_maxcnt)
|
|---|
| 116 | {
|
|---|
| 117 | struct aio_extra *aio_ex;
|
|---|
| 118 | SMB_STRUCT_AIOCB *a;
|
|---|
| 119 | size_t bufsize;
|
|---|
| 120 | size_t min_aio_read_size = lp_aio_read_size(SNUM(conn));
|
|---|
| 121 | int ret;
|
|---|
| 122 |
|
|---|
| 123 | if (fsp->base_fsp != NULL) {
|
|---|
| 124 | /* No AIO on streams yet */
|
|---|
| 125 | DEBUG(10, ("AIO on streams not yet supported\n"));
|
|---|
| 126 | return false;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | if ((!min_aio_read_size || (smb_maxcnt < min_aio_read_size))
|
|---|
| 130 | && !SMB_VFS_AIO_FORCE(fsp)) {
|
|---|
| 131 | /* Too small a read for aio request. */
|
|---|
| 132 | DEBUG(10,("schedule_aio_read_and_X: read size (%u) too small "
|
|---|
| 133 | "for minimum aio_read of %u\n",
|
|---|
| 134 | (unsigned int)smb_maxcnt,
|
|---|
| 135 | (unsigned int)min_aio_read_size ));
|
|---|
| 136 | return False;
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | /* Only do this on non-chained and non-chaining reads not using the
|
|---|
| 140 | * write cache. */
|
|---|
| 141 | if (req_is_in_chain(req) || (lp_write_cache_size(SNUM(conn)) != 0)) {
|
|---|
| 142 | return False;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | if (outstanding_aio_calls >= aio_pending_size) {
|
|---|
| 146 | DEBUG(10,("schedule_aio_read_and_X: Already have %d aio "
|
|---|
| 147 | "activities outstanding.\n",
|
|---|
| 148 | outstanding_aio_calls ));
|
|---|
| 149 | return False;
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | /* The following is safe from integer wrap as we've already checked
|
|---|
| 153 | smb_maxcnt is 128k or less. Wct is 12 for read replies */
|
|---|
| 154 |
|
|---|
| 155 | bufsize = smb_size + 12 * 2 + smb_maxcnt;
|
|---|
| 156 |
|
|---|
| 157 | if ((aio_ex = create_aio_extra(fsp, bufsize)) == NULL) {
|
|---|
| 158 | DEBUG(10,("schedule_aio_read_and_X: malloc fail.\n"));
|
|---|
| 159 | return False;
|
|---|
| 160 | }
|
|---|
| 161 | aio_ex->handle_completion = handle_aio_read_complete;
|
|---|
| 162 |
|
|---|
| 163 | construct_reply_common_req(req, aio_ex->outbuf);
|
|---|
| 164 | srv_set_message(aio_ex->outbuf, 12, 0, True);
|
|---|
| 165 | SCVAL(aio_ex->outbuf,smb_vwv0,0xFF); /* Never a chained reply. */
|
|---|
| 166 |
|
|---|
| 167 | a = &aio_ex->acb;
|
|---|
| 168 |
|
|---|
| 169 | /* Now set up the aio record for the read call. */
|
|---|
| 170 |
|
|---|
| 171 | a->aio_fildes = fsp->fh->fd;
|
|---|
| 172 | a->aio_buf = smb_buf(aio_ex->outbuf);
|
|---|
| 173 | a->aio_nbytes = smb_maxcnt;
|
|---|
| 174 | a->aio_offset = startpos;
|
|---|
| 175 | a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
|
|---|
| 176 | a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
|
|---|
| 177 | a->aio_sigevent.sigev_value.sival_int = req->mid;
|
|---|
| 178 |
|
|---|
| 179 | ret = SMB_VFS_AIO_READ(fsp, a);
|
|---|
| 180 | if (ret == -1) {
|
|---|
| 181 | DEBUG(0,("schedule_aio_read_and_X: aio_read failed. "
|
|---|
| 182 | "Error %s\n", strerror(errno) ));
|
|---|
| 183 | TALLOC_FREE(aio_ex);
|
|---|
| 184 | return False;
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | outstanding_aio_calls++;
|
|---|
| 188 | aio_ex->req = talloc_move(aio_ex, &req);
|
|---|
| 189 |
|
|---|
| 190 | DEBUG(10,("schedule_aio_read_and_X: scheduled aio_read for file %s, "
|
|---|
| 191 | "offset %.0f, len = %u (mid = %u)\n",
|
|---|
| 192 | fsp_str_dbg(fsp), (double)startpos, (unsigned int)smb_maxcnt,
|
|---|
| 193 | (unsigned int)aio_ex->req->mid ));
|
|---|
| 194 |
|
|---|
| 195 | return True;
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | /****************************************************************************
|
|---|
| 199 | Set up an aio request from a SMBwriteX call.
|
|---|
| 200 | *****************************************************************************/
|
|---|
| 201 |
|
|---|
| 202 | bool schedule_aio_write_and_X(connection_struct *conn,
|
|---|
| 203 | struct smb_request *req,
|
|---|
| 204 | files_struct *fsp, char *data,
|
|---|
| 205 | SMB_OFF_T startpos,
|
|---|
| 206 | size_t numtowrite)
|
|---|
| 207 | {
|
|---|
| 208 | struct aio_extra *aio_ex;
|
|---|
| 209 | SMB_STRUCT_AIOCB *a;
|
|---|
| 210 | size_t bufsize;
|
|---|
| 211 | bool write_through = BITSETW(req->vwv+7,0);
|
|---|
| 212 | size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
|
|---|
| 213 | int ret;
|
|---|
| 214 |
|
|---|
| 215 | if (fsp->base_fsp != NULL) {
|
|---|
| 216 | /* No AIO on streams yet */
|
|---|
| 217 | DEBUG(10, ("AIO on streams not yet supported\n"));
|
|---|
| 218 | return false;
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | if ((!min_aio_write_size || (numtowrite < min_aio_write_size))
|
|---|
| 222 | && !SMB_VFS_AIO_FORCE(fsp)) {
|
|---|
| 223 | /* Too small a write for aio request. */
|
|---|
| 224 | DEBUG(10,("schedule_aio_write_and_X: write size (%u) too "
|
|---|
| 225 | "small for minimum aio_write of %u\n",
|
|---|
| 226 | (unsigned int)numtowrite,
|
|---|
| 227 | (unsigned int)min_aio_write_size ));
|
|---|
| 228 | return False;
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | /* Only do this on non-chained and non-chaining reads not using the
|
|---|
| 232 | * write cache. */
|
|---|
| 233 | if (req_is_in_chain(req) || (lp_write_cache_size(SNUM(conn)) != 0)) {
|
|---|
| 234 | return False;
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 | if (outstanding_aio_calls >= aio_pending_size) {
|
|---|
| 238 | DEBUG(3,("schedule_aio_write_and_X: Already have %d aio "
|
|---|
| 239 | "activities outstanding.\n",
|
|---|
| 240 | outstanding_aio_calls ));
|
|---|
| 241 | DEBUG(10,("schedule_aio_write_and_X: failed to schedule "
|
|---|
| 242 | "aio_write for file %s, offset %.0f, len = %u "
|
|---|
| 243 | "(mid = %u)\n",
|
|---|
| 244 | fsp_str_dbg(fsp), (double)startpos,
|
|---|
| 245 | (unsigned int)numtowrite,
|
|---|
| 246 | (unsigned int)req->mid ));
|
|---|
| 247 | return False;
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | bufsize = smb_size + 6*2;
|
|---|
| 251 |
|
|---|
| 252 | if (!(aio_ex = create_aio_extra(fsp, bufsize))) {
|
|---|
| 253 | DEBUG(0,("schedule_aio_write_and_X: malloc fail.\n"));
|
|---|
| 254 | return False;
|
|---|
| 255 | }
|
|---|
| 256 | aio_ex->handle_completion = handle_aio_write_complete;
|
|---|
| 257 |
|
|---|
| 258 | construct_reply_common_req(req, aio_ex->outbuf);
|
|---|
| 259 | srv_set_message(aio_ex->outbuf, 6, 0, True);
|
|---|
| 260 | SCVAL(aio_ex->outbuf,smb_vwv0,0xFF); /* Never a chained reply. */
|
|---|
| 261 |
|
|---|
| 262 | a = &aio_ex->acb;
|
|---|
| 263 |
|
|---|
| 264 | /* Now set up the aio record for the write call. */
|
|---|
| 265 |
|
|---|
| 266 | a->aio_fildes = fsp->fh->fd;
|
|---|
| 267 | a->aio_buf = data;
|
|---|
| 268 | a->aio_nbytes = numtowrite;
|
|---|
| 269 | a->aio_offset = startpos;
|
|---|
| 270 | a->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
|
|---|
| 271 | a->aio_sigevent.sigev_signo = RT_SIGNAL_AIO;
|
|---|
| 272 | a->aio_sigevent.sigev_value.sival_int = req->mid;
|
|---|
| 273 |
|
|---|
| 274 | ret = SMB_VFS_AIO_WRITE(fsp, a);
|
|---|
| 275 | if (ret == -1) {
|
|---|
| 276 | DEBUG(3,("schedule_aio_wrote_and_X: aio_write failed. "
|
|---|
| 277 | "Error %s\n", strerror(errno) ));
|
|---|
| 278 | TALLOC_FREE(aio_ex);
|
|---|
| 279 | return False;
|
|---|
| 280 | }
|
|---|
| 281 |
|
|---|
| 282 | outstanding_aio_calls++;
|
|---|
| 283 | aio_ex->req = talloc_move(aio_ex, &req);
|
|---|
| 284 |
|
|---|
| 285 | /* This should actually be improved to span the write. */
|
|---|
| 286 | contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WRITE);
|
|---|
| 287 | contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WRITE);
|
|---|
| 288 |
|
|---|
| 289 | if (!write_through && !lp_syncalways(SNUM(fsp->conn))
|
|---|
| 290 | && fsp->aio_write_behind) {
|
|---|
| 291 | /* Lie to the client and immediately claim we finished the
|
|---|
| 292 | * write. */
|
|---|
| 293 | SSVAL(aio_ex->outbuf,smb_vwv2,numtowrite);
|
|---|
| 294 | SSVAL(aio_ex->outbuf,smb_vwv4,(numtowrite>>16)&1);
|
|---|
| 295 | show_msg(aio_ex->outbuf);
|
|---|
| 296 | if (!srv_send_smb(smbd_server_fd(),aio_ex->outbuf,
|
|---|
| 297 | true, aio_ex->req->seqnum+1,
|
|---|
| 298 | IS_CONN_ENCRYPTED(fsp->conn),
|
|---|
| 299 | &aio_ex->req->pcd)) {
|
|---|
| 300 | exit_server_cleanly("handle_aio_write: srv_send_smb "
|
|---|
| 301 | "failed.");
|
|---|
| 302 | }
|
|---|
| 303 | DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write "
|
|---|
| 304 | "behind for file %s\n", fsp_str_dbg(fsp)));
|
|---|
| 305 | }
|
|---|
| 306 |
|
|---|
| 307 | DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write for file "
|
|---|
| 308 | "%s, offset %.0f, len = %u (mid = %u) "
|
|---|
| 309 | "outstanding_aio_calls = %d\n",
|
|---|
| 310 | fsp_str_dbg(fsp), (double)startpos, (unsigned int)numtowrite,
|
|---|
| 311 | (unsigned int)aio_ex->req->mid, outstanding_aio_calls ));
|
|---|
| 312 |
|
|---|
| 313 | return True;
|
|---|
| 314 | }
|
|---|
| 315 |
|
|---|
| 316 |
|
|---|
| 317 | /****************************************************************************
|
|---|
| 318 | Complete the read and return the data or error back to the client.
|
|---|
| 319 | Returns errno or zero if all ok.
|
|---|
| 320 | *****************************************************************************/
|
|---|
| 321 |
|
|---|
| 322 | static int handle_aio_read_complete(struct aio_extra *aio_ex, int errcode)
|
|---|
| 323 | {
|
|---|
| 324 | int outsize;
|
|---|
| 325 | char *outbuf = aio_ex->outbuf;
|
|---|
| 326 | char *data = smb_buf(outbuf);
|
|---|
| 327 | ssize_t nread = SMB_VFS_AIO_RETURN(aio_ex->fsp,&aio_ex->acb);
|
|---|
| 328 |
|
|---|
| 329 | if (nread < 0) {
|
|---|
| 330 | /* We're relying here on the fact that if the fd is
|
|---|
| 331 | closed then the aio will complete and aio_return
|
|---|
| 332 | will return an error. Hopefully this is
|
|---|
| 333 | true.... JRA. */
|
|---|
| 334 |
|
|---|
| 335 | DEBUG( 3,( "handle_aio_read_complete: file %s nread == %d. "
|
|---|
| 336 | "Error = %s\n",
|
|---|
| 337 | fsp_str_dbg(aio_ex->fsp), (int)nread, strerror(errcode)));
|
|---|
| 338 |
|
|---|
| 339 | ERROR_NT(map_nt_error_from_unix(errcode));
|
|---|
| 340 | outsize = srv_set_message(outbuf,0,0,true);
|
|---|
| 341 | } else {
|
|---|
| 342 | outsize = srv_set_message(outbuf,12,nread,False);
|
|---|
| 343 | SSVAL(outbuf,smb_vwv2,0xFFFF); /* Remaining - must be * -1. */
|
|---|
| 344 | SSVAL(outbuf,smb_vwv5,nread);
|
|---|
| 345 | SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
|
|---|
| 346 | SSVAL(outbuf,smb_vwv7,((nread >> 16) & 1));
|
|---|
| 347 | SSVAL(smb_buf(outbuf),-2,nread);
|
|---|
| 348 |
|
|---|
| 349 | aio_ex->fsp->fh->pos = aio_ex->acb.aio_offset + nread;
|
|---|
| 350 | aio_ex->fsp->fh->position_information = aio_ex->fsp->fh->pos;
|
|---|
| 351 |
|
|---|
| 352 | DEBUG( 3, ( "handle_aio_read_complete file %s max=%d "
|
|---|
| 353 | "nread=%d\n",
|
|---|
| 354 | fsp_str_dbg(aio_ex->fsp),
|
|---|
| 355 | (int)aio_ex->acb.aio_nbytes, (int)nread ) );
|
|---|
| 356 |
|
|---|
| 357 | }
|
|---|
| 358 | smb_setlen(outbuf,outsize - 4);
|
|---|
| 359 | show_msg(outbuf);
|
|---|
| 360 | if (!srv_send_smb(smbd_server_fd(),outbuf,
|
|---|
| 361 | true, aio_ex->req->seqnum+1,
|
|---|
| 362 | IS_CONN_ENCRYPTED(aio_ex->fsp->conn), NULL)) {
|
|---|
| 363 | exit_server_cleanly("handle_aio_read_complete: srv_send_smb "
|
|---|
| 364 | "failed.");
|
|---|
| 365 | }
|
|---|
| 366 |
|
|---|
| 367 | DEBUG(10,("handle_aio_read_complete: scheduled aio_read completed "
|
|---|
| 368 | "for file %s, offset %.0f, len = %u\n",
|
|---|
| 369 | fsp_str_dbg(aio_ex->fsp), (double)aio_ex->acb.aio_offset,
|
|---|
| 370 | (unsigned int)nread ));
|
|---|
| 371 |
|
|---|
| 372 | return errcode;
|
|---|
| 373 | }
|
|---|
| 374 |
|
|---|
| 375 | /****************************************************************************
|
|---|
| 376 | Complete the write and return the data or error back to the client.
|
|---|
| 377 | Returns error code or zero if all ok.
|
|---|
| 378 | *****************************************************************************/
|
|---|
| 379 |
|
|---|
| 380 | static int handle_aio_write_complete(struct aio_extra *aio_ex, int errcode)
|
|---|
| 381 | {
|
|---|
| 382 | files_struct *fsp = aio_ex->fsp;
|
|---|
| 383 | char *outbuf = aio_ex->outbuf;
|
|---|
| 384 | ssize_t numtowrite = aio_ex->acb.aio_nbytes;
|
|---|
| 385 | ssize_t nwritten = SMB_VFS_AIO_RETURN(fsp,&aio_ex->acb);
|
|---|
| 386 |
|
|---|
| 387 | if (fsp->aio_write_behind) {
|
|---|
| 388 | if (nwritten != numtowrite) {
|
|---|
| 389 | if (nwritten == -1) {
|
|---|
| 390 | DEBUG(5,("handle_aio_write_complete: "
|
|---|
| 391 | "aio_write_behind failed ! File %s "
|
|---|
| 392 | "is corrupt ! Error %s\n",
|
|---|
| 393 | fsp_str_dbg(fsp), strerror(errcode)));
|
|---|
| 394 | } else {
|
|---|
| 395 | DEBUG(0,("handle_aio_write_complete: "
|
|---|
| 396 | "aio_write_behind failed ! File %s "
|
|---|
| 397 | "is corrupt ! Wanted %u bytes but "
|
|---|
| 398 | "only wrote %d\n", fsp_str_dbg(fsp),
|
|---|
| 399 | (unsigned int)numtowrite,
|
|---|
| 400 | (int)nwritten ));
|
|---|
| 401 | errcode = EIO;
|
|---|
| 402 | }
|
|---|
| 403 | } else {
|
|---|
| 404 | DEBUG(10,("handle_aio_write_complete: "
|
|---|
| 405 | "aio_write_behind completed for file %s\n",
|
|---|
| 406 | fsp_str_dbg(fsp)));
|
|---|
| 407 | }
|
|---|
| 408 | /* TODO: should no return 0 in case of an error !!! */
|
|---|
| 409 | return 0;
|
|---|
| 410 | }
|
|---|
| 411 |
|
|---|
| 412 | /* We don't need outsize or set_message here as we've already set the
|
|---|
| 413 | fixed size length when we set up the aio call. */
|
|---|
| 414 |
|
|---|
| 415 | if(nwritten == -1) {
|
|---|
| 416 | DEBUG( 3,( "handle_aio_write: file %s wanted %u bytes. "
|
|---|
| 417 | "nwritten == %d. Error = %s\n",
|
|---|
| 418 | fsp_str_dbg(fsp), (unsigned int)numtowrite,
|
|---|
| 419 | (int)nwritten, strerror(errcode) ));
|
|---|
| 420 |
|
|---|
| 421 | ERROR_NT(map_nt_error_from_unix(errcode));
|
|---|
| 422 | srv_set_message(outbuf,0,0,true);
|
|---|
| 423 | } else {
|
|---|
| 424 | bool write_through = BITSETW(aio_ex->req->vwv+7,0);
|
|---|
| 425 | NTSTATUS status;
|
|---|
| 426 |
|
|---|
| 427 | SSVAL(outbuf,smb_vwv2,nwritten);
|
|---|
| 428 | SSVAL(outbuf,smb_vwv4,(nwritten>>16)&1);
|
|---|
| 429 | if (nwritten < (ssize_t)numtowrite) {
|
|---|
| 430 | SCVAL(outbuf,smb_rcls,ERRHRD);
|
|---|
| 431 | SSVAL(outbuf,smb_err,ERRdiskfull);
|
|---|
| 432 | }
|
|---|
| 433 |
|
|---|
| 434 | DEBUG(3,("handle_aio_write: fnum=%d num=%d wrote=%d\n",
|
|---|
| 435 | fsp->fnum, (int)numtowrite, (int)nwritten));
|
|---|
| 436 | status = sync_file(fsp->conn,fsp, write_through);
|
|---|
| 437 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 438 | errcode = errno;
|
|---|
| 439 | ERROR_BOTH(map_nt_error_from_unix(errcode),
|
|---|
| 440 | ERRHRD, ERRdiskfull);
|
|---|
| 441 | srv_set_message(outbuf,0,0,true);
|
|---|
| 442 | DEBUG(5,("handle_aio_write: sync_file for %s returned %s\n",
|
|---|
| 443 | fsp_str_dbg(fsp), nt_errstr(status)));
|
|---|
| 444 | }
|
|---|
| 445 |
|
|---|
| 446 | aio_ex->fsp->fh->pos = aio_ex->acb.aio_offset + nwritten;
|
|---|
| 447 | }
|
|---|
| 448 |
|
|---|
| 449 | show_msg(outbuf);
|
|---|
| 450 | if (!srv_send_smb(smbd_server_fd(),outbuf,
|
|---|
| 451 | true, aio_ex->req->seqnum+1,
|
|---|
| 452 | IS_CONN_ENCRYPTED(fsp->conn),
|
|---|
| 453 | NULL)) {
|
|---|
| 454 | exit_server_cleanly("handle_aio_write: srv_send_smb failed.");
|
|---|
| 455 | }
|
|---|
| 456 |
|
|---|
| 457 | DEBUG(10,("handle_aio_write_complete: scheduled aio_write completed "
|
|---|
| 458 | "for file %s, offset %.0f, requested %u, written = %u\n",
|
|---|
| 459 | fsp_str_dbg(fsp), (double)aio_ex->acb.aio_offset,
|
|---|
| 460 | (unsigned int)numtowrite, (unsigned int)nwritten ));
|
|---|
| 461 |
|
|---|
| 462 | return errcode;
|
|---|
| 463 | }
|
|---|
| 464 |
|
|---|
| 465 | /****************************************************************************
|
|---|
| 466 | Handle any aio completion. Returns True if finished (and sets *perr if err
|
|---|
| 467 | was non-zero), False if not.
|
|---|
| 468 | *****************************************************************************/
|
|---|
| 469 |
|
|---|
| 470 | static bool handle_aio_completed(struct aio_extra *aio_ex, int *perr)
|
|---|
| 471 | {
|
|---|
| 472 | int err;
|
|---|
| 473 |
|
|---|
| 474 | if(!aio_ex) {
|
|---|
| 475 | DEBUG(3, ("handle_aio_completed: Non-existing aio_ex passed\n"));
|
|---|
| 476 | return false;
|
|---|
| 477 | }
|
|---|
| 478 |
|
|---|
| 479 | /* Ensure the operation has really completed. */
|
|---|
| 480 | err = SMB_VFS_AIO_ERROR(aio_ex->fsp, &aio_ex->acb);
|
|---|
| 481 | if (err == EINPROGRESS) {
|
|---|
| 482 | DEBUG(10,( "handle_aio_completed: operation mid %u still in "
|
|---|
| 483 | "process for file %s\n",
|
|---|
| 484 | aio_ex->req->mid, fsp_str_dbg(aio_ex->fsp)));
|
|---|
| 485 | return False;
|
|---|
| 486 | } else if (err == ECANCELED) {
|
|---|
| 487 | /* If error is ECANCELED then don't return anything to the
|
|---|
| 488 | * client. */
|
|---|
| 489 | DEBUG(10,( "handle_aio_completed: operation mid %u"
|
|---|
| 490 | " canceled\n", aio_ex->req->mid));
|
|---|
| 491 | return True;
|
|---|
| 492 | }
|
|---|
| 493 |
|
|---|
| 494 | err = aio_ex->handle_completion(aio_ex, err);
|
|---|
| 495 | if (err) {
|
|---|
| 496 | *perr = err; /* Only save non-zero errors. */
|
|---|
| 497 | }
|
|---|
| 498 |
|
|---|
| 499 | return True;
|
|---|
| 500 | }
|
|---|
| 501 |
|
|---|
| 502 | /****************************************************************************
|
|---|
| 503 | Handle any aio completion inline.
|
|---|
| 504 | *****************************************************************************/
|
|---|
| 505 |
|
|---|
| 506 | void smbd_aio_complete_mid(unsigned int mid)
|
|---|
| 507 | {
|
|---|
| 508 | files_struct *fsp = NULL;
|
|---|
| 509 | struct aio_extra *aio_ex = find_aio_ex(mid);
|
|---|
| 510 | int ret = 0;
|
|---|
| 511 |
|
|---|
| 512 | outstanding_aio_calls--;
|
|---|
| 513 |
|
|---|
| 514 | DEBUG(10,("smbd_aio_complete_mid: mid[%u]\n", mid));
|
|---|
| 515 |
|
|---|
| 516 | if (!aio_ex) {
|
|---|
| 517 | DEBUG(3,("smbd_aio_complete_mid: Can't find record to "
|
|---|
| 518 | "match mid %u.\n", mid));
|
|---|
| 519 | return;
|
|---|
| 520 | }
|
|---|
| 521 |
|
|---|
| 522 | fsp = aio_ex->fsp;
|
|---|
| 523 | if (fsp == NULL) {
|
|---|
| 524 | /* file was closed whilst I/O was outstanding. Just
|
|---|
| 525 | * ignore. */
|
|---|
| 526 | DEBUG( 3,( "smbd_aio_complete_mid: file closed whilst "
|
|---|
| 527 | "aio outstanding (mid[%u]).\n", mid));
|
|---|
| 528 | return;
|
|---|
| 529 | }
|
|---|
| 530 |
|
|---|
| 531 | if (!handle_aio_completed(aio_ex, &ret)) {
|
|---|
| 532 | return;
|
|---|
| 533 | }
|
|---|
| 534 |
|
|---|
| 535 | TALLOC_FREE(aio_ex);
|
|---|
| 536 | }
|
|---|
| 537 |
|
|---|
| 538 | static void smbd_aio_signal_handler(struct tevent_context *ev_ctx,
|
|---|
| 539 | struct tevent_signal *se,
|
|---|
| 540 | int signum, int count,
|
|---|
| 541 | void *_info, void *private_data)
|
|---|
| 542 | {
|
|---|
| 543 | siginfo_t *info = (siginfo_t *)_info;
|
|---|
| 544 | unsigned int mid = (unsigned int)info->si_value.sival_int;
|
|---|
| 545 |
|
|---|
| 546 | smbd_aio_complete_mid(mid);
|
|---|
| 547 | }
|
|---|
| 548 |
|
|---|
| 549 | /****************************************************************************
|
|---|
| 550 | We're doing write behind and the client closed the file. Wait up to 30
|
|---|
| 551 | seconds (my arbitrary choice) for the aio to complete. Return 0 if all writes
|
|---|
| 552 | completed, errno to return if not.
|
|---|
| 553 | *****************************************************************************/
|
|---|
| 554 |
|
|---|
| 555 | #define SMB_TIME_FOR_AIO_COMPLETE_WAIT 29
|
|---|
| 556 |
|
|---|
| 557 | int wait_for_aio_completion(files_struct *fsp)
|
|---|
| 558 | {
|
|---|
| 559 | struct aio_extra *aio_ex;
|
|---|
| 560 | const SMB_STRUCT_AIOCB **aiocb_list;
|
|---|
| 561 | int aio_completion_count = 0;
|
|---|
| 562 | time_t start_time = time(NULL);
|
|---|
| 563 | int seconds_left;
|
|---|
| 564 |
|
|---|
| 565 | for (seconds_left = SMB_TIME_FOR_AIO_COMPLETE_WAIT;
|
|---|
| 566 | seconds_left >= 0;) {
|
|---|
| 567 | int err = 0;
|
|---|
| 568 | int i;
|
|---|
| 569 | struct timespec ts;
|
|---|
| 570 |
|
|---|
| 571 | aio_completion_count = 0;
|
|---|
| 572 | for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
|
|---|
| 573 | if (aio_ex->fsp == fsp) {
|
|---|
| 574 | aio_completion_count++;
|
|---|
| 575 | }
|
|---|
| 576 | }
|
|---|
| 577 |
|
|---|
| 578 | if (!aio_completion_count) {
|
|---|
| 579 | return 0;
|
|---|
| 580 | }
|
|---|
| 581 |
|
|---|
| 582 | DEBUG(3,("wait_for_aio_completion: waiting for %d aio events "
|
|---|
| 583 | "to complete.\n", aio_completion_count ));
|
|---|
| 584 |
|
|---|
| 585 | aiocb_list = SMB_MALLOC_ARRAY(const SMB_STRUCT_AIOCB *,
|
|---|
| 586 | aio_completion_count);
|
|---|
| 587 | if (!aiocb_list) {
|
|---|
| 588 | return ENOMEM;
|
|---|
| 589 | }
|
|---|
| 590 |
|
|---|
| 591 | for( i = 0, aio_ex = aio_list_head;
|
|---|
| 592 | aio_ex;
|
|---|
| 593 | aio_ex = aio_ex->next) {
|
|---|
| 594 | if (aio_ex->fsp == fsp) {
|
|---|
| 595 | aiocb_list[i++] = &aio_ex->acb;
|
|---|
| 596 | }
|
|---|
| 597 | }
|
|---|
| 598 |
|
|---|
| 599 | /* Now wait up to seconds_left for completion. */
|
|---|
| 600 | ts.tv_sec = seconds_left;
|
|---|
| 601 | ts.tv_nsec = 0;
|
|---|
| 602 |
|
|---|
| 603 | DEBUG(10,("wait_for_aio_completion: %d events, doing a wait "
|
|---|
| 604 | "of %d seconds.\n",
|
|---|
| 605 | aio_completion_count, seconds_left ));
|
|---|
| 606 |
|
|---|
| 607 | err = SMB_VFS_AIO_SUSPEND(fsp, aiocb_list,
|
|---|
| 608 | aio_completion_count, &ts);
|
|---|
| 609 |
|
|---|
| 610 | DEBUG(10,("wait_for_aio_completion: returned err = %d, "
|
|---|
| 611 | "errno = %s\n", err, strerror(errno) ));
|
|---|
| 612 |
|
|---|
| 613 | if (err == -1 && errno == EAGAIN) {
|
|---|
| 614 | DEBUG(0,("wait_for_aio_completion: aio_suspend timed "
|
|---|
| 615 | "out waiting for %d events after a wait of "
|
|---|
| 616 | "%d seconds\n", aio_completion_count,
|
|---|
| 617 | seconds_left));
|
|---|
| 618 | /* Timeout. */
|
|---|
| 619 | cancel_aio_by_fsp(fsp);
|
|---|
| 620 | SAFE_FREE(aiocb_list);
|
|---|
| 621 | return EIO;
|
|---|
| 622 | }
|
|---|
| 623 |
|
|---|
| 624 | /* One or more events might have completed - process them if
|
|---|
| 625 | * so. */
|
|---|
| 626 | for( i = 0; i < aio_completion_count; i++) {
|
|---|
| 627 | uint16 mid = aiocb_list[i]->aio_sigevent.sigev_value.sival_int;
|
|---|
| 628 |
|
|---|
| 629 | aio_ex = find_aio_ex(mid);
|
|---|
| 630 |
|
|---|
| 631 | if (!aio_ex) {
|
|---|
| 632 | DEBUG(0, ("wait_for_aio_completion: mid %u "
|
|---|
| 633 | "doesn't match an aio record\n",
|
|---|
| 634 | (unsigned int)mid ));
|
|---|
| 635 | continue;
|
|---|
| 636 | }
|
|---|
| 637 |
|
|---|
| 638 | if (!handle_aio_completed(aio_ex, &err)) {
|
|---|
| 639 | continue;
|
|---|
| 640 | }
|
|---|
| 641 | TALLOC_FREE(aio_ex);
|
|---|
| 642 | }
|
|---|
| 643 |
|
|---|
| 644 | SAFE_FREE(aiocb_list);
|
|---|
| 645 | seconds_left = SMB_TIME_FOR_AIO_COMPLETE_WAIT
|
|---|
| 646 | - (time(NULL) - start_time);
|
|---|
| 647 | }
|
|---|
| 648 |
|
|---|
| 649 | /* We timed out - we don't know why. Return ret if already an error,
|
|---|
| 650 | * else EIO. */
|
|---|
| 651 | DEBUG(10,("wait_for_aio_completion: aio_suspend timed out waiting "
|
|---|
| 652 | "for %d events\n",
|
|---|
| 653 | aio_completion_count));
|
|---|
| 654 |
|
|---|
| 655 | return EIO;
|
|---|
| 656 | }
|
|---|
| 657 |
|
|---|
| 658 | /****************************************************************************
|
|---|
| 659 | Cancel any outstanding aio requests. The client doesn't care about the reply.
|
|---|
| 660 | *****************************************************************************/
|
|---|
| 661 |
|
|---|
| 662 | void cancel_aio_by_fsp(files_struct *fsp)
|
|---|
| 663 | {
|
|---|
| 664 | struct aio_extra *aio_ex;
|
|---|
| 665 |
|
|---|
| 666 | for( aio_ex = aio_list_head; aio_ex; aio_ex = aio_ex->next) {
|
|---|
| 667 | if (aio_ex->fsp == fsp) {
|
|---|
| 668 | /* Don't delete the aio_extra record as we may have
|
|---|
| 669 | completed and don't yet know it. Just do the
|
|---|
| 670 | aio_cancel call and return. */
|
|---|
| 671 | SMB_VFS_AIO_CANCEL(fsp, &aio_ex->acb);
|
|---|
| 672 | aio_ex->fsp = NULL; /* fsp will be closed when we
|
|---|
| 673 | * return. */
|
|---|
| 674 | }
|
|---|
| 675 | }
|
|---|
| 676 | }
|
|---|
| 677 |
|
|---|
| 678 | /****************************************************************************
|
|---|
| 679 | Initialize the signal handler for aio read/write.
|
|---|
| 680 | *****************************************************************************/
|
|---|
| 681 |
|
|---|
| 682 | void initialize_async_io_handler(void)
|
|---|
| 683 | {
|
|---|
| 684 | aio_signal_event = tevent_add_signal(smbd_event_context(),
|
|---|
| 685 | smbd_event_context(),
|
|---|
| 686 | RT_SIGNAL_AIO, SA_SIGINFO,
|
|---|
| 687 | smbd_aio_signal_handler,
|
|---|
| 688 | NULL);
|
|---|
| 689 | if (!aio_signal_event) {
|
|---|
| 690 | exit_server("Failed to setup RT_SIGNAL_AIO handler");
|
|---|
| 691 | }
|
|---|
| 692 |
|
|---|
| 693 | /* tevent supports 100 signal with SA_SIGINFO */
|
|---|
| 694 | aio_pending_size = 100;
|
|---|
| 695 | }
|
|---|
| 696 |
|
|---|
| 697 | #else
|
|---|
| 698 | void initialize_async_io_handler(void)
|
|---|
| 699 | {
|
|---|
| 700 | }
|
|---|
| 701 |
|
|---|
| 702 | bool schedule_aio_read_and_X(connection_struct *conn,
|
|---|
| 703 | struct smb_request *req,
|
|---|
| 704 | files_struct *fsp, SMB_OFF_T startpos,
|
|---|
| 705 | size_t smb_maxcnt)
|
|---|
| 706 | {
|
|---|
| 707 | return False;
|
|---|
| 708 | }
|
|---|
| 709 |
|
|---|
| 710 | bool schedule_aio_write_and_X(connection_struct *conn,
|
|---|
| 711 | struct smb_request *req,
|
|---|
| 712 | files_struct *fsp, char *data,
|
|---|
| 713 | SMB_OFF_T startpos,
|
|---|
| 714 | size_t numtowrite)
|
|---|
| 715 | {
|
|---|
| 716 | return False;
|
|---|
| 717 | }
|
|---|
| 718 |
|
|---|
| 719 | void cancel_aio_by_fsp(files_struct *fsp)
|
|---|
| 720 | {
|
|---|
| 721 | }
|
|---|
| 722 |
|
|---|
| 723 | int wait_for_aio_completion(files_struct *fsp)
|
|---|
| 724 | {
|
|---|
| 725 | return ENOSYS;
|
|---|
| 726 | }
|
|---|
| 727 |
|
|---|
| 728 | void smbd_aio_complete_mid(unsigned int mid);
|
|---|
| 729 |
|
|---|
| 730 | #endif
|
|---|