ctdb-vacuum: rename ctdb_vacuum_db_full --> ctdb_vacuum_traverse_db
[Samba.git] / source3 / smbd / smb2_break.c
blob6446b79ff890de0d1dbe07b203c839aae205baf0
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 = smbd_smb2_generate_outbody(req, 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 tevent_req_done(req);
203 return tevent_req_post(req, ev);
206 static NTSTATUS smbd_smb2_oplock_break_recv(struct tevent_req *req,
207 uint8_t *out_oplock_level)
209 NTSTATUS status;
210 struct smbd_smb2_oplock_break_state *state =
211 tevent_req_data(req,
212 struct smbd_smb2_oplock_break_state);
214 if (tevent_req_is_nterror(req, &status)) {
215 tevent_req_received(req);
216 return status;
219 *out_oplock_level = state->out_oplock_level;
221 tevent_req_received(req);
222 return NT_STATUS_OK;
225 /*********************************************************
226 Create and send an asynchronous
227 SMB2 OPLOCK_BREAK_NOTIFICATION.
228 *********************************************************/
230 void send_break_message_smb2(files_struct *fsp, int level)
232 uint8_t smb2_oplock_level = (level == OPLOCKLEVEL_II) ?
233 SMB2_OPLOCK_LEVEL_II :
234 SMB2_OPLOCK_LEVEL_NONE;
235 NTSTATUS status;
236 struct smbXsrv_session *session = NULL;
237 struct timeval tv = timeval_current();
238 NTTIME now = timeval_to_nttime(&tv);
240 status = smb2srv_session_lookup(fsp->conn->sconn->conn,
241 fsp->vuid,
242 now,
243 &session);
244 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED) ||
245 (session == NULL))
248 DEBUG(10,("send_break_message_smb2: skip oplock break "
249 "for file %s, %s, smb2 level %u session %llu not found\n",
250 fsp_str_dbg(fsp),
251 fsp_fnum_dbg(fsp),
252 (unsigned int)smb2_oplock_level,
253 (unsigned long long)fsp->vuid));
254 return;
257 DEBUG(10,("send_break_message_smb2: sending oplock break "
258 "for file %s, %s, smb2 level %u\n",
259 fsp_str_dbg(fsp),
260 fsp_fnum_dbg(fsp),
261 (unsigned int)smb2_oplock_level ));
263 status = smbd_smb2_send_oplock_break(fsp->conn->sconn,
264 session,
265 fsp->conn->tcon,
266 fsp->op,
267 smb2_oplock_level);
268 if (!NT_STATUS_IS_OK(status)) {
269 smbd_server_connection_terminate(fsp->conn->sconn,
270 nt_errstr(status));