Only MULTIPLE-UNLOCK test left to fix !
[nivanova/samba-autobuild/.git] / source3 / smbd / smb2_lock.c
1 /*
2    Unix SMB/CIFS implementation.
3    Core SMB2 server
4
5    Copyright (C) Stefan Metzmacher 2009
6    Copyright (C) Jeremy Allison 2010
7
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.
12
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.
17
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/>.
20 */
21
22 #include "includes.h"
23 #include "smbd/globals.h"
24 #include "../libcli/smb/smb_common.h"
25 #include "librpc/gen_ndr/messaging.h"
26
27 struct smbd_smb2_lock_element {
28         uint64_t offset;
29         uint64_t length;
30         uint32_t flags;
31 };
32
33 struct smbd_smb2_lock_state {
34         struct smbd_smb2_request *smb2req;
35         struct smb_request *smb1req;
36         struct blocking_lock_record *blr;
37         uint16_t lock_count;
38         struct smbd_lock_element *locks;
39 };
40
41 static void remove_pending_lock(TALLOC_CTX *mem_ctx, struct blocking_lock_record *blr);
42
43 static struct tevent_req *smbd_smb2_lock_send(TALLOC_CTX *mem_ctx,
44                                                  struct tevent_context *ev,
45                                                  struct smbd_smb2_request *smb2req,
46                                                  uint32_t in_smbpid,
47                                                  uint64_t in_file_id_volatile,
48                                                  uint16_t in_lock_count,
49                                                  struct smbd_smb2_lock_element *in_locks);
50 static NTSTATUS smbd_smb2_lock_recv(struct tevent_req *req);
51
52 static void smbd_smb2_request_lock_done(struct tevent_req *subreq);
53 NTSTATUS smbd_smb2_request_process_lock(struct smbd_smb2_request *req)
54 {
55         const uint8_t *inhdr;
56         const uint8_t *inbody;
57         const int i = req->current_idx;
58         size_t expected_body_size = 0x30;
59         size_t body_size;
60         uint32_t in_smbpid;
61         uint16_t in_lock_count;
62         uint64_t in_file_id_persistent;
63         uint64_t in_file_id_volatile;
64         struct smbd_smb2_lock_element *in_locks;
65         struct tevent_req *subreq;
66         const uint8_t *lock_buffer;
67         uint16_t l;
68
69         inhdr = (const uint8_t *)req->in.vector[i+0].iov_base;
70         if (req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
71                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
72         }
73
74         inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
75
76         body_size = SVAL(inbody, 0x00);
77         if (body_size != expected_body_size) {
78                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
79         }
80
81         in_smbpid                       = IVAL(inhdr, SMB2_HDR_PID);
82
83         in_lock_count                   = CVAL(inbody, 0x02);
84         /* 0x04 - 4 bytes reserved */
85         in_file_id_persistent           = BVAL(inbody, 0x08);
86         in_file_id_volatile             = BVAL(inbody, 0x10);
87
88         if (in_lock_count < 1) {
89                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
90         }
91
92         if (((in_lock_count - 1) * 0x18) > req->in.vector[i+2].iov_len) {
93                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
94         }
95
96         if (req->compat_chain_fsp) {
97                 /* skip check */
98         } else if (in_file_id_persistent != 0) {
99                 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
100         }
101
102         in_locks = talloc_array(req, struct smbd_smb2_lock_element,
103                                 in_lock_count);
104         if (in_locks == NULL) {
105                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
106         }
107
108         l = 0;
109         lock_buffer = inbody + 0x18;
110
111         in_locks[l].offset      = BVAL(lock_buffer, 0x00);
112         in_locks[l].length      = BVAL(lock_buffer, 0x08);
113         in_locks[l].flags       = IVAL(lock_buffer, 0x10);
114         /* 0x14 - 4 reserved bytes */
115
116         lock_buffer = (const uint8_t *)req->in.vector[i+2].iov_base;
117
118         for (l=1; l < in_lock_count; l++) {
119                 in_locks[l].offset      = BVAL(lock_buffer, 0x00);
120                 in_locks[l].length      = BVAL(lock_buffer, 0x08);
121                 in_locks[l].flags       = IVAL(lock_buffer, 0x10);
122                 /* 0x14 - 4 reserved bytes */
123
124                 lock_buffer += 0x18;
125         }
126
127         subreq = smbd_smb2_lock_send(req,
128                                      req->sconn->smb2.event_ctx,
129                                      req,
130                                      in_smbpid,
131                                      in_file_id_volatile,
132                                      in_lock_count,
133                                      in_locks);
134         if (subreq == NULL) {
135                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
136         }
137         tevent_req_set_callback(subreq, smbd_smb2_request_lock_done, req);
138
139         return smbd_smb2_request_pending_queue(req, subreq);
140 }
141
142 static void smbd_smb2_request_lock_done(struct tevent_req *subreq)
143 {
144         struct smbd_smb2_request *smb2req = tevent_req_callback_data(subreq,
145                                         struct smbd_smb2_request);
146         DATA_BLOB outbody;
147         NTSTATUS status;
148         NTSTATUS error; /* transport error */
149
150         if (smb2req->cancelled) {
151                 const uint8_t *inhdr = (const uint8_t *)
152                         smb2req->in.vector[smb2req->current_idx].iov_base;
153                 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
154                 struct smbd_smb2_lock_state *state;
155
156                 DEBUG(10,("smbd_smb2_request_lock_done: cancelled mid %llu\n",
157                         (unsigned long long)mid ));
158
159                 state = tevent_req_data(smb2req->subreq,
160                                 struct smbd_smb2_lock_state);
161
162                 SMB_ASSERT(state);
163                 SMB_ASSERT(state->blr);
164
165                 remove_pending_lock(state, state->blr);
166
167                 error = smbd_smb2_request_error(smb2req, NT_STATUS_CANCELLED);
168                 if (!NT_STATUS_IS_OK(error)) {
169                         smbd_server_connection_terminate(smb2req->sconn,
170                                 nt_errstr(error));
171                         return;
172                 }
173                 return;
174         }
175
176         status = smbd_smb2_lock_recv(subreq);
177         TALLOC_FREE(subreq);
178         if (!NT_STATUS_IS_OK(status)) {
179                 error = smbd_smb2_request_error(smb2req, status);
180                 if (!NT_STATUS_IS_OK(error)) {
181                         smbd_server_connection_terminate(smb2req->sconn,
182                                                          nt_errstr(error));
183                         return;
184                 }
185                 return;
186         }
187
188         outbody = data_blob_talloc(smb2req->out.vector, NULL, 0x04);
189         if (outbody.data == NULL) {
190                 error = smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
191                 if (!NT_STATUS_IS_OK(error)) {
192                         smbd_server_connection_terminate(smb2req->sconn,
193                                                          nt_errstr(error));
194                         return;
195                 }
196                 return;
197         }
198
199         SSVAL(outbody.data, 0x00, 0x04);        /* struct size */
200         SSVAL(outbody.data, 0x02, 0);           /* reserved */
201
202         error = smbd_smb2_request_done(smb2req, outbody, NULL);
203         if (!NT_STATUS_IS_OK(error)) {
204                 smbd_server_connection_terminate(smb2req->sconn,
205                                                  nt_errstr(error));
206                 return;
207         }
208 }
209
210 static struct tevent_req *smbd_smb2_lock_send(TALLOC_CTX *mem_ctx,
211                                                  struct tevent_context *ev,
212                                                  struct smbd_smb2_request *smb2req,
213                                                  uint32_t in_smbpid,
214                                                  uint64_t in_file_id_volatile,
215                                                  uint16_t in_lock_count,
216                                                  struct smbd_smb2_lock_element *in_locks)
217 {
218         struct tevent_req *req;
219         struct smbd_smb2_lock_state *state;
220         struct smb_request *smb1req;
221         connection_struct *conn = smb2req->tcon->compat_conn;
222         files_struct *fsp;
223         int32_t timeout = -1;
224         bool isunlock = false;
225         uint16_t i;
226         struct smbd_lock_element *locks;
227         NTSTATUS status;
228         bool async = false;
229
230         req = tevent_req_create(mem_ctx, &state,
231                         struct smbd_smb2_lock_state);
232         if (req == NULL) {
233                 return NULL;
234         }
235         state->smb2req = smb2req;
236         smb2req->subreq = req; /* So we can find this when going async. */
237
238         smb1req = smbd_smb2_fake_smb_request(smb2req);
239         if (tevent_req_nomem(smb1req, req)) {
240                 return tevent_req_post(req, ev);
241         }
242         state->smb1req = smb1req;
243
244         DEBUG(10,("smbd_smb2_lock_send: file_id[0x%016llX]\n",
245                   (unsigned long long)in_file_id_volatile));
246
247         fsp = file_fsp(smb1req, (uint16_t)in_file_id_volatile);
248         if (fsp == NULL) {
249                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
250                 return tevent_req_post(req, ev);
251         }
252         if (conn != fsp->conn) {
253                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
254                 return tevent_req_post(req, ev);
255         }
256         if (smb2req->session->vuid != fsp->vuid) {
257                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
258                 return tevent_req_post(req, ev);
259         }
260
261         locks = talloc_array(state, struct smbd_lock_element, in_lock_count);
262         if (locks == NULL) {
263                 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
264                 return tevent_req_post(req, ev);
265         }
266
267         switch (in_locks[0].flags) {
268         case SMB2_LOCK_FLAG_SHARED:
269         case SMB2_LOCK_FLAG_EXCLUSIVE:
270                 if (in_lock_count > 1) {
271                         tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
272                         return tevent_req_post(req, ev);
273                 }
274                 timeout = -1;
275                 break;
276
277         case SMB2_LOCK_FLAG_SHARED|SMB2_LOCK_FLAG_FAIL_IMMEDIATELY:
278         case SMB2_LOCK_FLAG_EXCLUSIVE|SMB2_LOCK_FLAG_FAIL_IMMEDIATELY:
279                 timeout = 0;
280                 break;
281
282         case SMB2_LOCK_FLAG_UNLOCK:
283                 /* only the first lock gives the UNLOCK bit - see
284                    MS-SMB2 3.3.5.14 */
285                 isunlock = true;
286                 timeout = 0;
287                 break;
288
289         default:
290                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
291                 return tevent_req_post(req, ev);
292         }
293
294         for (i=0; i<in_lock_count; i++) {
295                 bool invalid = false;
296
297                 switch (in_locks[i].flags) {
298                 case SMB2_LOCK_FLAG_SHARED:
299                 case SMB2_LOCK_FLAG_EXCLUSIVE:
300                         if (i > 0) {
301                                 tevent_req_nterror(req,
302                                                    NT_STATUS_INVALID_PARAMETER);
303                                 return tevent_req_post(req, ev);
304                         }
305                         if (isunlock) {
306                                 tevent_req_nterror(req,
307                                                    NT_STATUS_INVALID_PARAMETER);
308                                 return tevent_req_post(req, ev);
309                         }
310                         break;
311
312                 case SMB2_LOCK_FLAG_SHARED|SMB2_LOCK_FLAG_FAIL_IMMEDIATELY:
313                 case SMB2_LOCK_FLAG_EXCLUSIVE|SMB2_LOCK_FLAG_FAIL_IMMEDIATELY:
314                         if (isunlock) {
315                                 tevent_req_nterror(req,
316                                                    NT_STATUS_INVALID_PARAMETER);
317                                 return tevent_req_post(req, ev);
318                         }
319                         break;
320
321                 case SMB2_LOCK_FLAG_UNLOCK:
322                         if (!isunlock) {
323                                 tevent_req_nterror(req,
324                                                    NT_STATUS_INVALID_PARAMETER);
325                                 return tevent_req_post(req, ev);
326                         }
327                         break;
328
329                 default:
330                         if (isunlock) {
331                                 /*
332                                  * is the first element was a UNLOCK
333                                  * we need to deferr the error response
334                                  * to the backend, because we need to process
335                                  * all unlock elements before
336                                  */
337                                 invalid = true;
338                                 break;
339                         }
340                         tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
341                         return tevent_req_post(req, ev);
342                 }
343
344                 locks[i].smblctx = in_file_id_volatile;
345                 locks[i].offset = in_locks[i].offset;
346                 locks[i].count  = in_locks[i].length;
347
348                 if (in_locks[i].flags & SMB2_LOCK_FLAG_EXCLUSIVE) {
349                         locks[i].brltype = WRITE_LOCK;
350                 } else if (in_locks[i].flags & SMB2_LOCK_FLAG_SHARED) {
351                         locks[i].brltype = READ_LOCK;
352                 } else if (invalid) {
353                         /*
354                          * this is an invalid UNLOCK element
355                          * and the backend needs to test for
356                          * brltype != UNLOCK_LOCK and return
357                          * NT_STATUS_INVALID_PARAMER
358                          */
359                         locks[i].brltype = READ_LOCK;
360                 } else {
361                         locks[i].brltype = UNLOCK_LOCK;
362                 }
363
364                 DEBUG(10,("smbd_smb2_lock_send: index %d offset=%llu, count=%llu, "
365                         "smblctx = %llu type %d\n",
366                         i,
367                         (unsigned long long)locks[i].offset,
368                         (unsigned long long)locks[i].count,
369                         (unsigned long long)locks[i].smblctx,
370                         (int)locks[i].brltype ));
371
372         }
373
374         state->locks = locks;
375         state->lock_count = in_lock_count;
376
377         if (isunlock) {
378                 status = smbd_do_locking(smb1req, fsp,
379                                          0,
380                                          timeout,
381                                          in_lock_count,
382                                          locks,
383                                          0,
384                                          NULL,
385                                          &async);
386         } else {
387                 status = smbd_do_locking(smb1req, fsp,
388                                          0,
389                                          timeout,
390                                          0,
391                                          NULL,
392                                          in_lock_count,
393                                          locks,
394                                          &async);
395         }
396         if (!NT_STATUS_IS_OK(status)) {
397                 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_LOCK_CONFLICT)) {
398                        status = NT_STATUS_LOCK_NOT_GRANTED;
399                 }
400                 tevent_req_nterror(req, status);
401                 return tevent_req_post(req, ev);
402         }
403
404         if (async) {
405                 return req;
406         }
407
408         tevent_req_done(req);
409         return tevent_req_post(req, ev);
410 }
411
412 static NTSTATUS smbd_smb2_lock_recv(struct tevent_req *req)
413 {
414         NTSTATUS status;
415
416         if (tevent_req_is_nterror(req, &status)) {
417                 tevent_req_received(req);
418                 return status;
419         }
420
421         tevent_req_received(req);
422         return NT_STATUS_OK;
423 }
424
425 /****************************************************************
426  Cancel an outstanding blocking lock request.
427 *****************************************************************/
428
429 static bool smbd_smb2_lock_cancel(struct tevent_req *req)
430 {
431         struct smbd_smb2_request *smb2req = NULL;
432         struct smbd_smb2_lock_state *state = tevent_req_data(req,
433                                 struct smbd_smb2_lock_state);
434         if (!state) {
435                 return false;
436         }
437
438         if (!state->smb2req) {
439                 return false;
440         }
441
442         smb2req = state->smb2req;
443         smb2req->cancelled = true;
444
445         tevent_req_done(req);
446         return true;
447 }
448
449 /****************************************************************
450  Got a message saying someone unlocked a file. Re-schedule all
451  blocking lock requests as we don't know if anything overlapped.
452 *****************************************************************/
453
454 static void received_unlock_msg(struct messaging_context *msg,
455                                 void *private_data,
456                                 uint32_t msg_type,
457                                 struct server_id server_id,
458                                 DATA_BLOB *data)
459 {
460         DEBUG(10,("received_unlock_msg (SMB2)\n"));
461         process_blocking_lock_queue_smb2(timeval_current());
462 }
463
464 /****************************************************************
465  Function to get the blr on a pending record.
466 *****************************************************************/
467
468 struct blocking_lock_record *get_pending_smb2req_blr(struct smbd_smb2_request *smb2req)
469 {
470         struct smbd_smb2_lock_state *state = NULL;
471         const uint8_t *inhdr;
472
473         if (!smb2req) {
474                 return NULL;
475         }
476         if (smb2req->subreq == NULL) {
477                 return NULL;
478         }
479         if (!tevent_req_is_in_progress(smb2req->subreq)) {
480                 return NULL;
481         }
482         inhdr = (const uint8_t *)smb2req->in.vector[smb2req->current_idx].iov_base;
483         if (SVAL(inhdr, SMB2_HDR_OPCODE) != SMB2_OP_LOCK) {
484                 return NULL;
485         }
486         state = tevent_req_data(smb2req->subreq,
487                         struct smbd_smb2_lock_state);
488         if (!state) {
489                 return NULL;
490         }
491         return state->blr;
492 }
493 /****************************************************************
494  Set up the next brl timeout.
495 *****************************************************************/
496
497 static bool recalc_smb2_brl_timeout(struct smbd_server_connection *sconn)
498 {
499         struct smbd_smb2_request *smb2req;
500         struct timeval next_timeout = timeval_zero();
501         int max_brl_timeout = lp_parm_int(-1, "brl", "recalctime", 5);
502
503         TALLOC_FREE(sconn->smb2.locks.brl_timeout);
504
505         for (smb2req = sconn->smb2.requests; smb2req; smb2req = smb2req->next) {
506                 struct blocking_lock_record *blr =
507                         get_pending_smb2req_blr(smb2req);
508                 if (!blr) {
509                         continue;
510                 }
511                 if (timeval_is_zero(&blr->expire_time)) {
512                         /*
513                          * If we're blocked on pid 0xFFFFFFFFFFFFFFFFLL this is
514                          * a POSIX lock, so calculate a timeout of
515                          * 10 seconds into the future.
516                          */
517                         if (blr->blocking_smblctx == 0xFFFFFFFFFFFFFFFFLL) {
518                                 struct timeval psx_to = timeval_current_ofs(10, 0);
519                                 next_timeout = timeval_brl_min(&next_timeout, &psx_to);
520                         }
521
522                         continue;
523                 }
524
525                 next_timeout = timeval_brl_min(&next_timeout, &blr->expire_time);
526         }
527
528         if (timeval_is_zero(&next_timeout)) {
529                 DEBUG(10, ("recalc_smb2_brl_timeout:Next "
530                         "timeout = Infinite.\n"));
531                 return true;
532         }
533
534         /*
535          * To account for unclean shutdowns by clients we need a
536          * maximum timeout that we use for checking pending locks. If
537          * we have any pending locks at all, then check if the pending
538          * lock can continue at least every brl:recalctime seconds
539          * (default 5 seconds).
540          *
541          * This saves us needing to do a message_send_all() in the
542          * SIGCHLD handler in the parent daemon. That
543          * message_send_all() caused O(n^2) work to be done when IP
544          * failovers happened in clustered Samba, which could make the
545          * entire system unusable for many minutes.
546          */
547
548         if (max_brl_timeout > 0) {
549                 struct timeval min_to = timeval_current_ofs(max_brl_timeout, 0);
550                 next_timeout = timeval_brl_min(&next_timeout, &min_to);
551         }
552
553         if (DEBUGLVL(10)) {
554                 struct timeval cur, from_now;
555
556                 cur = timeval_current();
557                 from_now = timeval_until(&cur, &next_timeout);
558                 DEBUG(10, ("recalc_smb2_brl_timeout: Next "
559                         "timeout = %d.%d seconds from now.\n",
560                         (int)from_now.tv_sec, (int)from_now.tv_usec));
561         }
562
563         sconn->smb2.locks.brl_timeout = event_add_timed(
564                                 smbd_event_context(),
565                                 NULL,
566                                 next_timeout,
567                                 brl_timeout_fn,
568                                 NULL);
569         if (!sconn->smb2.locks.brl_timeout) {
570                 return false;
571         }
572         return true;
573 }
574
575 /****************************************************************
576  Get an SMB2 lock reqeust to go async. lock_timeout should
577  always be -1 here.
578 *****************************************************************/
579
580 bool push_blocking_lock_request_smb2( struct byte_range_lock *br_lck,
581                                 struct smb_request *smb1req,
582                                 files_struct *fsp,
583                                 int lock_timeout,
584                                 int lock_num,
585                                 uint64_t smblctx,
586                                 enum brl_type lock_type,
587                                 enum brl_flavour lock_flav,
588                                 uint64_t offset,
589                                 uint64_t count,
590                                 uint64_t blocking_smblctx)
591 {
592         struct smbd_server_connection *sconn = smbd_server_conn;
593         struct smbd_smb2_request *smb2req = smb1req->smb2req;
594         struct tevent_req *req = NULL;
595         struct smbd_smb2_lock_state *state = NULL;
596         struct blocking_lock_record *blr = NULL;
597         NTSTATUS status = NT_STATUS_OK;
598
599         if (!smb2req) {
600                 return false;
601         }
602         req = smb2req->subreq;
603         if (!req) {
604                 return false;
605         }
606         if (!tevent_req_is_in_progress(smb2req->subreq)) {
607                 return false;
608         }
609         state = tevent_req_data(req, struct smbd_smb2_lock_state);
610         if (!state) {
611                 return false;
612         }
613
614         blr = talloc_zero(state, struct blocking_lock_record);
615         if (!blr) {
616                 return false;
617         }
618         blr->fsp = fsp;
619
620         if (lock_timeout == -1) {
621                 blr->expire_time.tv_sec = 0;
622                 blr->expire_time.tv_usec = 0; /* Never expire. */
623         } else {
624                 blr->expire_time = timeval_current_ofs(
625                         lock_timeout/1000,
626                         (lock_timeout % 1000) * 1000);
627         }
628
629         blr->lock_num = lock_num;
630         blr->smblctx = smblctx;
631         blr->blocking_smblctx = blocking_smblctx;
632         blr->lock_flav = lock_flav;
633         blr->lock_type = lock_type;
634         blr->offset = offset;
635         blr->count = count;
636
637         /* Specific brl_lock() implementations can fill this in. */
638         blr->blr_private = NULL;
639
640         /* Add a pending lock record for this. */
641         status = brl_lock(smbd_messaging_context(),
642                         br_lck,
643                         smblctx,
644                         procid_self(),
645                         offset,
646                         count,
647                         lock_type == READ_LOCK ? PENDING_READ_LOCK : PENDING_WRITE_LOCK,
648                         blr->lock_flav,
649                         true,
650                         NULL,
651                         blr);
652
653         if (!NT_STATUS_IS_OK(status)) {
654                 DEBUG(0,("push_blocking_lock_request_smb2: "
655                         "failed to add PENDING_LOCK record.\n"));
656                 TALLOC_FREE(blr);
657                 return false;
658         }
659         state->blr = blr;
660
661         DEBUG(10,("push_blocking_lock_request_smb2: file %s timeout %d\n",
662                 fsp_str_dbg(fsp),
663                 lock_timeout ));
664
665         recalc_smb2_brl_timeout(sconn);
666
667         /* Ensure we'll receive messages when this is unlocked. */
668         if (!sconn->smb2.locks.blocking_lock_unlock_state) {
669                 messaging_register(smbd_messaging_context(), NULL,
670                                 MSG_SMB_UNLOCK, received_unlock_msg);
671                 sconn->smb2.locks.blocking_lock_unlock_state = true;
672         }
673
674         /* allow this request to be canceled */
675         tevent_req_set_cancel_fn(req, smbd_smb2_lock_cancel);
676
677         return true;
678 }
679
680 /****************************************************************
681  Remove a pending lock record under lock.
682 *****************************************************************/
683
684 static void remove_pending_lock(TALLOC_CTX *mem_ctx, struct blocking_lock_record *blr)
685 {
686         struct byte_range_lock *br_lck = brl_get_locks(
687                                 mem_ctx, blr->fsp);
688
689         DEBUG(10, ("remove_pending_lock: BLR = %p\n", blr));
690
691         if (br_lck) {
692                 brl_lock_cancel(br_lck,
693                                 blr->smblctx,
694                                 procid_self(),
695                                 blr->offset,
696                                 blr->count,
697                                 blr->lock_flav,
698                                 blr);
699                 TALLOC_FREE(br_lck);
700         }
701 }
702
703 /****************************************************************
704  Re-proccess a blocking lock request.
705  This is equivalent to process_lockingX() inside smbd/blocking.c
706 *****************************************************************/
707
708 static void reprocess_blocked_smb2_lock(struct smbd_smb2_request *smb2req,
709                                 struct timeval tv_curr)
710 {
711         NTSTATUS status;
712         struct blocking_lock_record *blr = NULL;
713         struct smbd_smb2_lock_state *state = NULL;
714         files_struct *fsp = NULL;
715
716         if (!smb2req->subreq) {
717                 return;
718         }
719         state = tevent_req_data(smb2req->subreq, struct smbd_smb2_lock_state);
720         if (!state) {
721                 return;
722         }
723
724         blr = state->blr;
725         fsp = blr->fsp;
726
727         /* Try and finish off getting all the outstanding locks. */
728
729         for (; blr->lock_num < state->lock_count; blr->lock_num++) {
730                 struct byte_range_lock *br_lck = NULL;
731                 struct smbd_lock_element *e = &state->locks[blr->lock_num];
732
733                 br_lck = do_lock(smbd_messaging_context(),
734                                 fsp,
735                                 e->smblctx,
736                                 e->count,
737                                 e->offset,
738                                 e->brltype,
739                                 WINDOWS_LOCK,
740                                 true,
741                                 &status,
742                                 &blr->blocking_smblctx,
743                                 blr);
744
745                 TALLOC_FREE(br_lck);
746
747                 if (NT_STATUS_IS_ERR(status)) {
748                         break;
749                 }
750         }
751
752         if(blr->lock_num == state->lock_count) {
753                 /*
754                  * Success - we got all the locks.
755                  */
756
757                 DEBUG(3,("reprocess_blocked_smb2_lock SUCCESS file = %s, "
758                         "fnum=%d num_locks=%d\n",
759                         fsp_str_dbg(fsp),
760                         fsp->fnum,
761                         (int)state->lock_count));
762
763                 tevent_req_done(smb2req->subreq);
764                 return;
765         }
766
767         if (!NT_STATUS_EQUAL(status,NT_STATUS_LOCK_NOT_GRANTED) &&
768                         !NT_STATUS_EQUAL(status,NT_STATUS_FILE_LOCK_CONFLICT)) {
769                 /*
770                  * We have other than a "can't get lock"
771                  * error. Return an error.
772                  */
773                 remove_pending_lock(state, blr);
774                 tevent_req_nterror(smb2req->subreq, status);
775                 return;
776         }
777
778         /*
779          * We couldn't get the locks for this record on the list.
780          * If the time has expired, return a lock error.
781          */
782
783         if (!timeval_is_zero(&blr->expire_time) &&
784                         timeval_compare(&blr->expire_time, &tv_curr) <= 0) {
785                 remove_pending_lock(state, blr);
786                 tevent_req_nterror(smb2req->subreq, NT_STATUS_LOCK_NOT_GRANTED);
787                 return;
788         }
789
790         /*
791          * Still can't get all the locks - keep waiting.
792          */
793
794         DEBUG(10,("reprocess_blocked_smb2_lock: only got %d locks of %d needed "
795                 "for file %s, fnum = %d. Still waiting....\n",
796                 (int)blr->lock_num,
797                 (int)state->lock_count,
798                 fsp_str_dbg(fsp),
799                 (int)fsp->fnum));
800
801         return;
802
803 }
804
805 /****************************************************************
806  Attempt to proccess all outstanding blocking locks pending on
807  the request queue.
808 *****************************************************************/
809
810 void process_blocking_lock_queue_smb2(struct timeval tv_curr)
811 {
812         struct smbd_server_connection *sconn = smbd_server_conn;
813         struct smbd_smb2_request *smb2req, *nextreq;
814
815         for (smb2req = sconn->smb2.requests; smb2req; smb2req = nextreq) {
816                 const uint8_t *inhdr;
817
818                 nextreq = smb2req->next;
819
820                 if (smb2req->subreq == NULL) {
821                         /* This message has been processed. */
822                         continue;
823                 }
824                 if (!tevent_req_is_in_progress(smb2req->subreq)) {
825                         /* This message has been processed. */
826                         continue;
827                 }
828
829                 inhdr = (const uint8_t *)smb2req->in.vector[smb2req->current_idx].iov_base;
830                 if (SVAL(inhdr, SMB2_HDR_OPCODE) == SMB2_OP_LOCK) {
831                         reprocess_blocked_smb2_lock(smb2req, tv_curr);
832                 }
833         }
834
835         recalc_smb2_brl_timeout(sconn);
836 }
837
838 /****************************************************************************
839  Remove any locks on this fd. Called from file_close().
840 ****************************************************************************/
841
842 void cancel_pending_lock_requests_by_fid_smb2(files_struct *fsp,
843                         struct byte_range_lock *br_lck,
844                         enum file_close_type close_type)
845 {
846         struct smbd_server_connection *sconn = smbd_server_conn;
847         struct smbd_smb2_request *smb2req, *nextreq;
848
849         for (smb2req = sconn->smb2.requests; smb2req; smb2req = nextreq) {
850                 struct smbd_smb2_lock_state *state = NULL;
851                 files_struct *fsp_curr = NULL;
852                 int i = smb2req->current_idx;
853                 uint64_t in_file_id_volatile;
854                 struct blocking_lock_record *blr = NULL;
855                 const uint8_t *inhdr;
856                 const uint8_t *inbody;
857
858                 nextreq = smb2req->next;
859
860                 if (smb2req->subreq == NULL) {
861                         /* This message has been processed. */
862                         continue;
863                 }
864                 if (!tevent_req_is_in_progress(smb2req->subreq)) {
865                         /* This message has been processed. */
866                         continue;
867                 }
868
869                 inhdr = (const uint8_t *)smb2req->in.vector[i].iov_base;
870                 if (SVAL(inhdr, SMB2_HDR_OPCODE) != SMB2_OP_LOCK) {
871                         /* Not a lock call. */
872                         continue;
873                 }
874
875                 inbody = (const uint8_t *)smb2req->in.vector[i+1].iov_base;
876                 in_file_id_volatile = BVAL(inbody, 0x10);
877
878                 state = tevent_req_data(smb2req->subreq,
879                                 struct smbd_smb2_lock_state);
880                 if (!state) {
881                         /* Strange - is this even possible ? */
882                         continue;
883                 }
884
885                 fsp_curr = file_fsp(state->smb1req, (uint16_t)in_file_id_volatile);
886                 if (fsp_curr == NULL) {
887                         /* Strange - is this even possible ? */
888                         continue;
889                 }
890
891                 if (fsp_curr != fsp) {
892                         /* It's not our fid */
893                         continue;
894                 }
895
896                 blr = state->blr;
897
898                 /* Remove the entries from the lock db. */
899                 brl_lock_cancel(br_lck,
900                                 blr->smblctx,
901                                 procid_self(),
902                                 blr->offset,
903                                 blr->count,
904                                 blr->lock_flav,
905                                 blr);
906
907                 /* Finally end the request. */
908                 if (close_type == SHUTDOWN_CLOSE) {
909                         tevent_req_done(smb2req->subreq);
910                 } else {
911                         tevent_req_nterror(smb2req->subreq,
912                                 NT_STATUS_RANGE_NOT_LOCKED);
913                 }
914         }
915 }