summaryrefslogtreecommitdiff
path: root/source3/smbd/smb2_lock.c
blob: bd0dd21ad0ff3803fa88902c45ddae8a59858902 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
/*
   Unix SMB/CIFS implementation.
   Core SMB2 server

   Copyright (C) Stefan Metzmacher 2009
   Copyright (C) Jeremy Allison 2010

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "smbd/globals.h"
#include "../libcli/smb/smb_common.h"
#include "librpc/gen_ndr/messaging.h"

struct smbd_smb2_lock_element {
	uint64_t offset;
	uint64_t length;
	uint32_t flags;
};

struct smbd_smb2_lock_state {
	struct smbd_smb2_request *smb2req;
	struct smb_request *smb1req;
	struct blocking_lock_record *blr;
	uint16_t lock_count;
	struct smbd_lock_element *locks;
};

static void remove_pending_lock(struct smbd_smb2_lock_state *state,
				struct blocking_lock_record *blr);

static struct tevent_req *smbd_smb2_lock_send(TALLOC_CTX *mem_ctx,
						 struct tevent_context *ev,
						 struct smbd_smb2_request *smb2req,
						 uint32_t in_smbpid,
						 uint64_t in_file_id_volatile,
						 uint16_t in_lock_count,
						 struct smbd_smb2_lock_element *in_locks);
static NTSTATUS smbd_smb2_lock_recv(struct tevent_req *req);

static void smbd_smb2_request_lock_done(struct tevent_req *subreq);
NTSTATUS smbd_smb2_request_process_lock(struct smbd_smb2_request *req)
{
	const uint8_t *inhdr;
	const uint8_t *inbody;
	const int i = req->current_idx;
	size_t expected_body_size = 0x30;
	size_t body_size;
	uint32_t in_smbpid;
	uint16_t in_lock_count;
	uint64_t in_file_id_persistent;
	uint64_t in_file_id_volatile;
	struct smbd_smb2_lock_element *in_locks;
	struct tevent_req *subreq;
	const uint8_t *lock_buffer;
	uint16_t l;

	inhdr = (const uint8_t *)req->in.vector[i+0].iov_base;
	if (req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
		return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
	}

	inbody = (const uint8_t *)req->in.vector[i+1].iov_base;

	body_size = SVAL(inbody, 0x00);
	if (body_size != expected_body_size) {
		return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
	}

	in_smbpid			= IVAL(inhdr, SMB2_HDR_PID);

	in_lock_count			= CVAL(inbody, 0x02);
	/* 0x04 - 4 bytes reserved */
	in_file_id_persistent		= BVAL(inbody, 0x08);
	in_file_id_volatile		= BVAL(inbody, 0x10);

	if (in_lock_count < 1) {
		return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
	}

	if (((in_lock_count - 1) * 0x18) > req->in.vector[i+2].iov_len) {
		return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
	}

	if (req->compat_chain_fsp) {
		/* skip check */
	} else if (in_file_id_persistent != in_file_id_volatile) {
		return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
	}

	in_locks = talloc_array(req, struct smbd_smb2_lock_element,
				in_lock_count);
	if (in_locks == NULL) {
		return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
	}

	l = 0;
	lock_buffer = inbody + 0x18;

	in_locks[l].offset	= BVAL(lock_buffer, 0x00);
	in_locks[l].length	= BVAL(lock_buffer, 0x08);
	in_locks[l].flags	= IVAL(lock_buffer, 0x10);
	/* 0x14 - 4 reserved bytes */

	lock_buffer = (const uint8_t *)req->in.vector[i+2].iov_base;

	for (l=1; l < in_lock_count; l++) {
		in_locks[l].offset	= BVAL(lock_buffer, 0x00);
		in_locks[l].length	= BVAL(lock_buffer, 0x08);
		in_locks[l].flags	= IVAL(lock_buffer, 0x10);
		/* 0x14 - 4 reserved bytes */

		lock_buffer += 0x18;
	}

	subreq = smbd_smb2_lock_send(req,
				     req->sconn->smb2.event_ctx,
				     req,
				     in_smbpid,
				     in_file_id_volatile,
				     in_lock_count,
				     in_locks);
	if (subreq == NULL) {
		return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
	}
	tevent_req_set_callback(subreq, smbd_smb2_request_lock_done, req);

	return smbd_smb2_request_pending_queue(req, subreq);
}

static void smbd_smb2_request_lock_done(struct tevent_req *subreq)
{
	struct smbd_smb2_request *smb2req = tevent_req_callback_data(subreq,
					struct smbd_smb2_request);
	DATA_BLOB outbody;
	NTSTATUS status;
	NTSTATUS error; /* transport error */

	if (smb2req->cancelled) {
		const uint8_t *inhdr = (const uint8_t *)
			smb2req->in.vector[smb2req->current_idx].iov_base;
		uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
		struct smbd_smb2_lock_state *state;

		DEBUG(10,("smbd_smb2_request_lock_done: cancelled mid %llu\n",
			(unsigned long long)mid ));

		state = tevent_req_data(smb2req->subreq,
				struct smbd_smb2_lock_state);

		SMB_ASSERT(state);
		SMB_ASSERT(state->blr);

		remove_pending_lock(state, state->blr);

		error = smbd_smb2_request_error(smb2req, NT_STATUS_CANCELLED);
		if (!NT_STATUS_IS_OK(error)) {
			smbd_server_connection_terminate(smb2req->sconn,
				nt_errstr(error));
			return;
		}
		return;
	}

	status = smbd_smb2_lock_recv(subreq);
	TALLOC_FREE(subreq);
	if (!NT_STATUS_IS_OK(status)) {
		error = smbd_smb2_request_error(smb2req, status);
		if (!NT_STATUS_IS_OK(error)) {
			smbd_server_connection_terminate(smb2req->sconn,
							 nt_errstr(error));
			return;
		}
		return;
	}

	outbody = data_blob_talloc(smb2req->out.vector, NULL, 0x04);
	if (outbody.data == NULL) {
		error = smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
		if (!NT_STATUS_IS_OK(error)) {
			smbd_server_connection_terminate(smb2req->sconn,
							 nt_errstr(error));
			return;
		}
		return;
	}

	SSVAL(outbody.data, 0x00, 0x04);	/* struct size */
	SSVAL(outbody.data, 0x02, 0);		/* reserved */

	error = smbd_smb2_request_done(smb2req, outbody, NULL);
	if (!NT_STATUS_IS_OK(error)) {
		smbd_server_connection_terminate(smb2req->sconn,
						 nt_errstr(error));
		return;
	}
}

static struct tevent_req *smbd_smb2_lock_send(TALLOC_CTX *mem_ctx,
						 struct tevent_context *ev,
						 struct smbd_smb2_request *smb2req,
						 uint32_t in_smbpid,
						 uint64_t in_file_id_volatile,
						 uint16_t in_lock_count,
						 struct smbd_smb2_lock_element *in_locks)
{
	struct tevent_req *req;
	struct smbd_smb2_lock_state *state;
	struct smb_request *smb1req;
	connection_struct *conn = smb2req->tcon->compat_conn;
	files_struct *fsp;
	int32_t timeout = -1;
	bool isunlock = false;
	uint16_t i;
	struct smbd_lock_element *locks;
	NTSTATUS status;
	bool async = false;

	req = tevent_req_create(mem_ctx, &state,
			struct smbd_smb2_lock_state);
	if (req == NULL) {
		return NULL;
	}
	state->smb2req = smb2req;
	smb2req->subreq = req; /* So we can find this when going async. */

	smb1req = smbd_smb2_fake_smb_request(smb2req);
	if (tevent_req_nomem(smb1req, req)) {
		return tevent_req_post(req, ev);
	}
	state->smb1req = smb1req;

	DEBUG(10,("smbd_smb2_lock_send: file_id[0x%016llX]\n",
		  (unsigned long long)in_file_id_volatile));

	fsp = file_fsp(smb1req, (uint16_t)in_file_id_volatile);
	if (fsp == NULL) {
		tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
		return tevent_req_post(req, ev);
	}
	if (conn != fsp->conn) {
		tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
		return tevent_req_post(req, ev);
	}
	if (smb2req->session->vuid != fsp->vuid) {
		tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
		return tevent_req_post(req, ev);
	}

	locks = talloc_array(state, struct smbd_lock_element, in_lock_count);
	if (locks == NULL) {
		tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
		return tevent_req_post(req, ev);
	}

	switch (in_locks[0].flags) {
	case SMB2_LOCK_FLAG_SHARED:
	case SMB2_LOCK_FLAG_EXCLUSIVE:
		if (in_lock_count > 1) {
			tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
			return tevent_req_post(req, ev);
		}
		timeout = -1;
		break;

	case SMB2_LOCK_FLAG_SHARED|SMB2_LOCK_FLAG_FAIL_IMMEDIATELY:
	case SMB2_LOCK_FLAG_EXCLUSIVE|SMB2_LOCK_FLAG_FAIL_IMMEDIATELY:
		timeout = 0;
		break;

	case SMB2_LOCK_FLAG_UNLOCK:
		/* only the first lock gives the UNLOCK bit - see
		   MS-SMB2 3.3.5.14 */
		isunlock = true;
		timeout = 0;
		break;

	default:
		tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
		return tevent_req_post(req, ev);
	}

	for (i=0; i<in_lock_count; i++) {
		bool invalid = false;

		switch (in_locks[i].flags) {
		case SMB2_LOCK_FLAG_SHARED:
		case SMB2_LOCK_FLAG_EXCLUSIVE:
			if (isunlock) {
				invalid = true;
				break;
			}
			if (i > 0) {
				tevent_req_nterror(req,
						   NT_STATUS_INVALID_PARAMETER);
				return tevent_req_post(req, ev);
			}
			break;

		case SMB2_LOCK_FLAG_SHARED|SMB2_LOCK_FLAG_FAIL_IMMEDIATELY:
		case SMB2_LOCK_FLAG_EXCLUSIVE|SMB2_LOCK_FLAG_FAIL_IMMEDIATELY:
			if (isunlock) {
				invalid = true;
			}
			break;

		case SMB2_LOCK_FLAG_UNLOCK:
			if (!isunlock) {
				tevent_req_nterror(req,
						   NT_STATUS_INVALID_PARAMETER);
				return tevent_req_post(req, ev);
			}
			break;

		default:
			if (isunlock) {
				/*
				 * is the first element was a UNLOCK
				 * we need to deferr the error response
				 * to the backend, because we need to process
				 * all unlock elements before
				 */
				invalid = true;
				break;
			}
			tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
			return tevent_req_post(req, ev);
		}

		locks[i].smblctx = in_file_id_volatile;
		locks[i].offset = in_locks[i].offset;
		locks[i].count  = in_locks[i].length;

		if (in_locks[i].flags & SMB2_LOCK_FLAG_EXCLUSIVE) {
			locks[i].brltype = WRITE_LOCK;
		} else if (in_locks[i].flags & SMB2_LOCK_FLAG_SHARED) {
			locks[i].brltype = READ_LOCK;
		} else if (invalid) {
			/*
			 * this is an invalid UNLOCK element
			 * and the backend needs to test for
			 * brltype != UNLOCK_LOCK and return
			 * NT_STATUS_INVALID_PARAMER
			 */
			locks[i].brltype = READ_LOCK;
		} else {
			locks[i].brltype = UNLOCK_LOCK;
		}

		DEBUG(10,("smbd_smb2_lock_send: index %d offset=%llu, count=%llu, "
			"smblctx = %llu type %d\n",
			i,
			(unsigned long long)locks[i].offset,
			(unsigned long long)locks[i].count,
			(unsigned long long)locks[i].smblctx,
			(int)locks[i].brltype ));
	}

	state->locks = locks;
	state->lock_count = in_lock_count;

	if (isunlock) {
		status = smbd_do_locking(smb1req, fsp,
					 0,
					 timeout,
					 in_lock_count,
					 locks,
					 0,
					 NULL,
					 &async);
	} else {
		status = smbd_do_locking(smb1req, fsp,
					 0,
					 timeout,
					 0,
					 NULL,
					 in_lock_count,
					 locks,
					 &async);
	}
	if (!NT_STATUS_IS_OK(status)) {
		if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_LOCK_CONFLICT)) {
		       status = NT_STATUS_LOCK_NOT_GRANTED;
		}
		tevent_req_nterror(req, status);
		return tevent_req_post(req, ev);
	}

	if (async) {
		return req;
	}

	tevent_req_done(req);
	return tevent_req_post(req, ev);
}

static NTSTATUS smbd_smb2_lock_recv(struct tevent_req *req)
{
	NTSTATUS status;

	if (tevent_req_is_nterror(req, &status)) {
		tevent_req_received(req);
		return status;
	}

	tevent_req_received(req);
	return NT_STATUS_OK;
}

/****************************************************************
 Cancel an outstanding blocking lock request.
*****************************************************************/

static bool smbd_smb2_lock_cancel(struct tevent_req *req)
{
        struct smbd_smb2_request *smb2req = NULL;
        struct smbd_smb2_lock_state *state = tevent_req_data(req,
                                struct smbd_smb2_lock_state);
        if (!state) {
                return false;
        }

        if (!state->smb2req) {
                return false;
        }

        smb2req = state->smb2req;
        smb2req->cancelled = true;

        tevent_req_done(req);
        return true;
}

/****************************************************************
 Got a message saying someone unlocked a file. Re-schedule all
 blocking lock requests as we don't know if anything overlapped.
*****************************************************************/

static void received_unlock_msg(struct messaging_context *msg,
				void *private_data,
				uint32_t msg_type,
				struct server_id server_id,
				DATA_BLOB *data)
{
	DEBUG(10,("received_unlock_msg (SMB2)\n"));
	process_blocking_lock_queue_smb2(smbd_server_conn, timeval_current());
}

/****************************************************************
 Function to get the blr on a pending record.
*****************************************************************/

struct blocking_lock_record *get_pending_smb2req_blr(struct smbd_smb2_request *smb2req)
{
	struct smbd_smb2_lock_state *state = NULL;
	const uint8_t *inhdr;

	if (!smb2req) {
		return NULL;
	}
	if (smb2req->subreq == NULL) {
		return NULL;
	}
	if (!tevent_req_is_in_progress(smb2req->subreq)) {
		return NULL;
	}
	inhdr = (const uint8_t *)smb2req->in.vector[smb2req->current_idx].iov_base;
	if (SVAL(inhdr, SMB2_HDR_OPCODE) != SMB2_OP_LOCK) {
		return NULL;
	}
	state = tevent_req_data(smb2req->subreq,
			struct smbd_smb2_lock_state);
	if (!state) {
		return NULL;
	}
	return state->blr;
}
/****************************************************************
 Set up the next brl timeout.
*****************************************************************/

static bool recalc_smb2_brl_timeout(struct smbd_server_connection *sconn)
{
	struct smbd_smb2_request *smb2req;
	struct timeval next_timeout = timeval_zero();
	int max_brl_timeout = lp_parm_int(-1, "brl", "recalctime", 5);

	TALLOC_FREE(sconn->smb2.locks.brl_timeout);

	for (smb2req = sconn->smb2.requests; smb2req; smb2req = smb2req->next) {
		struct blocking_lock_record *blr =
			get_pending_smb2req_blr(smb2req);
		if (!blr) {
			continue;
		}
		if (timeval_is_zero(&blr->expire_time)) {
			/*
			 * If we're blocked on pid 0xFFFFFFFFFFFFFFFFLL this is
			 * a POSIX lock, so calculate a timeout of
			 * 10 seconds into the future.
			 */
			if (blr->blocking_smblctx == 0xFFFFFFFFFFFFFFFFLL) {
				struct timeval psx_to = timeval_current_ofs(10, 0);
				next_timeout = timeval_brl_min(&next_timeout, &psx_to);
			}

			continue;
		}

		next_timeout = timeval_brl_min(&next_timeout, &blr->expire_time);
	}

	if (timeval_is_zero(&next_timeout)) {
		DEBUG(10, ("recalc_smb2_brl_timeout:Next "
			"timeout = Infinite.\n"));
		return true;
	}

        /*
	 * To account for unclean shutdowns by clients we need a
	 * maximum timeout that we use for checking pending locks. If
	 * we have any pending locks at all, then check if the pending
	 * lock can continue at least every brl:recalctime seconds
	 * (default 5 seconds).
	 *
	 * This saves us needing to do a message_send_all() in the
	 * SIGCHLD handler in the parent daemon. That
	 * message_send_all() caused O(n^2) work to be done when IP
	 * failovers happened in clustered Samba, which could make the
	 * entire system unusable for many minutes.
	 */

	if (max_brl_timeout > 0) {
		struct timeval min_to = timeval_current_ofs(max_brl_timeout, 0);
		next_timeout = timeval_brl_min(&next_timeout, &min_to);
	}

	if (DEBUGLVL(10)) {
		struct timeval cur, from_now;

		cur = timeval_current();
		from_now = timeval_until(&cur, &next_timeout);
		DEBUG(10, ("recalc_smb2_brl_timeout: Next "
			"timeout = %d.%d seconds from now.\n",
			(int)from_now.tv_sec, (int)from_now.tv_usec));
	}

	sconn->smb2.locks.brl_timeout = event_add_timed(
				smbd_event_context(),
				NULL,
				next_timeout,
				brl_timeout_fn,
				NULL);
	if (!sconn->smb2.locks.brl_timeout) {
		return false;
	}
	return true;
}

/****************************************************************
 Get an SMB2 lock reqeust to go async. lock_timeout should
 always be -1 here.
*****************************************************************/

bool push_blocking_lock_request_smb2( struct byte_range_lock *br_lck,
				struct smb_request *smb1req,
				files_struct *fsp,
				int lock_timeout,
				int lock_num,
				uint64_t smblctx,
				enum brl_type lock_type,
				enum brl_flavour lock_flav,
				uint64_t offset,
				uint64_t count,
				uint64_t blocking_smblctx)
{
	struct smbd_server_connection *sconn = smb1req->sconn;
	struct smbd_smb2_request *smb2req = smb1req->smb2req;
	struct tevent_req *req = NULL;
	struct smbd_smb2_lock_state *state = NULL;
	struct blocking_lock_record *blr = NULL;
	NTSTATUS status = NT_STATUS_OK;

	if (!smb2req) {
		return false;
	}
	req = smb2req->subreq;
	if (!req) {
		return false;
	}
	if (!tevent_req_is_in_progress(smb2req->subreq)) {
		return false;
	}
	state = tevent_req_data(req, struct smbd_smb2_lock_state);
	if (!state) {
		return false;
	}

	blr = talloc_zero(state, struct blocking_lock_record);
	if (!blr) {
		return false;
	}
	blr->fsp = fsp;

	if (lock_timeout == -1) {
		blr->expire_time.tv_sec = 0;
		blr->expire_time.tv_usec = 0; /* Never expire. */
	} else {
		blr->expire_time = timeval_current_ofs(
			lock_timeout/1000,
			(lock_timeout % 1000) * 1000);
	}

	blr->lock_num = lock_num;
	blr->smblctx = smblctx;
	blr->blocking_smblctx = blocking_smblctx;
	blr->lock_flav = lock_flav;
	blr->lock_type = lock_type;
	blr->offset = offset;
	blr->count = count;

	/* Specific brl_lock() implementations can fill this in. */
	blr->blr_private = NULL;

	/* Add a pending lock record for this. */
	status = brl_lock(sconn->msg_ctx,
			br_lck,
			smblctx,
			sconn_server_id(sconn),
			offset,
			count,
			lock_type == READ_LOCK ? PENDING_READ_LOCK : PENDING_WRITE_LOCK,
			blr->lock_flav,
			true,
			NULL,
			blr);

	if (!NT_STATUS_IS_OK(status)) {
		DEBUG(0,("push_blocking_lock_request_smb2: "
			"failed to add PENDING_LOCK record.\n"));
		TALLOC_FREE(blr);
		return false;
	}
	state->blr = blr;

	DEBUG(10,("push_blocking_lock_request_smb2: file %s timeout %d\n",
		fsp_str_dbg(fsp),
		lock_timeout ));

	recalc_smb2_brl_timeout(sconn);

	/* Ensure we'll receive messages when this is unlocked. */
	if (!sconn->smb2.locks.blocking_lock_unlock_state) {
		messaging_register(smbd_messaging_context(), NULL,
				MSG_SMB_UNLOCK, received_unlock_msg);
		sconn->smb2.locks.blocking_lock_unlock_state = true;
        }

	/* allow this request to be canceled */
	tevent_req_set_cancel_fn(req, smbd_smb2_lock_cancel);

	return true;
}

/****************************************************************
 Remove a pending lock record under lock.
*****************************************************************/

static void remove_pending_lock(struct smbd_smb2_lock_state *state,
			struct blocking_lock_record *blr)
{
	int i;
	struct byte_range_lock *br_lck = brl_get_locks(
				state, blr->fsp);

	DEBUG(10, ("remove_pending_lock: BLR = %p\n", blr));

	if (br_lck) {
		brl_lock_cancel(br_lck,
				blr->smblctx,
				procid_self(),
				blr->offset,
				blr->count,
				blr->lock_flav,
				blr);
		TALLOC_FREE(br_lck);
	}

	/* Remove the locks we already got. */

	for(i = blr->lock_num - 1; i >= 0; i--) {
		struct smbd_lock_element *e = &state->locks[i];

		do_unlock(smbd_messaging_context(),
			blr->fsp,
			e->smblctx,
			e->count,
			e->offset,
			WINDOWS_LOCK);
	}
}

/****************************************************************
 Re-proccess a blocking lock request.
 This is equivalent to process_lockingX() inside smbd/blocking.c
*****************************************************************/

static void reprocess_blocked_smb2_lock(struct smbd_smb2_request *smb2req,
				struct timeval tv_curr)
{
	NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
	struct blocking_lock_record *blr = NULL;
	struct smbd_smb2_lock_state *state = NULL;
	files_struct *fsp = NULL;

	if (!smb2req->subreq) {
		return;
	}
	state = tevent_req_data(smb2req->subreq, struct smbd_smb2_lock_state);
	if (!state) {
		return;
	}

	blr = state->blr;
	fsp = blr->fsp;

	/* Try and finish off getting all the outstanding locks. */

	for (; blr->lock_num < state->lock_count; blr->lock_num++) {
		struct byte_range_lock *br_lck = NULL;
		struct smbd_lock_element *e = &state->locks[blr->lock_num];

		br_lck = do_lock(smbd_messaging_context(),
				fsp,
				e->smblctx,
				e->count,
				e->offset,
				e->brltype,
				WINDOWS_LOCK,
				true,
				&status,
				&blr->blocking_smblctx,
				blr);

		TALLOC_FREE(br_lck);

		if (NT_STATUS_IS_ERR(status)) {
			break;
		}
	}

	if(blr->lock_num == state->lock_count) {
		/*
		 * Success - we got all the locks.
		 */

		DEBUG(3,("reprocess_blocked_smb2_lock SUCCESS file = %s, "
			"fnum=%d num_locks=%d\n",
			fsp_str_dbg(fsp),
			fsp->fnum,
			(int)state->lock_count));

		tevent_req_done(smb2req->subreq);
		return;
	}

	if (!NT_STATUS_EQUAL(status,NT_STATUS_LOCK_NOT_GRANTED) &&
			!NT_STATUS_EQUAL(status,NT_STATUS_FILE_LOCK_CONFLICT)) {
		/*
		 * We have other than a "can't get lock"
		 * error. Return an error.
		 */
		remove_pending_lock(state, blr);
		tevent_req_nterror(smb2req->subreq, status);
		return;
        }

	/*
	 * We couldn't get the locks for this record on the list.
	 * If the time has expired, return a lock error.
	 */

	if (!timeval_is_zero(&blr->expire_time) &&
			timeval_compare(&blr->expire_time, &tv_curr) <= 0) {
		remove_pending_lock(state, blr);
		tevent_req_nterror(smb2req->subreq, NT_STATUS_LOCK_NOT_GRANTED);
		return;
	}

	/*
	 * Still can't get all the locks - keep waiting.
	 */

	DEBUG(10,("reprocess_blocked_smb2_lock: only got %d locks of %d needed "
		"for file %s, fnum = %d. Still waiting....\n",
		(int)blr->lock_num,
		(int)state->lock_count,
		fsp_str_dbg(fsp),
		(int)fsp->fnum));

        return;

}

/****************************************************************
 Attempt to proccess all outstanding blocking locks pending on
 the request queue.
*****************************************************************/

void process_blocking_lock_queue_smb2(
	struct smbd_server_connection *sconn, struct timeval tv_curr)
{
	struct smbd_smb2_request *smb2req, *nextreq;

	for (smb2req = sconn->smb2.requests; smb2req; smb2req = nextreq) {
		const uint8_t *inhdr;

		nextreq = smb2req->next;

		if (smb2req->subreq == NULL) {
			/* This message has been processed. */
			continue;
		}
		if (!tevent_req_is_in_progress(smb2req->subreq)) {
			/* This message has been processed. */
			continue;
		}

		inhdr = (const uint8_t *)smb2req->in.vector[smb2req->current_idx].iov_base;
		if (SVAL(inhdr, SMB2_HDR_OPCODE) == SMB2_OP_LOCK) {
			reprocess_blocked_smb2_lock(smb2req, tv_curr);
		}
	}

	recalc_smb2_brl_timeout(sconn);
}

/****************************************************************************
 Remove any locks on this fd. Called from file_close().
****************************************************************************/

void cancel_pending_lock_requests_by_fid_smb2(files_struct *fsp,
			struct byte_range_lock *br_lck,
			enum file_close_type close_type)
{
	struct smbd_server_connection *sconn = fsp->conn->sconn;
	struct smbd_smb2_request *smb2req, *nextreq;

	for (smb2req = sconn->smb2.requests; smb2req; smb2req = nextreq) {
		struct smbd_smb2_lock_state *state = NULL;
		files_struct *fsp_curr = NULL;
		int i = smb2req->current_idx;
		uint64_t in_file_id_volatile;
		struct blocking_lock_record *blr = NULL;
		const uint8_t *inhdr;
		const uint8_t *inbody;

		nextreq = smb2req->next;

		if (smb2req->subreq == NULL) {
			/* This message has been processed. */
			continue;
		}
		if (!tevent_req_is_in_progress(smb2req->subreq)) {
			/* This message has been processed. */
			continue;
		}

		inhdr = (const uint8_t *)smb2req->in.vector[i].iov_base;
		if (SVAL(inhdr, SMB2_HDR_OPCODE) != SMB2_OP_LOCK) {
			/* Not a lock call. */
			continue;
		}

		inbody = (const uint8_t *)smb2req->in.vector[i+1].iov_base;
		in_file_id_volatile = BVAL(inbody, 0x10);

		state = tevent_req_data(smb2req->subreq,
				struct smbd_smb2_lock_state);
		if (!state) {
			/* Strange - is this even possible ? */
			continue;
		}

		fsp_curr = file_fsp(state->smb1req, (uint16_t)in_file_id_volatile);
		if (fsp_curr == NULL) {
			/* Strange - is this even possible ? */
			continue;
		}

		if (fsp_curr != fsp) {
			/* It's not our fid */
			continue;
		}

		blr = state->blr;

		/* Remove the entries from the lock db. */
		brl_lock_cancel(br_lck,
				blr->smblctx,
				procid_self(),
				blr->offset,
				blr->count,
				blr->lock_flav,
				blr);

		/* Finally end the request. */
		if (close_type == SHUTDOWN_CLOSE) {
			tevent_req_done(smb2req->subreq);
		} else {
			tevent_req_nterror(smb2req->subreq,
				NT_STATUS_RANGE_NOT_LOCKED);
		}
	}
}