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