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