wafsamba: improve wording in a comment
[Samba.git] / source3 / smbd / smb2_break.c
blob8db9e6ddeceac4ca4e9d46afcb38377903db3eb1
1 /*
2 Unix SMB/CIFS implementation.
3 Core SMB2 server
5 Copyright (C) Stefan Metzmacher 2009
6 Copyright (C) Jeremy Allison 2010
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "smbd/smbd.h"
24 #include "smbd/globals.h"
25 #include "../libcli/smb/smb_common.h"
26 #include "../lib/util/tevent_ntstatus.h"
28 static struct tevent_req *smbd_smb2_oplock_break_send(TALLOC_CTX *mem_ctx,
29 struct tevent_context *ev,
30 struct smbd_smb2_request *smb2req,
31 struct files_struct *in_fsp,
32 uint8_t in_oplock_level);
33 static NTSTATUS smbd_smb2_oplock_break_recv(struct tevent_req *req,
34 uint8_t *out_oplock_level);
36 static void smbd_smb2_request_oplock_break_done(struct tevent_req *subreq);
37 NTSTATUS smbd_smb2_request_process_break(struct smbd_smb2_request *req)
39 NTSTATUS status;
40 const uint8_t *inbody;
41 uint8_t in_oplock_level;
42 uint64_t in_file_id_persistent;
43 uint64_t in_file_id_volatile;
44 struct files_struct *in_fsp;
45 struct tevent_req *subreq;
47 status = smbd_smb2_request_verify_sizes(req, 0x18);
48 if (!NT_STATUS_IS_OK(status)) {
49 return smbd_smb2_request_error(req, status);
51 inbody = SMBD_SMB2_IN_BODY_PTR(req);
53 in_oplock_level = CVAL(inbody, 0x02);
55 if (in_oplock_level != SMB2_OPLOCK_LEVEL_NONE &&
56 in_oplock_level != SMB2_OPLOCK_LEVEL_II) {
57 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
60 /* 0x03 1 bytes reserved */
61 /* 0x04 4 bytes reserved */
62 in_file_id_persistent = BVAL(inbody, 0x08);
63 in_file_id_volatile = BVAL(inbody, 0x10);
65 in_fsp = file_fsp_smb2(req, in_file_id_persistent, in_file_id_volatile);
66 if (in_fsp == NULL) {
67 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
70 subreq = smbd_smb2_oplock_break_send(req, req->sconn->ev_ctx,
71 req, in_fsp, in_oplock_level);
72 if (subreq == NULL) {
73 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
75 tevent_req_set_callback(subreq, smbd_smb2_request_oplock_break_done, req);
77 return smbd_smb2_request_pending_queue(req, subreq, 500);
80 static void smbd_smb2_request_oplock_break_done(struct tevent_req *subreq)
82 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
83 struct smbd_smb2_request);
84 const uint8_t *inbody;
85 uint64_t in_file_id_persistent;
86 uint64_t in_file_id_volatile;
87 uint8_t out_oplock_level = 0;
88 DATA_BLOB outbody;
89 NTSTATUS status;
90 NTSTATUS error; /* transport error */
92 status = smbd_smb2_oplock_break_recv(subreq, &out_oplock_level);
93 TALLOC_FREE(subreq);
94 if (!NT_STATUS_IS_OK(status)) {
95 error = smbd_smb2_request_error(req, status);
96 if (!NT_STATUS_IS_OK(error)) {
97 smbd_server_connection_terminate(req->sconn,
98 nt_errstr(error));
99 return;
101 return;
104 inbody = SMBD_SMB2_IN_BODY_PTR(req);
106 in_file_id_persistent = BVAL(inbody, 0x08);
107 in_file_id_volatile = BVAL(inbody, 0x10);
109 outbody = data_blob_talloc(req->out.vector, NULL, 0x18);
110 if (outbody.data == NULL) {
111 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
112 if (!NT_STATUS_IS_OK(error)) {
113 smbd_server_connection_terminate(req->sconn,
114 nt_errstr(error));
115 return;
117 return;
120 SSVAL(outbody.data, 0x00, 0x18); /* struct size */
121 SCVAL(outbody.data, 0x02,
122 out_oplock_level); /* SMB2 oplock level */
123 SCVAL(outbody.data, 0x03, 0); /* reserved */
124 SIVAL(outbody.data, 0x04, 0); /* reserved */
125 SBVAL(outbody.data, 0x08,
126 in_file_id_persistent); /* file id (persistent) */
127 SBVAL(outbody.data, 0x10,
128 in_file_id_volatile); /* file id (volatile) */
130 error = smbd_smb2_request_done(req, outbody, NULL);
131 if (!NT_STATUS_IS_OK(error)) {
132 smbd_server_connection_terminate(req->sconn,
133 nt_errstr(error));
134 return;
138 struct smbd_smb2_oplock_break_state {
139 struct smbd_smb2_request *smb2req;
140 uint8_t out_oplock_level; /* SMB2 oplock level. */
143 static struct tevent_req *smbd_smb2_oplock_break_send(TALLOC_CTX *mem_ctx,
144 struct tevent_context *ev,
145 struct smbd_smb2_request *smb2req,
146 struct files_struct *fsp,
147 uint8_t in_oplock_level)
149 struct tevent_req *req;
150 struct smbd_smb2_oplock_break_state *state;
151 struct smb_request *smbreq;
152 int oplocklevel = map_smb2_oplock_levels_to_samba(in_oplock_level);
153 bool break_to_none = (oplocklevel == NO_OPLOCK);
154 bool result;
156 req = tevent_req_create(mem_ctx, &state,
157 struct smbd_smb2_oplock_break_state);
158 if (req == NULL) {
159 return NULL;
161 state->smb2req = smb2req;
162 state->out_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
164 DEBUG(10,("smbd_smb2_oplock_break_send: %s - %s, "
165 "samba level %d\n",
166 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
167 oplocklevel));
169 smbreq = smbd_smb2_fake_smb_request(smb2req);
170 if (tevent_req_nomem(smbreq, req)) {
171 return tevent_req_post(req, ev);
174 DEBUG(5,("smbd_smb2_oplock_break_send: got SMB2 oplock break (%u) from client "
175 "for file %s, %s\n",
176 (unsigned int)in_oplock_level,
177 fsp_str_dbg(fsp),
178 fsp_fnum_dbg(fsp)));
180 /* Are we awaiting a break message ? */
181 if (fsp->oplock_timeout == NULL) {
182 tevent_req_nterror(req, NT_STATUS_INVALID_OPLOCK_PROTOCOL);
183 return tevent_req_post(req, ev);
186 if ((fsp->sent_oplock_break == BREAK_TO_NONE_SENT) ||
187 (break_to_none)) {
188 result = remove_oplock(fsp);
189 state->out_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
190 } else {
191 result = downgrade_oplock(fsp);
192 state->out_oplock_level = SMB2_OPLOCK_LEVEL_II;
195 if (!result) {
196 DEBUG(0, ("smbd_smb2_oplock_break_send: error in removing "
197 "oplock on file %s\n", fsp_str_dbg(fsp)));
198 /* Hmmm. Is this panic justified? */
199 smb_panic("internal tdb error");
202 reply_to_oplock_break_requests(fsp);
204 tevent_req_done(req);
205 return tevent_req_post(req, ev);
208 static NTSTATUS smbd_smb2_oplock_break_recv(struct tevent_req *req,
209 uint8_t *out_oplock_level)
211 NTSTATUS status;
212 struct smbd_smb2_oplock_break_state *state =
213 tevent_req_data(req,
214 struct smbd_smb2_oplock_break_state);
216 if (tevent_req_is_nterror(req, &status)) {
217 tevent_req_received(req);
218 return status;
221 *out_oplock_level = state->out_oplock_level;
223 tevent_req_received(req);
224 return NT_STATUS_OK;
227 /*********************************************************
228 Create and send an asynchronous
229 SMB2 OPLOCK_BREAK_NOTIFICATION.
230 *********************************************************/
232 void send_break_message_smb2(files_struct *fsp, int level)
234 uint8_t smb2_oplock_level = (level == OPLOCKLEVEL_II) ?
235 SMB2_OPLOCK_LEVEL_II :
236 SMB2_OPLOCK_LEVEL_NONE;
237 NTSTATUS status;
238 struct smbXsrv_session *session = NULL;
239 struct timeval tv = timeval_current();
240 NTTIME now = timeval_to_nttime(&tv);
242 status = smb2srv_session_lookup(fsp->conn->sconn->conn,
243 fsp->vuid,
244 now,
245 &session);
246 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED) ||
247 (session == NULL))
250 DEBUG(10,("send_break_message_smb2: skip oplock break "
251 "for file %s, %s, smb2 level %u session %llu not found\n",
252 fsp_str_dbg(fsp),
253 fsp_fnum_dbg(fsp),
254 (unsigned int)smb2_oplock_level,
255 (unsigned long long)fsp->vuid));
256 return;
259 DEBUG(10,("send_break_message_smb2: sending oplock break "
260 "for file %s, %s, smb2 level %u\n",
261 fsp_str_dbg(fsp),
262 fsp_fnum_dbg(fsp),
263 (unsigned int)smb2_oplock_level ));
265 status = smbd_smb2_send_oplock_break(fsp->conn->sconn,
266 session,
267 fsp->conn->tcon,
268 fsp->op,
269 smb2_oplock_level);
270 if (!NT_STATUS_IS_OK(status)) {
271 smbd_server_connection_terminate(fsp->conn->sconn,
272 nt_errstr(status));