traffic_packets: provision request data for packet_drsuapi_13
[Samba.git] / source3 / smbd / smb2_flush.c
blobf7d9e964319a816925155dcc50d45ffbe9871ebf
1 /*
2 Unix SMB/CIFS implementation.
3 Core SMB2 server
5 Copyright (C) Stefan Metzmacher 2009
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 "../libcli/smb/smb_common.h"
25 #include "../lib/util/tevent_ntstatus.h"
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_SMB2
30 static struct tevent_req *smbd_smb2_flush_send(TALLOC_CTX *mem_ctx,
31 struct tevent_context *ev,
32 struct smbd_smb2_request *smb2req,
33 struct files_struct *fsp);
34 static NTSTATUS smbd_smb2_flush_recv(struct tevent_req *req);
36 static void smbd_smb2_request_flush_done(struct tevent_req *subreq);
37 NTSTATUS smbd_smb2_request_process_flush(struct smbd_smb2_request *req)
39 NTSTATUS status;
40 const uint8_t *inbody;
41 uint64_t in_file_id_persistent;
42 uint64_t in_file_id_volatile;
43 struct files_struct *in_fsp;
44 struct tevent_req *subreq;
46 status = smbd_smb2_request_verify_sizes(req, 0x18);
47 if (!NT_STATUS_IS_OK(status)) {
48 return smbd_smb2_request_error(req, status);
50 inbody = SMBD_SMB2_IN_BODY_PTR(req);
52 in_file_id_persistent = BVAL(inbody, 0x08);
53 in_file_id_volatile = BVAL(inbody, 0x10);
55 in_fsp = file_fsp_smb2(req, in_file_id_persistent, in_file_id_volatile);
56 if (in_fsp == NULL) {
57 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
60 subreq = smbd_smb2_flush_send(req, req->sconn->ev_ctx,
61 req, in_fsp);
62 if (subreq == NULL) {
63 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
65 tevent_req_set_callback(subreq, smbd_smb2_request_flush_done, req);
67 return smbd_smb2_request_pending_queue(req, subreq, 500);
70 static void smbd_smb2_request_flush_done(struct tevent_req *subreq)
72 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
73 struct smbd_smb2_request);
74 DATA_BLOB outbody;
75 NTSTATUS status;
76 NTSTATUS error; /* transport error */
78 status = smbd_smb2_flush_recv(subreq);
79 TALLOC_FREE(subreq);
80 if (!NT_STATUS_IS_OK(status)) {
81 error = smbd_smb2_request_error(req, status);
82 if (!NT_STATUS_IS_OK(error)) {
83 smbd_server_connection_terminate(req->xconn,
84 nt_errstr(error));
85 return;
87 return;
90 outbody = smbd_smb2_generate_outbody(req, 0x04);
91 if (outbody.data == NULL) {
92 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
93 if (!NT_STATUS_IS_OK(error)) {
94 smbd_server_connection_terminate(req->xconn,
95 nt_errstr(error));
96 return;
98 return;
101 SSVAL(outbody.data, 0x00, 0x04); /* struct size */
102 SSVAL(outbody.data, 0x02, 0); /* reserved */
104 error = smbd_smb2_request_done(req, outbody, NULL);
105 if (!NT_STATUS_IS_OK(error)) {
106 smbd_server_connection_terminate(req->xconn,
107 nt_errstr(error));
108 return;
112 struct smbd_smb2_flush_state {
113 struct smbd_smb2_request *smb2req;
116 static void smbd_smb2_flush_done(struct tevent_req *subreq);
118 static struct tevent_req *smbd_smb2_flush_send(TALLOC_CTX *mem_ctx,
119 struct tevent_context *ev,
120 struct smbd_smb2_request *smb2req,
121 struct files_struct *fsp)
123 struct tevent_req *req;
124 struct tevent_req *subreq;
125 struct smbd_smb2_flush_state *state;
126 struct smb_request *smbreq;
127 int ret;
129 req = tevent_req_create(mem_ctx, &state,
130 struct smbd_smb2_flush_state);
131 if (req == NULL) {
132 return NULL;
134 state->smb2req = smb2req;
136 DEBUG(10,("smbd_smb2_flush: %s - %s\n",
137 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp)));
139 smbreq = smbd_smb2_fake_smb_request(smb2req);
140 if (tevent_req_nomem(smbreq, req)) {
141 return tevent_req_post(req, ev);
144 if (IS_IPC(smbreq->conn)) {
145 tevent_req_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
146 return tevent_req_post(req, ev);
149 if (!CHECK_WRITE(fsp)) {
150 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
151 return tevent_req_post(req, ev);
154 if (fsp->fh->fd == -1) {
155 tevent_req_nterror(req, NT_STATUS_INVALID_HANDLE);
156 return tevent_req_post(req, ev);
159 if (!lp_strict_sync(SNUM(smbreq->conn))) {
161 * No strict sync. Don't really do
162 * anything here.
164 tevent_req_done(req);
165 return tevent_req_post(req, ev);
168 ret = flush_write_cache(fsp, SAMBA_SYNC_FLUSH);
169 if (ret == -1) {
170 tevent_req_nterror(req, map_nt_error_from_unix(errno));
171 return tevent_req_post(req, ev);
174 subreq = SMB_VFS_FSYNC_SEND(state, ev, fsp);
175 if (tevent_req_nomem(subreq, req)) {
176 return tevent_req_post(req, ev);
179 tevent_req_set_callback(subreq, smbd_smb2_flush_done, req);
181 /* Ensure any close request knows about this outstanding IO. */
182 if (!aio_add_req_to_fsp(fsp, req)) {
183 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
184 return tevent_req_post(req, ev);
187 return req;
191 static void smbd_smb2_flush_done(struct tevent_req *subreq)
193 struct tevent_req *req = tevent_req_callback_data(
194 subreq, struct tevent_req);
195 int ret;
196 struct vfs_aio_state vfs_aio_state;
198 ret = SMB_VFS_FSYNC_RECV(subreq, &vfs_aio_state);
199 TALLOC_FREE(subreq);
200 if (ret == -1) {
201 tevent_req_nterror(req, map_nt_error_from_unix(vfs_aio_state.error));
202 return;
204 tevent_req_done(req);
207 static NTSTATUS smbd_smb2_flush_recv(struct tevent_req *req)
209 NTSTATUS status;
211 if (tevent_req_is_nterror(req, &status)) {
212 tevent_req_received(req);
213 return status;
216 tevent_req_received(req);
217 return NT_STATUS_OK;