s3:smbd: change blocking.c to use fsp_fnum_dbg() for fsp->fnum logging.
[Samba/gebeck_regimport.git] / source3 / smbd / smb2_break.c
blob9318068699eedf5c37a91200825a77909eb6d6bb
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 int i = req->current_idx;
42 uint8_t in_oplock_level;
43 uint64_t in_file_id_persistent;
44 uint64_t in_file_id_volatile;
45 struct files_struct *in_fsp;
46 struct tevent_req *subreq;
48 status = smbd_smb2_request_verify_sizes(req, 0x18);
49 if (!NT_STATUS_IS_OK(status)) {
50 return smbd_smb2_request_error(req, status);
52 inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
54 in_oplock_level = CVAL(inbody, 0x02);
56 if (in_oplock_level != SMB2_OPLOCK_LEVEL_NONE &&
57 in_oplock_level != SMB2_OPLOCK_LEVEL_II) {
58 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
61 /* 0x03 1 bytes reserved */
62 /* 0x04 4 bytes reserved */
63 in_file_id_persistent = BVAL(inbody, 0x08);
64 in_file_id_volatile = BVAL(inbody, 0x10);
66 in_fsp = file_fsp_smb2(req, in_file_id_persistent, in_file_id_volatile);
67 if (in_fsp == NULL) {
68 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
71 subreq = smbd_smb2_oplock_break_send(req, req->sconn->ev_ctx,
72 req, in_fsp, in_oplock_level);
73 if (subreq == NULL) {
74 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
76 tevent_req_set_callback(subreq, smbd_smb2_request_oplock_break_done, req);
78 return smbd_smb2_request_pending_queue(req, subreq, 500);
81 static void smbd_smb2_request_oplock_break_done(struct tevent_req *subreq)
83 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
84 struct smbd_smb2_request);
85 const uint8_t *inbody;
86 int i = req->current_idx;
87 uint64_t in_file_id_persistent;
88 uint64_t in_file_id_volatile;
89 uint8_t out_oplock_level = 0;
90 DATA_BLOB outbody;
91 NTSTATUS status;
92 NTSTATUS error; /* transport error */
94 status = smbd_smb2_oplock_break_recv(subreq, &out_oplock_level);
95 TALLOC_FREE(subreq);
96 if (!NT_STATUS_IS_OK(status)) {
97 error = smbd_smb2_request_error(req, status);
98 if (!NT_STATUS_IS_OK(error)) {
99 smbd_server_connection_terminate(req->sconn,
100 nt_errstr(error));
101 return;
103 return;
106 inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
108 in_file_id_persistent = BVAL(inbody, 0x08);
109 in_file_id_volatile = BVAL(inbody, 0x10);
111 outbody = data_blob_talloc(req->out.vector, NULL, 0x18);
112 if (outbody.data == NULL) {
113 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
114 if (!NT_STATUS_IS_OK(error)) {
115 smbd_server_connection_terminate(req->sconn,
116 nt_errstr(error));
117 return;
119 return;
122 SSVAL(outbody.data, 0x00, 0x18); /* struct size */
123 SCVAL(outbody.data, 0x02,
124 out_oplock_level); /* SMB2 oplock level */
125 SCVAL(outbody.data, 0x03, 0); /* reserved */
126 SIVAL(outbody.data, 0x04, 0); /* reserved */
127 SBVAL(outbody.data, 0x08,
128 in_file_id_persistent); /* file id (persistent) */
129 SBVAL(outbody.data, 0x10,
130 in_file_id_volatile); /* file id (volatile) */
132 error = smbd_smb2_request_done(req, outbody, NULL);
133 if (!NT_STATUS_IS_OK(error)) {
134 smbd_server_connection_terminate(req->sconn,
135 nt_errstr(error));
136 return;
140 struct smbd_smb2_oplock_break_state {
141 struct smbd_smb2_request *smb2req;
142 uint8_t out_oplock_level; /* SMB2 oplock level. */
145 static struct tevent_req *smbd_smb2_oplock_break_send(TALLOC_CTX *mem_ctx,
146 struct tevent_context *ev,
147 struct smbd_smb2_request *smb2req,
148 struct files_struct *fsp,
149 uint8_t in_oplock_level)
151 struct tevent_req *req;
152 struct smbd_smb2_oplock_break_state *state;
153 struct smb_request *smbreq;
154 int oplocklevel = map_smb2_oplock_levels_to_samba(in_oplock_level);
155 bool break_to_none = (oplocklevel == NO_OPLOCK);
156 bool result;
158 req = tevent_req_create(mem_ctx, &state,
159 struct smbd_smb2_oplock_break_state);
160 if (req == NULL) {
161 return NULL;
163 state->smb2req = smb2req;
164 state->out_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
166 DEBUG(10,("smbd_smb2_oplock_break_send: %s - fnum[%d] "
167 "samba level %d\n",
168 fsp_str_dbg(fsp), fsp->fnum,
169 oplocklevel));
171 smbreq = smbd_smb2_fake_smb_request(smb2req);
172 if (tevent_req_nomem(smbreq, req)) {
173 return tevent_req_post(req, ev);
176 DEBUG(5,("smbd_smb2_oplock_break_send: got SMB2 oplock break (%u) from client "
177 "for file %s fnum = %d\n",
178 (unsigned int)in_oplock_level,
179 fsp_str_dbg(fsp),
180 fsp->fnum ));
182 /* Are we awaiting a break message ? */
183 if (fsp->oplock_timeout == NULL) {
184 tevent_req_nterror(req, NT_STATUS_INVALID_OPLOCK_PROTOCOL);
185 return tevent_req_post(req, ev);
188 if ((fsp->sent_oplock_break == BREAK_TO_NONE_SENT) ||
189 (break_to_none)) {
190 result = remove_oplock(fsp);
191 state->out_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
192 } else {
193 result = downgrade_oplock(fsp);
194 state->out_oplock_level = SMB2_OPLOCK_LEVEL_II;
197 if (!result) {
198 DEBUG(0, ("smbd_smb2_oplock_break_send: error in removing "
199 "oplock on file %s\n", fsp_str_dbg(fsp)));
200 /* Hmmm. Is this panic justified? */
201 smb_panic("internal tdb error");
204 reply_to_oplock_break_requests(fsp);
206 tevent_req_done(req);
207 return tevent_req_post(req, ev);
210 static NTSTATUS smbd_smb2_oplock_break_recv(struct tevent_req *req,
211 uint8_t *out_oplock_level)
213 NTSTATUS status;
214 struct smbd_smb2_oplock_break_state *state =
215 tevent_req_data(req,
216 struct smbd_smb2_oplock_break_state);
218 if (tevent_req_is_nterror(req, &status)) {
219 tevent_req_received(req);
220 return status;
223 *out_oplock_level = state->out_oplock_level;
225 tevent_req_received(req);
226 return NT_STATUS_OK;
229 /*********************************************************
230 Create and send an asynchronous
231 SMB2 OPLOCK_BREAK_NOTIFICATION.
232 *********************************************************/
234 void send_break_message_smb2(files_struct *fsp, int level)
236 uint8_t smb2_oplock_level = (level == OPLOCKLEVEL_II) ?
237 SMB2_OPLOCK_LEVEL_II :
238 SMB2_OPLOCK_LEVEL_NONE;
239 NTSTATUS status;
240 uint64_t fsp_persistent = fsp_persistent_id(fsp);
242 DEBUG(10,("send_break_message_smb2: sending oplock break "
243 "for file %s, fnum = %d, smb2 level %u\n",
244 fsp_str_dbg(fsp),
245 fsp->fnum,
246 (unsigned int)smb2_oplock_level ));
248 status = smbd_smb2_send_oplock_break(fsp->conn->sconn,
249 fsp_persistent,
250 (uint64_t)fsp->fnum,
251 smb2_oplock_level);
252 if (!NT_STATUS_IS_OK(status)) {
253 smbd_server_connection_terminate(fsp->conn->sconn,
254 nt_errstr(status));