summaryrefslogtreecommitdiff
path: root/source4/cluster/ctdb/brlock_ctdb.c
blob: 00fcb0335a2463aa8c3bccb1dc16806fa0a833e1 (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
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
/* 
   Unix SMB/CIFS implementation.

   generic byte range locking code - ctdb backend

   Copyright (C) Andrew Tridgell 2006
   
   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 2 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, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"
#include "system/filesys.h"
#include "lib/tdb/include/tdb.h"
#include "messaging/messaging.h"
#include "db_wrap.h"
#include "lib/messaging/irpc.h"
#include "libcli/libcli.h"
#include "cluster/cluster.h"
#include "ntvfs/common/brlock.h"
#include "cluster/ctdb/include/ctdb.h"

#define ENABLE_NOTIFIES 0

enum my_functions {FUNC_BRL_LOCK=1, FUNC_BRL_UNLOCK=2, 
		   FUNC_BRL_REMOVE_PENDING=3, FUNC_BRL_LOCKTEST=4,
		   FUNC_BRL_CLOSE=5};

/*
  in this module a "DATA_BLOB *file_key" is a blob that uniquely identifies
  a file. For a local posix filesystem this will usually be a combination
  of the device and inode numbers of the file, but it can be anything 
  that uniquely idetifies a file for locking purposes, as long
  as it is applied consistently.
*/

/* this struct is typically attached to tcon */
struct brl_context {
	struct ctdb_context *ctdb;
	struct server_id server;
	struct messaging_context *messaging_ctx;
};

/*
  the lock context contains the elements that define whether one
  lock is the same as another lock
*/
struct lock_context {
	struct server_id server;
	uint16_t smbpid;
	struct brl_context *ctx;
};

/* The data in brlock records is an unsorted linear array of these
   records.  It is unnecessary to store the count as tdb provides the
   size of the record */
struct lock_struct {
	struct lock_context context;
	struct ntvfs_handle *ntvfs;
	uint64_t start;
	uint64_t size;
	enum brl_type lock_type;
	void *notify_ptr;
};

/* this struct is attached to on open file handle */
struct brl_handle {
	DATA_BLOB key;
	struct ntvfs_handle *ntvfs;
	struct lock_struct last_lock;
};

/*
  Open up the brlock.tdb database. Close it down using
  talloc_free(). We need the messaging_ctx to allow for
  pending lock notifications.
*/
static struct brl_context *brl_ctdb_init(TALLOC_CTX *mem_ctx, struct server_id server, 
				    struct messaging_context *messaging_ctx)
{
	struct ctdb_context *ctdb = talloc_get_type(cluster_private(), struct ctdb_context);
	struct brl_context *brl;

	brl = talloc(mem_ctx, struct brl_context);
	if (brl == NULL) {
		return NULL;
	}

	brl->ctdb = ctdb;
	brl->server = server;
	brl->messaging_ctx = messaging_ctx;

	DEBUG(0,("brl_ctdb_init: brl=%p\n", brl));

	return brl;
}

static struct brl_handle *brl_ctdb_create_handle(TALLOC_CTX *mem_ctx, struct ntvfs_handle *ntvfs, 
						    DATA_BLOB *file_key)
{
	struct brl_handle *brlh;

	brlh = talloc(mem_ctx, struct brl_handle);
	if (brlh == NULL) {
		return NULL;
	}

	brlh->key = *file_key;
	brlh->ntvfs = ntvfs;
	ZERO_STRUCT(brlh->last_lock);

	return brlh;
}

/*
  see if two locking contexts are equal
*/
static BOOL brl_ctdb_same_context(struct lock_context *ctx1, struct lock_context *ctx2)
{
	return (cluster_id_equal(&ctx1->server, &ctx2->server) &&
		ctx1->smbpid == ctx2->smbpid &&
		ctx1->ctx == ctx2->ctx);
}

/*
  see if lck1 and lck2 overlap
*/
static BOOL brl_ctdb_overlap(struct lock_struct *lck1, 
			struct lock_struct *lck2)
{
	/* this extra check is not redundent - it copes with locks
	   that go beyond the end of 64 bit file space */
	if (lck1->size != 0 &&
	    lck1->start == lck2->start &&
	    lck1->size == lck2->size) {
		return True;
	}
	    
	if (lck1->start >= (lck2->start+lck2->size) ||
	    lck2->start >= (lck1->start+lck1->size)) {
		return False;
	}
	return True;
} 

/*
 See if lock2 can be added when lock1 is in place.
*/
static BOOL brl_ctdb_conflict(struct lock_struct *lck1, 
			 struct lock_struct *lck2)
{
	/* pending locks don't conflict with anything */
	if (lck1->lock_type >= PENDING_READ_LOCK ||
	    lck2->lock_type >= PENDING_READ_LOCK) {
		return False;
	}

	if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
		return False;
	}

	if (brl_ctdb_same_context(&lck1->context, &lck2->context) &&
	    lck2->lock_type == READ_LOCK && lck1->ntvfs == lck2->ntvfs) {
		return False;
	}

	return brl_ctdb_overlap(lck1, lck2);
} 


/*
 Check to see if this lock conflicts, but ignore our own locks on the
 same fnum only.
*/
static BOOL brl_ctdb_conflict_other(struct lock_struct *lck1, struct lock_struct *lck2)
{
	/* pending locks don't conflict with anything */
	if (lck1->lock_type >= PENDING_READ_LOCK ||
	    lck2->lock_type >= PENDING_READ_LOCK) {
		return False;
	}

	if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) 
		return False;

	/*
	 * note that incoming write calls conflict with existing READ
	 * locks even if the context is the same. JRA. See LOCKTEST7
	 * in smbtorture.
	 */
	if (brl_ctdb_same_context(&lck1->context, &lck2->context) &&
	    lck1->ntvfs == lck2->ntvfs &&
	    (lck2->lock_type == READ_LOCK || lck1->lock_type == WRITE_LOCK)) {
		return False;
	}

	return brl_ctdb_overlap(lck1, lck2);
} 


/*
  amazingly enough, w2k3 "remembers" whether the last lock failure
  is the same as this one and changes its error code. I wonder if any
  app depends on this?
*/
static NTSTATUS brl_ctdb_lock_failed(struct brl_handle *brlh, struct lock_struct *lock)
{
	/*
	 * this function is only called for non pending lock!
	 */

	/* 
	 * if the notify_ptr is non NULL,
	 * it means that we're at the end of a pending lock
	 * and the real lock is requested after the timeout went by
	 * In this case we need to remember the last_lock and always
	 * give FILE_LOCK_CONFLICT
	 */
	if (lock->notify_ptr) {
		brlh->last_lock = *lock;
		return NT_STATUS_FILE_LOCK_CONFLICT;
	}

	/* 
	 * amazing the little things you learn with a test
	 * suite. Locks beyond this offset (as a 64 bit
	 * number!) always generate the conflict error code,
	 * unless the top bit is set
	 */
	if (lock->start >= 0xEF000000 && (lock->start >> 63) == 0) {
		brlh->last_lock = *lock;
		return NT_STATUS_FILE_LOCK_CONFLICT;
	}

	/*
	 * if the current lock matches the last failed lock on the file handle
	 * and starts at the same offset, then FILE_LOCK_CONFLICT should be returned
	 */
	if (cluster_id_equal(&lock->context.server, &brlh->last_lock.context.server) &&
	    lock->context.ctx == brlh->last_lock.context.ctx &&
	    lock->ntvfs == brlh->last_lock.ntvfs &&
	    lock->start == brlh->last_lock.start) {
		return NT_STATUS_FILE_LOCK_CONFLICT;
	}

	brlh->last_lock = *lock;
	return NT_STATUS_LOCK_NOT_GRANTED;
}


static void show_locks(const char *op, struct lock_struct *locks, int count)
{
	int i;
	DEBUG(0,("OP: %s\n", op));
	for (i=0;i<count;i++) {
		DEBUG(0,("%2d: %4d %4d %d.%d.%d %p %p\n",
			 i, (int)locks[i].start, (int)locks[i].size, 
			 locks[i].context.server.node,
			 locks[i].context.server.id,
			 locks[i].context.smbpid,
			 locks[i].context.ctx,
			 locks[i].ntvfs));
	}
}


struct ctdb_lock_req {
	uint16_t smbpid;
	uint64_t start;
	uint64_t size;
	enum brl_type lock_type;
	void *notify_ptr;
	struct server_id server;
	struct brl_context *brl;
	struct ntvfs_handle *ntvfs;
};

/*
  ctdb call handling brl_lock()
*/
static int brl_ctdb_lock_func(struct ctdb_call *call)
{
	struct ctdb_lock_req *req = (struct ctdb_lock_req *)call->call_data->dptr;
	TDB_DATA dbuf;
	int count=0, i;
	struct lock_struct lock, *locks=NULL;
	NTSTATUS status = NT_STATUS_OK;

#if ENABLE_NOTIFIES
	/* if this is a pending lock, then with the chainlock held we
	   try to get the real lock. If we succeed then we don't need
	   to make it pending. This prevents a possible race condition
	   where the pending lock gets created after the lock that is
	   preventing the real lock gets removed */
	if (lock_type >= PENDING_READ_LOCK) {
		enum brl_type rw = (lock_type==PENDING_READ_LOCK? READ_LOCK : WRITE_LOCK);

		/* here we need to force that the last_lock isn't overwritten */
		lock = brlh->last_lock;
		status = brl_ctdb_lock(brl, brlh, smbpid, start, size, rw, NULL);
		brlh->last_lock = lock;

		if (NT_STATUS_IS_OK(status)) {
			tdb_chainunlock(brl->w->tdb, kbuf);
			return NT_STATUS_OK;
		}
	}
#endif

	dbuf = call->record_data;

	ZERO_STRUCT(lock);
	lock.context.smbpid = req->smbpid;
	lock.context.server = req->server;
	lock.context.ctx = req->brl;
	lock.ntvfs = req->ntvfs;
	lock.start = req->start;
	lock.size = req->size;
	lock.lock_type = req->lock_type;
	lock.notify_ptr = req->notify_ptr;

	{
		int xlen = sizeof(lock);
		uint8_t *xx = &lock;
		int ii, fd = open("/dev/null", O_WRONLY);
		for (ii=0;ii<xlen;ii++) {
			write(fd, &xx[ii], 1);
		}
		close(fd);
	}

	if (dbuf.dptr) {
		/* there are existing locks - make sure they don't conflict */
		locks = (struct lock_struct *)dbuf.dptr;
		count = dbuf.dsize / sizeof(*locks);

		show_locks("lock", locks, count);

		for (i=0; i<count; i++) {
			if (brl_ctdb_conflict(&locks[i], &lock)) {
				status = NT_STATUS_LOCK_NOT_GRANTED;
				goto reply;
			}
		}
	}

	call->new_data = talloc(call, TDB_DATA);
	if (call->new_data == NULL) {
		return CTDB_ERR_NOMEM;
	}

	call->new_data->dptr = talloc_size(call, dbuf.dsize + sizeof(lock));
	if (call->new_data->dptr == NULL) {
		return CTDB_ERR_NOMEM;
	}
	memcpy(call->new_data->dptr, locks, dbuf.dsize);
	memcpy(call->new_data->dptr+dbuf.dsize, &lock, sizeof(lock));
	call->new_data->dsize = dbuf.dsize + sizeof(lock);

	if (req->lock_type >= PENDING_READ_LOCK) {
		status = NT_STATUS_LOCK_NOT_GRANTED;
	}

	DEBUG(0,("lock: size now %d\n", call->new_data->dsize));

reply:
	call->reply_data = talloc(call, TDB_DATA);
	if (call->reply_data == NULL) {
		return CTDB_ERR_NOMEM;
	}

	call->reply_data->dptr = talloc_size(call, sizeof(NTSTATUS));
	call->reply_data->dsize = sizeof(NTSTATUS);
	if (call->reply_data->dptr == NULL) {
		return CTDB_ERR_NOMEM;
	}
	*(NTSTATUS *)call->reply_data->dptr = status;

	return 0;
}


/*
  Lock a range of bytes.  The lock_type can be a PENDING_*_LOCK, in
  which case a real lock is first tried, and if that fails then a
  pending lock is created. When the pending lock is triggered (by
  someone else closing an overlapping lock range) a messaging
  notification is sent, identified by the notify_ptr
*/
static NTSTATUS brl_ctdb_lock(struct brl_context *brl,
			      struct brl_handle *brlh,
			      uint16_t smbpid,
			      uint64_t start, uint64_t size, 
			      enum brl_type lock_type,
			      void *notify_ptr)
{
	TDB_DATA kbuf, rbuf, sbuf;
	struct ctdb_lock_req req;
	NTSTATUS status;
	int ret;

	kbuf.dptr = brlh->key.data;
	kbuf.dsize = brlh->key.length;

	rbuf.dptr = (uint8_t *)&req;
	rbuf.dsize = sizeof(req);

	ZERO_STRUCT(req);
	req.smbpid = smbpid;
	req.start  = start;
	req.size   = size;
	req.lock_type = lock_type;
	req.notify_ptr = notify_ptr;
	req.server = brl->server;
	req.brl = brl;
	req.ntvfs = brlh->ntvfs;
		
	ret = ctdb_call(brl->ctdb, kbuf, FUNC_BRL_LOCK, &rbuf, &sbuf);
	if (ret == -1) {
		DEBUG(0,("ctdb_call failed - %s\n", __location__));
		return NT_STATUS_INTERNAL_DB_CORRUPTION;
	}

	status = *(NTSTATUS *)sbuf.dptr;
	talloc_free(sbuf.dptr);

	return status;
}

#if ENABLE_NOTIFIES
/*
  we are removing a lock that might be holding up a pending lock. Scan for pending
  locks that cover this range and if we find any then notify the server that it should
  retry the lock
*/
static void brl_ctdb_notify_unlock(struct brl_context *brl,
			      struct lock_struct *locks, int count, 
			      struct lock_struct *removed_lock)
{
	int i, last_notice;

	/* the last_notice logic is to prevent stampeding on a lock
	   range. It prevents us sending hundreds of notifies on the
	   same range of bytes. It doesn't prevent all possible
	   stampedes, but it does prevent the most common problem */
	last_notice = -1;

	for (i=0;i<count;i++) {
		if (locks[i].lock_type >= PENDING_READ_LOCK &&
		    brl_ctdb_overlap(&locks[i], removed_lock)) {
			if (last_notice != -1 && brl_ctdb_overlap(&locks[i], &locks[last_notice])) {
				continue;
			}
			if (locks[i].lock_type == PENDING_WRITE_LOCK) {
				last_notice = i;
			}
			messaging_send_ptr(brl->messaging_ctx, locks[i].context.server, 
					   MSG_BRL_RETRY, locks[i].notify_ptr);
		}
	}
}
#endif

/*
  send notifications for all pending locks - the file is being closed by this
  user
*/
static void brl_ctdb_notify_all(struct brl_context *brl,
			   struct lock_struct *locks, int count)
{
	int i;
	for (i=0;i<count;i++) {
		if (locks->lock_type >= PENDING_READ_LOCK) {
#if ENABLE_NOTIFIES
			brl_ctdb_notify_unlock(brl, locks, count, &locks[i]);
#endif
		}
	}
}

struct ctdb_unlock_req {
	uint16_t smbpid;
	uint64_t start;
	uint64_t size;
	struct server_id server;
	struct brl_context *brl;
	struct ntvfs_handle *ntvfs;
};

/*
 Unlock a range of bytes.
*/
static int brl_ctdb_unlock_func(struct ctdb_call *call)
{
	struct ctdb_unlock_req *req = (struct ctdb_unlock_req *)call->call_data->dptr;
	TDB_DATA dbuf;
	int count, i;
	struct lock_struct *locks;
	struct lock_context context;
	NTSTATUS status = NT_STATUS_OK;

	dbuf = call->record_data;

	context.smbpid = req->smbpid;
	context.server = req->server;
	context.ctx = req->brl;

	/* there are existing locks - find a match */
	locks = (struct lock_struct *)dbuf.dptr;
	count = dbuf.dsize / sizeof(*locks);

	show_locks("unlock", locks, count);

	for (i=0; i<count; i++) {
		struct lock_struct *lock = &locks[i];
		
		if (brl_ctdb_same_context(&lock->context, &context) &&
		    lock->ntvfs == req->ntvfs &&
		    lock->start == req->start &&
		    lock->size == req->size &&
		    lock->lock_type < PENDING_READ_LOCK) {
#if ENABLE_NOTIFIES
			struct lock_struct removed_lock = *lock;
#endif

			call->new_data = talloc(call, TDB_DATA);
			if (call->new_data == NULL) {
				return CTDB_ERR_NOMEM;
			}

			call->new_data->dptr = talloc_size(call, dbuf.dsize - sizeof(lock));
			if (call->new_data->dptr == NULL) {
				return CTDB_ERR_NOMEM;
			}
			call->new_data->dsize = dbuf.dsize - sizeof(lock);

			memcpy(call->new_data->dptr, locks, i*sizeof(lock));
			memcpy(call->new_data->dptr+i*sizeof(lock), locks+i+1,
			       (count-(i+1))*sizeof(lock));
			
			if (count > 1) {
#if ENABLE_NOTIFIES
				brl_ctdb_notify_unlock(req->brl, locks, count, &removed_lock);
#endif
			}
			break;
		}
	}

	if (call->new_data) {
		DEBUG(0,("unlock: size now %d\n", call->new_data->dsize));
	}
	
	if (i == count) {
		/* we didn't find it */
		status = NT_STATUS_RANGE_NOT_LOCKED;
	}

	call->reply_data = talloc(call, TDB_DATA);
	if (call->reply_data == NULL) {
		return CTDB_ERR_NOMEM;
	}

	call->reply_data->dptr = talloc_size(call, sizeof(NTSTATUS));
	call->reply_data->dsize = sizeof(NTSTATUS);
	if (call->reply_data->dptr == NULL) {
		return CTDB_ERR_NOMEM;
	}
	*(NTSTATUS *)call->reply_data->dptr = status;

	return 0;
}


/*
 Unlock a range of bytes.
*/
static NTSTATUS brl_ctdb_unlock(struct brl_context *brl,
				struct brl_handle *brlh, 
				uint16_t smbpid,
				uint64_t start, uint64_t size)
{
	TDB_DATA kbuf, rbuf, sbuf;
	struct ctdb_unlock_req req;
	NTSTATUS status;
	int ret;

	kbuf.dptr = brlh->key.data;
	kbuf.dsize = brlh->key.length;

	rbuf.dptr = (uint8_t *)&req;
	rbuf.dsize = sizeof(req);

	ZERO_STRUCT(req);
	req.smbpid = smbpid;
	req.start  = start;
	req.size   = size;
	req.server = brl->server;
	req.brl = brl;
	req.ntvfs = brlh->ntvfs;
		
	ret = ctdb_call(brl->ctdb, kbuf, FUNC_BRL_UNLOCK, &rbuf, &sbuf);
	if (ret == -1) {
		DEBUG(0,("ctdb_call failed - %s\n", __location__));
		return NT_STATUS_INTERNAL_DB_CORRUPTION;
	}

	status = *(NTSTATUS *)sbuf.dptr;
	talloc_free(sbuf.dptr);

	return status;
}


struct ctdb_remove_pending_req {
	struct server_id server;
	void *notify_ptr;
};

/*
  remove a pending lock. This is called when the caller has either
  given up trying to establish a lock or when they have succeeded in
  getting it. In either case they no longer need to be notified.
*/
static int brl_ctdb_remove_pending_func(struct ctdb_call *call)
{
	struct ctdb_remove_pending_req *req = (struct ctdb_remove_pending_req *)call->call_data->dptr;
	TDB_DATA dbuf;
	int count, i;
	struct lock_struct *locks;
	NTSTATUS status = NT_STATUS_OK;

	dbuf = call->record_data;

	/* there are existing locks - find a match */
	locks = (struct lock_struct *)dbuf.dptr;
	count = dbuf.dsize / sizeof(*locks);

	show_locks("remove_pending", locks, count);

	for (i=0; i<count; i++) {
		struct lock_struct *lock = &locks[i];
		
		if (lock->lock_type >= PENDING_READ_LOCK &&
		    lock->notify_ptr == req->notify_ptr &&
		    cluster_id_equal(&lock->context.server, &req->server)) {
			call->new_data = talloc(call, TDB_DATA);
			if (call->new_data == NULL) {
				return CTDB_ERR_NOMEM;
			}

			call->new_data->dptr = talloc_size(call, dbuf.dsize - sizeof(lock));
			if (call->new_data->dptr == NULL) {
				return CTDB_ERR_NOMEM;
			}
			call->new_data->dsize = dbuf.dsize - sizeof(lock);

			memcpy(call->new_data->dptr, locks, i*sizeof(lock));
			memcpy(call->new_data->dptr+i*sizeof(lock), locks+i+1,
			       (count-(i+1))*sizeof(lock));
			break;
		}
	}
	
	if (call->new_data) {
		DEBUG(0,("remove_pending: size now %d\n", call->new_data->dsize));
	}

	if (i == count) {
		/* we didn't find it */
		status = NT_STATUS_RANGE_NOT_LOCKED;
	}

	call->reply_data = talloc(call, TDB_DATA);
	if (call->reply_data == NULL) {
		return CTDB_ERR_NOMEM;
	}

	call->reply_data->dptr = talloc_size(call, sizeof(NTSTATUS));
	call->reply_data->dsize = sizeof(NTSTATUS);
	if (call->reply_data->dptr == NULL) {
		return CTDB_ERR_NOMEM;
	}
	*(NTSTATUS *)call->reply_data->dptr = status;

	return 0;
}

static NTSTATUS brl_ctdb_remove_pending(struct brl_context *brl,
					struct brl_handle *brlh, 
					void *notify_ptr)
{
	TDB_DATA kbuf, rbuf, sbuf;
	struct ctdb_remove_pending_req req;
	NTSTATUS status;
	int ret;

	kbuf.dptr = brlh->key.data;
	kbuf.dsize = brlh->key.length;

	rbuf.dptr = (uint8_t *)&req;
	rbuf.dsize = sizeof(req);

	ZERO_STRUCT(req);
	req.notify_ptr = notify_ptr;
	req.server = brl->server;
		
	ret = ctdb_call(brl->ctdb, kbuf, FUNC_BRL_REMOVE_PENDING, &rbuf, &sbuf);
	if (ret == -1) {
		DEBUG(0,("ctdb_call failed - %s\n", __location__));
		return NT_STATUS_INTERNAL_DB_CORRUPTION;
	}

	status = *(NTSTATUS *)sbuf.dptr;
	talloc_free(sbuf.dptr);

	return status;
}


struct ctdb_locktest_req {
	uint16_t smbpid;
	uint64_t start;
	uint64_t size;
	enum brl_type lock_type;
	struct brl_context *brl;
	struct server_id server;
	struct ntvfs_handle *ntvfs;
};

/*
  remove a pending lock. This is called when the caller has either
  given up trying to establish a lock or when they have succeeded in
  getting it. In either case they no longer need to be notified.
*/
static int brl_ctdb_locktest_func(struct ctdb_call *call)
{
	struct ctdb_locktest_req *req = (struct ctdb_locktest_req *)call->call_data->dptr;
	TDB_DATA dbuf;
	int count, i;
	struct lock_struct *locks, lock;
	NTSTATUS status = NT_STATUS_OK;

	lock.context.smbpid = req->smbpid;
	lock.context.server = req->server;
	lock.context.ctx = req->brl;
	lock.ntvfs = req->ntvfs;
	lock.start = req->start;
	lock.size = req->size;
	lock.lock_type = req->lock_type;

	dbuf = call->record_data;

	/* there are existing locks - find a match */
	locks = (struct lock_struct *)dbuf.dptr;
	count = dbuf.dsize / sizeof(*locks);

	show_locks("locktest", locks, count);

	for (i=0; i<count; i++) {
		if (brl_ctdb_conflict_other(&locks[i], &lock)) {
			status = NT_STATUS_FILE_LOCK_CONFLICT;
			break;
		}
	}
	
	call->reply_data = talloc(call, TDB_DATA);
	if (call->reply_data == NULL) {
		return CTDB_ERR_NOMEM;
	}

	call->reply_data->dptr = talloc_size(call, sizeof(NTSTATUS));
	call->reply_data->dsize = sizeof(NTSTATUS);
	if (call->reply_data->dptr == NULL) {
		return CTDB_ERR_NOMEM;
	}
	*(NTSTATUS *)call->reply_data->dptr = status;

	return 0;
}

/*
  Test if we are allowed to perform IO on a region of an open file
*/
static NTSTATUS brl_ctdb_locktest(struct brl_context *brl,
				  struct brl_handle *brlh,
				  uint16_t smbpid, 
				  uint64_t start, uint64_t size, 
				  enum brl_type lock_type)
{
	TDB_DATA kbuf, rbuf, sbuf;
	struct ctdb_locktest_req req;
	NTSTATUS status;
	int ret;

	kbuf.dptr = brlh->key.data;
	kbuf.dsize = brlh->key.length;

	rbuf.dptr = (uint8_t *)&req;
	rbuf.dsize = sizeof(req);

	ZERO_STRUCT(req);
	req.smbpid = smbpid;
	req.start  = start;
	req.size   = size;
	req.lock_type = lock_type;
	req.server = brl->server;
	req.brl = brl;
	req.ntvfs = brlh->ntvfs;

	ret = ctdb_call(brl->ctdb, kbuf, FUNC_BRL_LOCKTEST, &rbuf, &sbuf);
	if (ret == -1) {
		DEBUG(0,("ctdb_call failed - %s\n", __location__));
		return NT_STATUS_INTERNAL_DB_CORRUPTION;
	}

	status = *(NTSTATUS *)sbuf.dptr;
	talloc_free(sbuf.dptr);

	return status;
}


struct ctdb_close_req {
	struct brl_context *brl;
	struct server_id server;
	struct ntvfs_handle *ntvfs;
};

/*
  remove a pending lock. This is called when the caller has either
  given up trying to establish a lock or when they have succeeded in
  getting it. In either case they no longer need to be notified.
*/
static int brl_ctdb_close_func(struct ctdb_call *call)
{
	struct ctdb_close_req *req = (struct ctdb_close_req *)call->call_data->dptr;
	TDB_DATA dbuf;
	int count, dcount=0, i;
	struct lock_struct *locks;
	NTSTATUS status = NT_STATUS_OK;

	dbuf = call->record_data;

	/* there are existing locks - find a match */
	locks = (struct lock_struct *)dbuf.dptr;
	count = dbuf.dsize / sizeof(*locks);

	show_locks("close", locks, count);

	DEBUG(0,("closing ctx=%p server=%d.%d ntvfs=%p\n",
		 req->brl, req->server.node, req->server.id, req->ntvfs));

	for (i=0; i<count; i++) {
		struct lock_struct *lock = &locks[i];

		if (lock->context.ctx == req->brl &&
		    cluster_id_equal(&lock->context.server, &req->server) &&
		    lock->ntvfs == req->ntvfs) {
			/* found it - delete it */
			if (count > 1 && i < count-1) {
				memmove(&locks[i], &locks[i+1], 
					sizeof(*locks)*((count-1) - i));
			}
			count--;
			i--;
			dcount++;
		}
	}

	if (dcount > 0) {
		call->new_data = talloc(call, TDB_DATA);
		if (call->new_data == NULL) {
			return CTDB_ERR_NOMEM;
		}
		
		call->new_data->dptr = talloc_size(call, count*sizeof(struct lock_struct));
		if (call->new_data->dptr == NULL) {
			return CTDB_ERR_NOMEM;
		}
		call->new_data->dsize = count*sizeof(struct lock_struct);

		memcpy(call->new_data->dptr, locks, count*sizeof(struct lock_struct));
	}

	if (call->new_data) {
		DEBUG(0,("close: size now %d\n", call->new_data->dsize));
	}

	DEBUG(0,("brl_ctdb_close_func dcount=%d count=%d\n", dcount, count));
	
	call->reply_data = talloc(call, TDB_DATA);
	if (call->reply_data == NULL) {
		return CTDB_ERR_NOMEM;
	}

	call->reply_data->dptr = talloc_size(call, sizeof(NTSTATUS));
	call->reply_data->dsize = sizeof(NTSTATUS);
	if (call->reply_data->dptr == NULL) {
		return CTDB_ERR_NOMEM;
	}
	*(NTSTATUS *)call->reply_data->dptr = status;

	return 0;
}

/*
  Test if we are allowed to perform IO on a region of an open file
*/
static NTSTATUS brl_ctdb_close(struct brl_context *brl,
			       struct brl_handle *brlh)
{
	TDB_DATA kbuf, rbuf, sbuf;
	struct ctdb_close_req req;
	NTSTATUS status;
	int ret;

	kbuf.dptr = brlh->key.data;
	kbuf.dsize = brlh->key.length;

	rbuf.dptr = (uint8_t *)&req;
	rbuf.dsize = sizeof(req);

	ZERO_STRUCT(req);
	req.brl = brl;
	req.server = brl->server;
	req.ntvfs = brlh->ntvfs;

	DEBUG(0,("brl_ctdb_close %u.%u %p\n", req.server.node, req.server.id, brl));

	ret = ctdb_call(brl->ctdb, kbuf, FUNC_BRL_CLOSE, &rbuf, &sbuf);
	if (ret == -1) {
		DEBUG(0,("ctdb_call failed - %s\n", __location__));
		return NT_STATUS_INTERNAL_DB_CORRUPTION;
	}

	status = *(NTSTATUS *)sbuf.dptr;
	talloc_free(sbuf.dptr);

	return status;
}


static const struct brlock_ops brlock_tdb_ops = {
	.brl_init           = brl_ctdb_init,
	.brl_create_handle  = brl_ctdb_create_handle,
	.brl_lock           = brl_ctdb_lock,
	.brl_unlock         = brl_ctdb_unlock,
	.brl_remove_pending = brl_ctdb_remove_pending,
	.brl_locktest       = brl_ctdb_locktest,
	.brl_close          = brl_ctdb_close
};


void brl_ctdb_init_ops(void)
{
	struct ctdb_context *ctdb = talloc_get_type(cluster_private(), struct ctdb_context);

	brl_set_ops(&brlock_tdb_ops);

	ctdb_set_call(ctdb, brl_ctdb_lock_func,  FUNC_BRL_LOCK);
	ctdb_set_call(ctdb, brl_ctdb_unlock_func,  FUNC_BRL_UNLOCK);
	ctdb_set_call(ctdb, brl_ctdb_remove_pending_func,  FUNC_BRL_REMOVE_PENDING);
	ctdb_set_call(ctdb, brl_ctdb_locktest_func,  FUNC_BRL_LOCKTEST);
	ctdb_set_call(ctdb, brl_ctdb_close_func,  FUNC_BRL_CLOSE);

}