2 Unix SMB/Netbios implementation.
4 async_io read handling using POSIX async io.
5 Copyright (C) Jeremy Allison 2005.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "smbd/smbd.h"
23 #include "smbd/globals.h"
24 #include "../lib/util/tevent_ntstatus.h"
25 #include "../lib/util/tevent_unix.h"
27 static void aio_pread_smb1_done(struct tevent_req
*req
);
29 /****************************************************************************
30 Set up an aio request from a SMBreadX call.
31 *****************************************************************************/
33 NTSTATUS
schedule_aio_read_and_X(connection_struct
*conn
,
34 struct smb_request
*smbreq
,
35 files_struct
*fsp
, off_t startpos
,
38 struct aio_extra
*aio_ex
;
40 size_t min_aio_read_size
= lp_aio_read_size(SNUM(conn
));
41 struct tevent_req
*req
;
44 ok
= vfs_valid_pread_range(startpos
, smb_maxcnt
);
46 return NT_STATUS_INVALID_PARAMETER
;
49 if (fsp_is_alternate_stream(fsp
)) {
50 DEBUG(10, ("AIO on streams not yet supported\n"));
51 return NT_STATUS_RETRY
;
54 if ((!min_aio_read_size
|| (smb_maxcnt
< min_aio_read_size
))
55 && !SMB_VFS_AIO_FORCE(fsp
)) {
56 /* Too small a read for aio request. */
57 DEBUG(10,("schedule_aio_read_and_X: read size (%u) too small "
58 "for minimum aio_read of %u\n",
59 (unsigned int)smb_maxcnt
,
60 (unsigned int)min_aio_read_size
));
61 return NT_STATUS_RETRY
;
64 /* Only do this on non-chained and non-chaining reads */
65 if (req_is_in_chain(smbreq
)) {
66 return NT_STATUS_RETRY
;
69 /* The following is safe from integer wrap as we've already checked
70 smb_maxcnt is 128k or less. Wct is 12 for read replies */
72 bufsize
= smb_size
+ 12 * 2 + smb_maxcnt
+ 1 /* padding byte */;
74 if ((aio_ex
= create_aio_extra(NULL
, fsp
, bufsize
)) == NULL
) {
75 DEBUG(10,("schedule_aio_read_and_X: malloc fail.\n"));
76 return NT_STATUS_NO_MEMORY
;
79 construct_smb1_reply_common_req(smbreq
, (char *)aio_ex
->outbuf
.data
);
80 srv_smb1_set_message((char *)aio_ex
->outbuf
.data
, 12, 0, True
);
81 SCVAL(aio_ex
->outbuf
.data
,smb_vwv0
,0xFF); /* Never a chained reply. */
82 SCVAL(smb_buf(aio_ex
->outbuf
.data
), 0, 0); /* padding byte */
84 init_strict_lock_struct(fsp
,
85 (uint64_t)smbreq
->smbpid
,
89 lp_posix_cifsu_locktype(fsp
),
92 /* Take the lock until the AIO completes. */
93 if (!SMB_VFS_STRICT_LOCK_CHECK(conn
, fsp
, &aio_ex
->lock
)) {
95 return NT_STATUS_FILE_LOCK_CONFLICT
;
98 aio_ex
->nbyte
= smb_maxcnt
;
99 aio_ex
->offset
= startpos
;
101 req
= SMB_VFS_PREAD_SEND(aio_ex
, fsp
->conn
->sconn
->ev_ctx
,
103 smb_buf(aio_ex
->outbuf
.data
) + 1 /* pad */,
104 smb_maxcnt
, startpos
);
106 DEBUG(0,("schedule_aio_read_and_X: aio_read failed. "
107 "Error %s\n", strerror(errno
) ));
109 return NT_STATUS_RETRY
;
111 tevent_req_set_callback(req
, aio_pread_smb1_done
, aio_ex
);
113 if (!aio_add_req_to_fsp(fsp
, req
)) {
114 DEBUG(1, ("Could not add req to fsp\n"));
116 return NT_STATUS_RETRY
;
119 aio_ex
->smbreq
= talloc_move(aio_ex
, &smbreq
);
121 DEBUG(10,("schedule_aio_read_and_X: scheduled aio_read for file %s, "
122 "offset %.0f, len = %u (mid = %u)\n",
123 fsp_str_dbg(fsp
), (double)startpos
, (unsigned int)smb_maxcnt
,
124 (unsigned int)aio_ex
->smbreq
->mid
));
129 static void aio_pread_smb1_done(struct tevent_req
*req
)
131 struct aio_extra
*aio_ex
= tevent_req_callback_data(
132 req
, struct aio_extra
);
133 files_struct
*fsp
= aio_ex
->fsp
;
135 char *outbuf
= (char *)aio_ex
->outbuf
.data
;
137 struct vfs_aio_state vfs_aio_state
;
139 nread
= SMB_VFS_PREAD_RECV(req
, &vfs_aio_state
);
142 DEBUG(10, ("pread_recv returned %d, err = %s\n", (int)nread
,
143 (nread
== -1) ? strerror(vfs_aio_state
.error
) : "no error"));
146 DEBUG( 3, ("aio_pread_smb1_done: file closed whilst "
147 "aio outstanding (mid[%llu]).\n",
148 (unsigned long long)aio_ex
->smbreq
->mid
));
154 DEBUG( 3, ("handle_aio_read_complete: file %s nread == %d. "
155 "Error = %s\n", fsp_str_dbg(fsp
), (int)nread
,
156 strerror(vfs_aio_state
.error
)));
158 ERROR_NT(map_nt_error_from_unix(vfs_aio_state
.error
));
159 outsize
= srv_smb1_set_message(outbuf
,0,0,true);
161 outsize
= setup_readX_header(outbuf
, nread
);
163 fh_set_pos(aio_ex
->fsp
->fh
, aio_ex
->offset
+ nread
);
164 fh_set_position_information(aio_ex
->fsp
->fh
,
165 fh_get_pos(aio_ex
->fsp
->fh
));
167 DEBUG( 3, ("handle_aio_read_complete file %s max=%d "
168 "nread=%d\n", fsp_str_dbg(fsp
),
169 (int)aio_ex
->nbyte
, (int)nread
) );
174 DBG_INFO("Invalid outsize (%zu)\n", outsize
);
179 _smb_setlen_large(outbuf
, outsize
);
182 if (!smb1_srv_send(aio_ex
->smbreq
->xconn
,
185 aio_ex
->smbreq
->seqnum
+ 1,
186 IS_CONN_ENCRYPTED(fsp
->conn
))) {
187 exit_server_cleanly("handle_aio_read_complete: smb1_srv_send "
191 DEBUG(10, ("handle_aio_read_complete: scheduled aio_read completed "
192 "for file %s, offset %.0f, len = %u\n",
193 fsp_str_dbg(fsp
), (double)aio_ex
->offset
,
194 (unsigned int)nread
));
199 static void aio_pwrite_smb1_done(struct tevent_req
*req
);
201 /****************************************************************************
202 Set up an aio request from a SMBwriteX call.
203 *****************************************************************************/
205 NTSTATUS
schedule_aio_write_and_X(connection_struct
*conn
,
206 struct smb_request
*smbreq
,
207 files_struct
*fsp
, const char *data
,
211 struct aio_extra
*aio_ex
;
213 size_t min_aio_write_size
= lp_aio_write_size(SNUM(conn
));
214 struct tevent_req
*req
;
216 if (fsp_is_alternate_stream(fsp
)) {
217 DEBUG(10, ("AIO on streams not yet supported\n"));
218 return NT_STATUS_RETRY
;
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 NT_STATUS_RETRY
;
231 /* Only do this on non-chained and non-chaining writes */
232 if (req_is_in_chain(smbreq
)) {
233 return NT_STATUS_RETRY
;
236 bufsize
= smb_size
+ 6*2;
238 if (!(aio_ex
= create_aio_extra(NULL
, fsp
, bufsize
))) {
239 DEBUG(0,("schedule_aio_write_and_X: malloc fail.\n"));
240 return NT_STATUS_NO_MEMORY
;
242 aio_ex
->write_through
= BITSETW(smbreq
->vwv
+7,0);
244 construct_smb1_reply_common_req(smbreq
, (char *)aio_ex
->outbuf
.data
);
245 srv_smb1_set_message((char *)aio_ex
->outbuf
.data
, 6, 0, True
);
246 SCVAL(aio_ex
->outbuf
.data
,smb_vwv0
,0xFF); /* Never a chained reply. */
248 init_strict_lock_struct(fsp
,
249 (uint64_t)smbreq
->smbpid
,
251 (uint64_t)numtowrite
,
253 lp_posix_cifsu_locktype(fsp
),
256 /* Take the lock until the AIO completes. */
257 if (!SMB_VFS_STRICT_LOCK_CHECK(conn
, fsp
, &aio_ex
->lock
)) {
259 return NT_STATUS_FILE_LOCK_CONFLICT
;
262 aio_ex
->nbyte
= numtowrite
;
263 aio_ex
->offset
= startpos
;
265 req
= pwrite_fsync_send(aio_ex
, fsp
->conn
->sconn
->ev_ctx
, fsp
,
266 data
, numtowrite
, startpos
,
267 aio_ex
->write_through
);
269 DEBUG(3,("schedule_aio_wrote_and_X: aio_write failed. "
270 "Error %s\n", strerror(errno
) ));
272 return NT_STATUS_RETRY
;
274 tevent_req_set_callback(req
, aio_pwrite_smb1_done
, aio_ex
);
276 if (!aio_add_req_to_fsp(fsp
, req
)) {
277 DEBUG(1, ("Could not add req to fsp\n"));
279 return NT_STATUS_RETRY
;
282 aio_ex
->smbreq
= talloc_move(aio_ex
, &smbreq
);
284 /* This should actually be improved to span the write. */
285 contend_level2_oplocks_begin(fsp
, LEVEL2_CONTEND_WRITE
);
286 contend_level2_oplocks_end(fsp
, LEVEL2_CONTEND_WRITE
);
288 if (!aio_ex
->write_through
&& !lp_sync_always(SNUM(fsp
->conn
))
289 && fsp
->fsp_flags
.aio_write_behind
)
291 /* Lie to the client and immediately claim we finished the
293 SSVAL(aio_ex
->outbuf
.data
,smb_vwv2
,numtowrite
);
294 SSVAL(aio_ex
->outbuf
.data
,smb_vwv4
,(numtowrite
>>16)&1);
295 show_msg((char *)aio_ex
->outbuf
.data
);
296 if (!smb1_srv_send(aio_ex
->smbreq
->xconn
,
297 (char *)aio_ex
->outbuf
.data
,
299 aio_ex
->smbreq
->seqnum
+ 1,
300 IS_CONN_ENCRYPTED(fsp
->conn
))) {
301 exit_server_cleanly("schedule_aio_write_and_X: "
302 "smb1_srv_send failed.");
304 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write "
305 "behind for file %s\n", fsp_str_dbg(fsp
)));
308 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write for file "
309 "%s, offset %.0f, len = %u (mid = %u)\n",
310 fsp_str_dbg(fsp
), (double)startpos
, (unsigned int)numtowrite
,
311 (unsigned int)aio_ex
->smbreq
->mid
));
316 static void aio_pwrite_smb1_done(struct tevent_req
*req
)
318 struct aio_extra
*aio_ex
= tevent_req_callback_data(
319 req
, struct aio_extra
);
320 files_struct
*fsp
= aio_ex
->fsp
;
321 char *outbuf
= (char *)aio_ex
->outbuf
.data
;
322 ssize_t numtowrite
= aio_ex
->nbyte
;
326 nwritten
= pwrite_fsync_recv(req
, &err
);
329 DEBUG(10, ("pwrite_recv returned %d, err = %s\n", (int)nwritten
,
330 (nwritten
== -1) ? strerror(err
) : "no error"));
333 DEBUG( 3, ("aio_pwrite_smb1_done: file closed whilst "
334 "aio outstanding (mid[%llu]).\n",
335 (unsigned long long)aio_ex
->smbreq
->mid
));
340 mark_file_modified(fsp
);
342 if (fsp
->fsp_flags
.aio_write_behind
) {
344 if (nwritten
!= numtowrite
) {
345 if (nwritten
== -1) {
346 DEBUG(5,("handle_aio_write_complete: "
347 "aio_write_behind failed ! File %s "
348 "is corrupt ! Error %s\n",
349 fsp_str_dbg(fsp
), strerror(err
)));
351 DEBUG(0,("handle_aio_write_complete: "
352 "aio_write_behind failed ! File %s "
353 "is corrupt ! Wanted %u bytes but "
354 "only wrote %d\n", fsp_str_dbg(fsp
),
355 (unsigned int)numtowrite
,
359 DEBUG(10,("handle_aio_write_complete: "
360 "aio_write_behind completed for file %s\n",
363 /* TODO: should no return success in case of an error !!! */
368 /* We don't need outsize or set_message here as we've already set the
369 fixed size length when we set up the aio call. */
371 if (nwritten
== -1) {
372 DEBUG(3, ("handle_aio_write: file %s wanted %u bytes. "
373 "nwritten == %d. Error = %s\n",
374 fsp_str_dbg(fsp
), (unsigned int)numtowrite
,
375 (int)nwritten
, strerror(err
)));
377 ERROR_NT(map_nt_error_from_unix(err
));
378 srv_smb1_set_message(outbuf
,0,0,true);
380 SSVAL(outbuf
,smb_vwv2
,nwritten
);
381 SSVAL(outbuf
,smb_vwv4
,(nwritten
>>16)&1);
382 if (nwritten
< (ssize_t
)numtowrite
) {
383 SCVAL(outbuf
,smb_rcls
,ERRHRD
);
384 SSVAL(outbuf
,smb_err
,ERRdiskfull
);
387 DEBUG(3,("handle_aio_write: %s, num=%d wrote=%d\n",
388 fsp_fnum_dbg(fsp
), (int)numtowrite
, (int)nwritten
));
390 fh_set_pos(aio_ex
->fsp
->fh
, aio_ex
->offset
+ nwritten
);
394 if (!smb1_srv_send(aio_ex
->smbreq
->xconn
,
397 aio_ex
->smbreq
->seqnum
+ 1,
398 IS_CONN_ENCRYPTED(fsp
->conn
))) {
399 exit_server_cleanly("handle_aio_write_complete: "
400 "smb1_srv_send failed.");
403 DEBUG(10, ("handle_aio_write_complete: scheduled aio_write completed "
404 "for file %s, offset %.0f, requested %u, written = %u\n",
405 fsp_str_dbg(fsp
), (double)aio_ex
->offset
,
406 (unsigned int)numtowrite
, (unsigned int)nwritten
));