aed489f6f382b6c686551e1ebb1bca30d8353bbf
[kai/samba.git] / source3 / libsmb / clioplock.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SMB client oplock functions
4    Copyright (C) Andrew Tridgell 2001
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "../lib/util/tevent_ntstatus.h"
22 #include "async_smb.h"
23
24 /****************************************************************************
25 send an ack for an oplock break request
26 ****************************************************************************/
27
28 struct cli_oplock_ack_state {
29         uint16_t vwv[8];
30 };
31
32 static void cli_oplock_ack_done(struct tevent_req *subreq);
33
34 struct tevent_req *cli_oplock_ack_send(TALLOC_CTX *mem_ctx,
35                                        struct tevent_context *ev,
36                                        struct cli_state *cli,
37                                        uint16_t fnum, uint8_t level)
38 {
39         struct tevent_req *req, *subreq;
40         struct cli_oplock_ack_state *state;
41
42         req = tevent_req_create(mem_ctx, &state, struct cli_oplock_ack_state);
43         if (req == NULL) {
44                 return NULL;
45         }
46         SCVAL(state->vwv+0, 0, 0xff);
47         SCVAL(state->vwv+0, 1, 0);
48         SSVAL(state->vwv+1, 0, 0);
49         SSVAL(state->vwv+2, 0, fnum);
50         SCVAL(state->vwv+3, 0, LOCKING_ANDX_OPLOCK_RELEASE);
51         SCVAL(state->vwv+3, 1, level);
52         SIVAL(state->vwv+4, 0, 0); /* timeout */
53         SSVAL(state->vwv+6, 0, 0); /* unlockcount */
54         SSVAL(state->vwv+7, 0, 0); /* lockcount */
55
56         subreq = cli_smb_send(state, ev, cli, SMBlockingX, 0, 8, state->vwv,
57                               0, NULL);
58         if (tevent_req_nomem(subreq, req)) {
59                 return tevent_req_post(req, ev);
60         }
61         tevent_req_set_callback(subreq, cli_oplock_ack_done, req);
62         return req;
63 }
64
65 static void cli_oplock_ack_done(struct tevent_req *subreq)
66 {
67         struct tevent_req *req = tevent_req_callback_data(
68                 subreq, struct tevent_req);
69         NTSTATUS status;
70
71         status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
72         TALLOC_FREE(subreq);
73         if (!NT_STATUS_IS_OK(status)) {
74                 tevent_req_nterror(req, status);
75                 return;
76         }
77         tevent_req_done(req);
78 }
79
80 NTSTATUS cli_oplock_ack_recv(struct tevent_req *req)
81 {
82         return tevent_req_simple_recv_ntstatus(req);
83 }
84
85 NTSTATUS cli_oplock_ack(struct cli_state *cli, uint16_t fnum, unsigned char level)
86 {
87         TALLOC_CTX *frame = talloc_stackframe();
88         struct event_context *ev;
89         struct tevent_req *req;
90         NTSTATUS status = NT_STATUS_OK;
91
92         if (cli_has_async_calls(cli)) {
93                 /*
94                  * Can't use sync call while an async call is in flight
95                  */
96                 status = NT_STATUS_INVALID_PARAMETER;
97                 goto fail;
98         }
99
100         ev = event_context_init(frame);
101         if (ev == NULL) {
102                 status = NT_STATUS_NO_MEMORY;
103                 goto fail;
104         }
105
106         req = cli_oplock_ack_send(frame, ev, cli, fnum, level);
107         if (req == NULL) {
108                 status = NT_STATUS_NO_MEMORY;
109                 goto fail;
110         }
111
112         if (!tevent_req_poll(req, ev)) {
113                 status = map_nt_error_from_unix(errno);
114                 goto fail;
115         }
116
117         status = cli_oplock_ack_recv(req);
118  fail:
119         TALLOC_FREE(frame);
120         if (!NT_STATUS_IS_OK(status)) {
121                 cli_set_error(cli, status);
122         }
123         return status;
124 }
125
126 /****************************************************************************
127 set the oplock handler for a connection
128 ****************************************************************************/
129
130 void cli_oplock_handler(struct cli_state *cli, 
131                         NTSTATUS (*handler)(struct cli_state *, uint16_t, unsigned char))
132 {
133         cli->oplock_handler = handler;
134 }