s3: smbd: Correctly process SMB3 POSIX paths in create.
[Samba.git] / source3 / smbd / smb1_aio.c
blob8807fde42b3363d253b0dd8e86dc4c94d095fe7e
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.
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/>.
21 #include "includes.h"
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,
36 size_t smb_maxcnt)
38 struct aio_extra *aio_ex;
39 size_t bufsize;
40 size_t min_aio_read_size = lp_aio_read_size(SNUM(conn));
41 struct tevent_req *req;
42 bool ok;
44 ok = vfs_valid_pread_range(startpos, smb_maxcnt);
45 if (!ok) {
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,
86 (uint64_t)startpos,
87 (uint64_t)smb_maxcnt,
88 READ_LOCK,
89 lp_posix_cifsu_locktype(fsp),
90 &aio_ex->lock);
92 /* Take the lock until the AIO completes. */
93 if (!SMB_VFS_STRICT_LOCK_CHECK(conn, fsp, &aio_ex->lock)) {
94 TALLOC_FREE(aio_ex);
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,
102 fsp,
103 smb_buf(aio_ex->outbuf.data) + 1 /* pad */,
104 smb_maxcnt, startpos);
105 if (req == NULL) {
106 DEBUG(0,("schedule_aio_read_and_X: aio_read failed. "
107 "Error %s\n", strerror(errno) ));
108 TALLOC_FREE(aio_ex);
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"));
115 TALLOC_FREE(aio_ex);
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 ));
126 return NT_STATUS_OK;
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;
134 size_t outsize;
135 char *outbuf = (char *)aio_ex->outbuf.data;
136 ssize_t nread;
137 struct vfs_aio_state vfs_aio_state;
139 nread = SMB_VFS_PREAD_RECV(req, &vfs_aio_state);
140 TALLOC_FREE(req);
142 DEBUG(10, ("pread_recv returned %d, err = %s\n", (int)nread,
143 (nread == -1) ? strerror(vfs_aio_state.error) : "no error"));
145 if (fsp == NULL) {
146 DEBUG( 3, ("aio_pread_smb1_done: file closed whilst "
147 "aio outstanding (mid[%llu]).\n",
148 (unsigned long long)aio_ex->smbreq->mid));
149 TALLOC_FREE(aio_ex);
150 return;
153 if (nread < 0) {
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);
160 } else {
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 ) );
173 if (outsize <= 4) {
174 DBG_INFO("Invalid outsize (%zu)\n", outsize);
175 TALLOC_FREE(aio_ex);
176 return;
178 outsize -= 4;
179 _smb_setlen_large(outbuf, outsize);
181 show_msg(outbuf);
182 if (!smb1_srv_send(aio_ex->smbreq->xconn, outbuf,
183 true, aio_ex->smbreq->seqnum+1,
184 IS_CONN_ENCRYPTED(fsp->conn), NULL)) {
185 exit_server_cleanly("handle_aio_read_complete: smb1_srv_send "
186 "failed.");
189 DEBUG(10, ("handle_aio_read_complete: scheduled aio_read completed "
190 "for file %s, offset %.0f, len = %u\n",
191 fsp_str_dbg(fsp), (double)aio_ex->offset,
192 (unsigned int)nread));
194 TALLOC_FREE(aio_ex);
197 static void aio_pwrite_smb1_done(struct tevent_req *req);
199 /****************************************************************************
200 Set up an aio request from a SMBwriteX call.
201 *****************************************************************************/
203 NTSTATUS schedule_aio_write_and_X(connection_struct *conn,
204 struct smb_request *smbreq,
205 files_struct *fsp, const char *data,
206 off_t startpos,
207 size_t numtowrite)
209 struct aio_extra *aio_ex;
210 size_t bufsize;
211 size_t min_aio_write_size = lp_aio_write_size(SNUM(conn));
212 struct tevent_req *req;
214 if (fsp_is_alternate_stream(fsp)) {
215 DEBUG(10, ("AIO on streams not yet supported\n"));
216 return NT_STATUS_RETRY;
219 if ((!min_aio_write_size || (numtowrite < min_aio_write_size))
220 && !SMB_VFS_AIO_FORCE(fsp)) {
221 /* Too small a write for aio request. */
222 DEBUG(10,("schedule_aio_write_and_X: write size (%u) too "
223 "small for minimum aio_write of %u\n",
224 (unsigned int)numtowrite,
225 (unsigned int)min_aio_write_size ));
226 return NT_STATUS_RETRY;
229 /* Only do this on non-chained and non-chaining writes */
230 if (req_is_in_chain(smbreq)) {
231 return NT_STATUS_RETRY;
234 bufsize = smb_size + 6*2;
236 if (!(aio_ex = create_aio_extra(NULL, fsp, bufsize))) {
237 DEBUG(0,("schedule_aio_write_and_X: malloc fail.\n"));
238 return NT_STATUS_NO_MEMORY;
240 aio_ex->write_through = BITSETW(smbreq->vwv+7,0);
242 construct_smb1_reply_common_req(smbreq, (char *)aio_ex->outbuf.data);
243 srv_smb1_set_message((char *)aio_ex->outbuf.data, 6, 0, True);
244 SCVAL(aio_ex->outbuf.data,smb_vwv0,0xFF); /* Never a chained reply. */
246 init_strict_lock_struct(fsp,
247 (uint64_t)smbreq->smbpid,
248 (uint64_t)startpos,
249 (uint64_t)numtowrite,
250 WRITE_LOCK,
251 lp_posix_cifsu_locktype(fsp),
252 &aio_ex->lock);
254 /* Take the lock until the AIO completes. */
255 if (!SMB_VFS_STRICT_LOCK_CHECK(conn, fsp, &aio_ex->lock)) {
256 TALLOC_FREE(aio_ex);
257 return NT_STATUS_FILE_LOCK_CONFLICT;
260 aio_ex->nbyte = numtowrite;
261 aio_ex->offset = startpos;
263 req = pwrite_fsync_send(aio_ex, fsp->conn->sconn->ev_ctx, fsp,
264 data, numtowrite, startpos,
265 aio_ex->write_through);
266 if (req == NULL) {
267 DEBUG(3,("schedule_aio_wrote_and_X: aio_write failed. "
268 "Error %s\n", strerror(errno) ));
269 TALLOC_FREE(aio_ex);
270 return NT_STATUS_RETRY;
272 tevent_req_set_callback(req, aio_pwrite_smb1_done, aio_ex);
274 if (!aio_add_req_to_fsp(fsp, req)) {
275 DEBUG(1, ("Could not add req to fsp\n"));
276 TALLOC_FREE(aio_ex);
277 return NT_STATUS_RETRY;
280 aio_ex->smbreq = talloc_move(aio_ex, &smbreq);
282 /* This should actually be improved to span the write. */
283 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WRITE);
284 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WRITE);
286 if (!aio_ex->write_through && !lp_sync_always(SNUM(fsp->conn))
287 && fsp->fsp_flags.aio_write_behind)
289 /* Lie to the client and immediately claim we finished the
290 * write. */
291 SSVAL(aio_ex->outbuf.data,smb_vwv2,numtowrite);
292 SSVAL(aio_ex->outbuf.data,smb_vwv4,(numtowrite>>16)&1);
293 show_msg((char *)aio_ex->outbuf.data);
294 if (!smb1_srv_send(aio_ex->smbreq->xconn,
295 (char *)aio_ex->outbuf.data,
296 true, aio_ex->smbreq->seqnum+1,
297 IS_CONN_ENCRYPTED(fsp->conn),
298 &aio_ex->smbreq->pcd)) {
299 exit_server_cleanly("schedule_aio_write_and_X: "
300 "smb1_srv_send failed.");
302 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write "
303 "behind for file %s\n", fsp_str_dbg(fsp)));
306 DEBUG(10,("schedule_aio_write_and_X: scheduled aio_write for file "
307 "%s, offset %.0f, len = %u (mid = %u)\n",
308 fsp_str_dbg(fsp), (double)startpos, (unsigned int)numtowrite,
309 (unsigned int)aio_ex->smbreq->mid));
311 return NT_STATUS_OK;
314 static void aio_pwrite_smb1_done(struct tevent_req *req)
316 struct aio_extra *aio_ex = tevent_req_callback_data(
317 req, struct aio_extra);
318 files_struct *fsp = aio_ex->fsp;
319 char *outbuf = (char *)aio_ex->outbuf.data;
320 ssize_t numtowrite = aio_ex->nbyte;
321 ssize_t nwritten;
322 int err;
324 nwritten = pwrite_fsync_recv(req, &err);
325 TALLOC_FREE(req);
327 DEBUG(10, ("pwrite_recv returned %d, err = %s\n", (int)nwritten,
328 (nwritten == -1) ? strerror(err) : "no error"));
330 if (fsp == NULL) {
331 DEBUG( 3, ("aio_pwrite_smb1_done: file closed whilst "
332 "aio outstanding (mid[%llu]).\n",
333 (unsigned long long)aio_ex->smbreq->mid));
334 TALLOC_FREE(aio_ex);
335 return;
338 mark_file_modified(fsp);
340 if (fsp->fsp_flags.aio_write_behind) {
342 if (nwritten != numtowrite) {
343 if (nwritten == -1) {
344 DEBUG(5,("handle_aio_write_complete: "
345 "aio_write_behind failed ! File %s "
346 "is corrupt ! Error %s\n",
347 fsp_str_dbg(fsp), strerror(err)));
348 } else {
349 DEBUG(0,("handle_aio_write_complete: "
350 "aio_write_behind failed ! File %s "
351 "is corrupt ! Wanted %u bytes but "
352 "only wrote %d\n", fsp_str_dbg(fsp),
353 (unsigned int)numtowrite,
354 (int)nwritten ));
356 } else {
357 DEBUG(10,("handle_aio_write_complete: "
358 "aio_write_behind completed for file %s\n",
359 fsp_str_dbg(fsp)));
361 /* TODO: should no return success in case of an error !!! */
362 TALLOC_FREE(aio_ex);
363 return;
366 /* We don't need outsize or set_message here as we've already set the
367 fixed size length when we set up the aio call. */
369 if (nwritten == -1) {
370 DEBUG(3, ("handle_aio_write: file %s wanted %u bytes. "
371 "nwritten == %d. Error = %s\n",
372 fsp_str_dbg(fsp), (unsigned int)numtowrite,
373 (int)nwritten, strerror(err)));
375 ERROR_NT(map_nt_error_from_unix(err));
376 srv_smb1_set_message(outbuf,0,0,true);
377 } else {
378 SSVAL(outbuf,smb_vwv2,nwritten);
379 SSVAL(outbuf,smb_vwv4,(nwritten>>16)&1);
380 if (nwritten < (ssize_t)numtowrite) {
381 SCVAL(outbuf,smb_rcls,ERRHRD);
382 SSVAL(outbuf,smb_err,ERRdiskfull);
385 DEBUG(3,("handle_aio_write: %s, num=%d wrote=%d\n",
386 fsp_fnum_dbg(fsp), (int)numtowrite, (int)nwritten));
388 fh_set_pos(aio_ex->fsp->fh, aio_ex->offset + nwritten);
391 show_msg(outbuf);
392 if (!smb1_srv_send(aio_ex->smbreq->xconn, outbuf,
393 true, aio_ex->smbreq->seqnum+1,
394 IS_CONN_ENCRYPTED(fsp->conn),
395 NULL)) {
396 exit_server_cleanly("handle_aio_write_complete: "
397 "smb1_srv_send failed.");
400 DEBUG(10, ("handle_aio_write_complete: scheduled aio_write completed "
401 "for file %s, offset %.0f, requested %u, written = %u\n",
402 fsp_str_dbg(fsp), (double)aio_ex->offset,
403 (unsigned int)numtowrite, (unsigned int)nwritten));
405 TALLOC_FREE(aio_ex);