summaryrefslogtreecommitdiffstats
path: root/dlm/dlm_load.c
blob: 3b967ef7abc621c4ff4bc4bc1d1d217a80db2d54 (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
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
/*
 * Copyright 2010-2011 David Teigland
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License V2
 * as published by the Free Software Foundation.
 */

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <time.h>
#include <signal.h>
#include <syslog.h>
#include <sys/time.h>
#include <asm/types.h>
#include <sys/socket.h>
#include <sys/poll.h>
#include <sys/un.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/errno.h>

#include "libdlm.h"

#define LKM_IVMODE -1

#define MAX_CLIENTS 4
#define DEFAULT_LOCKS 16
#define DEFAULT_RESOURCES 16

#define DEFAULT_SLEEP_ITER 10
#define DEFAULT_SLEEP_TIME 200000 /* usecs */

static dlm_lshandle_t *dh;
static int libdlm_fd;
static uint32_t iterations = 0;
static int maxn = DEFAULT_LOCKS;
static int maxr = DEFAULT_RESOURCES;
static int openclose = 0;
static int quiet = 0;
static int do_log_pre = 1;
static int exit_on_error = 1;
static int user_sh = 0;
static int sleep_iter = DEFAULT_SLEEP_ITER;
static int sleep_time = DEFAULT_SLEEP_TIME;
static int timewarn = 0;
static uint64_t our_xid = 0;
static uint32_t ast_count = 0;

struct client {
	int fd;
	char type[32];
};

static int client_size = MAX_CLIENTS;
static struct client client[MAX_CLIENTS];
static struct pollfd pollfd[MAX_CLIENTS];

enum {
	Op_request = 1,
	Op_convert,
	Op_unlock,
	Op_cancel,
};

struct lk {
	int num;
	int locked_stable;
	int unlocked_stable;
	int wait_request;
	int wait_convert;
	int wait_unlock;
	int wait_cancel;
	int rqmode;
	int grmode;
	int lastop;
	int last_status;
	int bast;
	int cancel_busy;
	time_t wait_start;
	struct dlm_lksb lksb;
};

static struct lk *locks;

#define log_debug(fmt, args...) \
do { \
	if (!quiet) \
		printf(fmt "\n", ##args); \
} while (0)

#define log_pre(fmt, args...) \
do { \
	if (!quiet && do_log_pre) \
		printf(fmt "\n", ##args); \
} while (0)

#define log_error(fmt, args...) \
do { \
	printf("ERROR " fmt "\n", ##args); \
	if (exit_on_error) \
		exit(-1); \
} while (0)

static int rand_int(int a, int b)
{
	return a + (int) (((float)(b - a + 1)) * random() / (RAND_MAX+1.0)); 
}

static const char *status_str(int status)
{
	static char sts_str[8];

	switch (status) {
	case 0:
		return "0      ";
	case EUNLOCK:
		return "U      ";
	case ECANCEL:
		return "ECANCEL";
	case EAGAIN:
		return "EAGAIN ";
	case EBUSY:
		return "EBUSY  ";
	case ETIMEDOUT:
		return "ETIMEDO";
	case EDEADLK:
		return "EDEADLK";
	default:
		snprintf(sts_str, 8, "%8x", status);
		return sts_str;
	}
}

static const char *op_str(int op)
{
	switch (op) {
	case Op_request:
		return "request";
	case Op_convert:
		return "convert";
	case Op_unlock:
		return "unlock ";
	case Op_cancel:
		return "cancel ";
	default:
		return "unknown";
	}
}

static const char *num_str(int num)
{
	static char num_str[8];

	if (num == -1)
		return "-";

	snprintf(num_str, 8, "%d", num);
	return num_str;
}

static struct lk *get_lock(int i)
{
	if (i < 0)
		return NULL;
	if (i >= maxn)
		return NULL;
	return &locks[i];
}

static void dump_lk(int num)
{
	struct lk *lk = get_lock(num);

	if (!lk)
		return;

	printf("lk %03u id %08x gr %s rq %s op %s ",
	       num, lk->lksb.sb_lkid, num_str(lk->grmode), num_str(lk->rqmode),
	       op_str(lk->lastop));

	if (lk->wait_request || lk->wait_convert ||
	    lk->wait_unlock || lk->wait_cancel) {
		printf("wait r%d c%d u%d c%d\n",
			lk->wait_request,
			lk->wait_convert,
			lk->wait_unlock,
			lk->wait_cancel);
	} else {
		printf("done\n");
	}

	fflush(stdout);
}

static void dump(void)
{
	int i;

	for (i = 0; i < maxn; i++)
		dump_lk(i);
}

static void bastfn(void *arg)
{
	struct lk *lk = arg;
	lk->bast = 1;

	ast_count++;

	printf("lk %03u bast\n", lk->num);
	fflush(stdout);
}

static void astfn(void *arg)
{
	struct lk *lk = arg;
	int status = lk->lksb.sb_status;
	int num = lk->num;

	ast_count++;

	switch (status) {
	case 0:
	case EAGAIN:
		if (lk->wait_request || lk->wait_convert) {
			if (lk->wait_request && (status == EAGAIN)) {
				lk->locked_stable = 0;
				lk->unlocked_stable = 1;
			} else {
				lk->locked_stable = 1;
				lk->unlocked_stable = 0;
			}

			lk->wait_request = 0;
			lk->wait_convert = 0;

			if (!status) {
				lk->grmode = lk->rqmode;
				lk->rqmode = LKM_IVMODE;
			} else {
				lk->rqmode = LKM_IVMODE;
			}
		} else {
			/* wait state error */
			log_error("lk %03u cast %04u %s %08x "
				  "wait r%d c%d u%d c%d "
		  		  "gr %2d rq %2d last %s wait state error 1",
				  num, ast_count, status_str(status),
				  lk->lksb.sb_lkid,
				  lk->wait_request, lk->wait_convert,
				  lk->wait_unlock, lk->wait_cancel,
				  lk->grmode, lk->rqmode,
				  op_str(lk->lastop));
		}

		if (lk->wait_cancel) {
			lk->wait_cancel = 0;
		}

		if (lk->wait_unlock) {
			/* wait state error */
			log_error("lk %03u cast %04u %s %08x "
				  "wait r%d c%d u%d c%d "
		  		  "gr %2d rq %2d last %s wait state error 2",
				  num, ast_count, status_str(status),
				  lk->lksb.sb_lkid,
				  lk->wait_request, lk->wait_convert,
				  lk->wait_unlock, lk->wait_cancel,
				  lk->grmode, lk->rqmode,
				  op_str(lk->lastop));
		}
		break;

	case EUNLOCK:
		if (lk->wait_unlock) {
			lk->locked_stable = 0;
			lk->unlocked_stable = 1;

			lk->wait_unlock = 0;

			lk->grmode = LKM_IVMODE;
			lk->bast = 0;
		} else {
			/* wait state error */
			log_error("lk %03u cast %04u %s %08x "
				  "wait r%d c%d u%d c%d "
		  		  "gr %2d rq %2d last %s wait state error 3",
				  num, ast_count, status_str(status),
				  lk->lksb.sb_lkid,
				  lk->wait_request, lk->wait_convert,
				  lk->wait_unlock, lk->wait_cancel,
				  lk->grmode, lk->rqmode,
				  op_str(lk->lastop));
		}

		if (lk->wait_request || lk->wait_convert || lk->wait_cancel) {
			/* wait state error */
			log_error("lk %03u cast %04u %s %08x "
				  "wait r%d c%d u%d c%d "
		  		  "gr %2d rq %2d last %s wait state error 4",
				  num, ast_count, status_str(status),
				  lk->lksb.sb_lkid,
				  lk->wait_request, lk->wait_convert,
				  lk->wait_unlock, lk->wait_cancel,
				  lk->grmode, lk->rqmode,
				  op_str(lk->lastop));
		}
		break;

	case ECANCEL:
		if (lk->wait_cancel) {
			if (lk->grmode > LKM_IVMODE) {
				lk->locked_stable = 1;
				lk->unlocked_stable = 0;
			} else {
				lk->locked_stable = 0;
				lk->unlocked_stable = 1;
			}

			lk->wait_cancel = 0;

			lk->rqmode = LKM_IVMODE;
		} else {
			/* wait state error */
			log_error("lk %03u cast %04u %s %08x "
				  "wait r%d c%d u%d c%d "
		  		  "gr %2d rq %2d last %s wait state error 5",
				  num, ast_count, status_str(status),
				  lk->lksb.sb_lkid,
				  lk->wait_request, lk->wait_convert,
				  lk->wait_unlock, lk->wait_cancel,
				  lk->grmode, lk->rqmode,
				  op_str(lk->lastop));
		}

		if (lk->wait_request || lk->wait_convert) {
			lk->wait_request = 0;
			lk->wait_convert = 0;
		}

		if (lk->wait_unlock) {
			/* wait state error */
			log_error("lk %03u cast %04u %s %08x "
				  "wait r%d c%d u%d c%d "
		  		  "gr %2d rq %2d last %s wait state error 6",
				  num, ast_count, status_str(status),
				  lk->lksb.sb_lkid,
				  lk->wait_request, lk->wait_convert,
				  lk->wait_unlock, lk->wait_cancel,
				  lk->grmode, lk->rqmode,
				  op_str(lk->lastop));
		}
		break;

	case ETIMEDOUT:
		log_error("lk %03u cast %04u %s %08x not using timeouts",
			  num, ast_count, status_str(status),
			  lk->lksb.sb_lkid);
		break;

	case EDEADLK:
		if (lk->wait_convert) {
			lk->locked_stable = 1;
			lk->unlocked_stable = 0;

			lk->wait_convert = 0;

			lk->rqmode = LKM_IVMODE;
		} else {
			/* wait state error */
			log_error("lk %03u cast %04u %s %08x "
				  "wait r%d c%d u%d c%d "
		  		  "gr %2d rq %2d last %s wait state error 7",
				  num, ast_count, status_str(status),
				  lk->lksb.sb_lkid,
				  lk->wait_request, lk->wait_convert,
				  lk->wait_unlock, lk->wait_cancel,
				  lk->grmode, lk->rqmode,
				  op_str(lk->lastop));
		}

		if (lk->wait_request || lk->wait_unlock || lk->wait_cancel) {
			/* wait state error */
			log_error("lk %03u cast %04u %s %08x "
				  "wait r%d c%d u%d c%d "
		  		  "gr %2d rq %2d last %s wait state error 8",
				  num, ast_count, status_str(status),
				  lk->lksb.sb_lkid,
				  lk->wait_request, lk->wait_convert,
				  lk->wait_unlock, lk->wait_cancel,
				  lk->grmode, lk->rqmode,
				  op_str(lk->lastop));
		}

		break;

	default:
		log_error("lk %03u cast %04u %d %08x error unexpected status",
			  num, ast_count, status, lk->lksb.sb_lkid);
	};

	printf("lk %03u id %08x gr %s rq %s op %s cast %s\n",
	       num, lk->lksb.sb_lkid, num_str(lk->grmode), num_str(lk->rqmode),
	       op_str(lk->lastop), status_str(status));
	fflush(stdout);

}

/* FIXME: ran two dlm_load's on four nodes over the weekend, and one instance
   of dlm_load hit:
   lk 007 unlock
   lk 007 cast 3000665 EUNLOCK 022c0912 wait r0 c0 u1 c0 gr  2 rq -1 last unlock
   lk 007 request mode 2 noqueue 1
   lk 007 cast 3000705 EAGAIN  00680909 wait r1 c0 u0 c0 gr -1 rq  2 last request
   lk 007 request mode 0 noqueue 1
   lk 007 cast 3000732 EAGAIN  017b090e wait r1 c0 u0 c0 gr -1 rq  0 last request
   lk 007 request mode 6 noqueue 0
   ERROR lk 007 request errno 22 wait r0 c0 u0 c0 lastop request
*/

static int do_request(int num, int mode, int noqueue)
{
	struct lk *lk;
	char name[DLM_RESNAME_MAXLEN];
	uint64_t *timeout_arg = NULL;
	uint32_t flags = 0;
	int rv;

	lk = get_lock(num);
	if (!lk) {
		printf("unknown lk %d\n", num);
		return;
	}

	if (noqueue)
		flags |= LKF_NOQUEUE;

	memset(name, 0, sizeof(name));
	snprintf(name, sizeof(name), "test%d", (num % maxr));

	log_pre("lk %03u request mode %d noqueue %d", num, mode, noqueue);

	rv = dlm_ls_lockx(dh, mode, &lk->lksb, flags, name, strlen(name), 0,
			  astfn, (void *)lk, bastfn, &our_xid, timeout_arg);

	if (rv) {
		log_error("lk %03u request errno %d mode %d noqueue %d",
			  num, errno, mode, noqueue);
		dump_lk(num);
	} else {
		lk->lastop = Op_request;
		lk->rqmode = mode;
		lk->wait_start = time(NULL);
		lk->wait_request = 1;
		lk->locked_stable = 0;
		lk->unlocked_stable = 0;
	}
	return rv;
}

static int do_convert(int num, int mode, int noqueue, int quecvt)
{
	struct lk *lk;
	char name[DLM_RESNAME_MAXLEN];
	uint64_t *timeout_arg = NULL;
	uint32_t flags = LKF_CONVERT;
	int rv;

	lk = get_lock(num);
	if (!lk) {
		printf("unknown lk %d\n", num);
		return;
	}

	if (noqueue)
		flags |= LKF_NOQUEUE;
	if (quecvt)
		flags |= LKF_QUECVT;

	memset(name, 0, sizeof(name));
	snprintf(name, sizeof(name), "test%d", (num % maxr));

	log_pre("lk %03u convert mode %d %x noqueue %d quecvt %d",
		num, mode, flags, noqueue, quecvt);

	rv = dlm_ls_lockx(dh, mode, &lk->lksb, flags, name, strlen(name), 0,
			  astfn, (void *)lk, bastfn, &our_xid, timeout_arg);

	/* Unfortunately we don't have a way to know when the dlm will be
	   done dealing with a cancel and won't return -EBUSY for ops.
	   (We don't get a callback for the cancel when got a callback for
	   the original op and the cancel did nothing.) */

	if (rv == -1 && errno == EBUSY && lk->lastop == Op_cancel) {
		lk->cancel_busy++;
		log_debug("lk %03u convert cancel_busy %d", lk->cancel_busy);
		goto out;
	} else {
		lk->cancel_busy = 0;
	}

	if (rv) {
		log_error("lk %03u convert errno %d mode %d noqueue %d",
			  num, errno, mode, noqueue);
		dump_lk(num);
	} else {
		lk->lastop = Op_convert;
		lk->rqmode = mode;
		lk->bast = 0;
		lk->wait_start = time(NULL);
		lk->wait_convert = 1;
		lk->locked_stable = 0;
		lk->unlocked_stable = 0;
	}
 out:
	return rv;
}

static int do_unlock(int num)
{
	struct lk *lk;
	uint32_t flags = 0;
	int rv;

	lk = get_lock(num);
	if (!lk) {
		printf("unknown lk %d\n", num);
		return;
	}

	log_pre("lk %03u unlock", num);

	rv = dlm_ls_unlock(dh, lk->lksb.sb_lkid, flags, &lk->lksb, lk);

	/* See comment above. */

	if (rv == -1 && errno == EBUSY && lk->lastop == Op_cancel) {
		lk->cancel_busy++;
		log_debug("lk %03u unlock cancel_busy %d", lk->cancel_busy);
		goto out;
	} else {
		lk->cancel_busy = 0;
	}

	if (rv) {
		log_error("lk %03u unlock errno %d", num, errno);
		dump_lk(num);
	} else {
		lk->lastop = Op_unlock;
		lk->rqmode = LKM_IVMODE;
		lk->bast = 0;
		lk->wait_unlock = 1;
		lk->locked_stable = 0;
		lk->unlocked_stable = 0;
	}
 out:
	return rv;
}

/* The op following a cancel can return -EBUSY.  I don't think there is any
   way to know in this case when we can do the next op without getting
   EBUSY back.  See lk->cancel_busy.  Without the cancel_busy check we get
   this error:
   lk 025 request mode 4 noqueue 0
   lk 025 busy locked 0 unlocked 0 wait r1 c0 u0 c0
   lk 025 cancel
   lk 025 busy locked 0 unlocked 0 wait r1 c0 u0 c1
   lk 025 cast 0706 0       01060001 wait r1 c0 u0 c1 gr -1 rq  4 last cancel
   lk 025 bast 0707
   lk 025 convert mode 0 noqueue 0
   ERROR lk 025 convert errno 16 wait r0 c0 u0 c0 lastop cancel
*/

static int do_cancel(int num)
{
	struct lk *lk;
	uint32_t flags = LKF_CANCEL;
	int rv;

	lk = get_lock(num);
	if (!lk) {
		printf("unknown lk %d\n", num);
		return;
	}

	log_pre("lk %03u cancel", num);

	rv = dlm_ls_unlock(dh, lk->lksb.sb_lkid, flags, &lk->lksb, lk);

	if (rv) {
		log_error("lk %03u cancel errno %d", num, errno);
		dump_lk(num);
	} else {
		lk->lastop = Op_cancel;
		lk->wait_cancel = 1;
		lk->locked_stable = 0;
		lk->unlocked_stable = 0;
	}
	return rv;
}

static void rand_loop(int iter)
{
	struct lk *lk;
	uint32_t n = 0;
	int num, noqueue, quecvt, rqmode, rv;

	while (1) {
		dlm_dispatch(libdlm_fd);

		n++;
		if (iter && n == iter+1)
			break;

		if (sleep_iter && !(n % sleep_iter))
			usleep(sleep_time);

		num = rand_int(0, maxn - 1);
		noqueue = rand_int(0, 1);
		rqmode = rand_int(0, LKM_EXMODE);
		quecvt = 0;

		lk = get_lock(num);
		if (!lk)
			continue;

		if (lk->unlocked_stable) {
			rv = do_request(num, rqmode, noqueue);
			continue;
		}

		if (lk->locked_stable && lk->bast) {
			if (rand_int(0, 1)) {
				rv = do_convert(num, LKM_NLMODE, 0, 0);
			} else {
				rv = do_unlock(num);
			}
			continue;
		}

		if (lk->locked_stable) {
			if (rqmode > lk->grmode)
				quecvt = rand_int(0, 1);

			if (rand_int(0, 1)) {
				rv = do_convert(num, rqmode, noqueue, quecvt);
			} else {
				rv = do_unlock(num);
			}
			continue;
		}

		if ((lk->wait_request || lk->wait_convert) && !lk->wait_cancel) {
			if (time(NULL) - lk->wait_start > 2) {
				rv = do_cancel(num);
			}
		}

		log_debug("lk %03u id %08x gr %s rq %s op %s skip "
			  "ls %d us %d wait r%d c%d u%d c%d",
			  num,
			  lk->lksb.sb_lkid,
			  num_str(lk->grmode),
			  num_str(lk->rqmode),
			  op_str(lk->lastop),
			  lk->locked_stable,
			  lk->unlocked_stable,
			  lk->wait_request,
			  lk->wait_convert,
			  lk->wait_unlock,
			  lk->wait_cancel);
	}
}

static int client_add(int fd, int *maxi)
{
	int i;

	for (i = 0; i < client_size; i++) {
		if (client[i].fd == -1) {
			client[i].fd = fd;
			pollfd[i].fd = fd;
			pollfd[i].events = POLLIN;
			if (i > *maxi)
				*maxi = i;
			log_debug("client %d fd %d added", i, fd);
			return i;
		}
	}
	log_error("client add failed");
	return -1;
}

static void client_dead(int ci)
{
	log_debug("client %d fd %d dead", ci, client[ci].fd);
	close(client[ci].fd);
	client[ci].fd = -1;
	pollfd[ci].fd = -1;
}

static void client_init(void)
{
	int i;

	for (i = 0; i < client_size; i++)
		client[i].fd = -1;
}

static void print_help(void)
{
	printf("quit\n");
	printf("help\n");
	printf("rand num - run num random ops on random lks\n");
	printf("ls [lk]  - list one or all locks\n");
	printf("lock    lk mode noqueue\n");
	printf("convert lk mode noqueue\n");
	printf("quecvt  lk mode\n");
	printf("unlock  lk\n");
	printf("cancel  lk\n");
}

static void user_cmd(int *quit)
{
	char inbuf[132];
	char cmd[132];
	int rv, num = 0, x = 0, y = 0;

	memset(inbuf, 0, sizeof(inbuf));
	memset(cmd, 0, sizeof(cmd));

	fgets(inbuf, sizeof(inbuf), stdin);
	rv = sscanf(inbuf, "%s %d %d %d", cmd, &num, &x, &y);

	if (!rv || !strcmp(cmd, "help")) {
		print_help();
		return;
	}

	if (!strcmp(cmd, "quit")) {
		*quit = 1;
		return;
	}

	if (!strcmp(cmd, "ls") && rv == 1) {
		dump();
		return;
	}

	else if (!strcmp(cmd, "lock")) {
		do_request(num, x, y);
	}

	else if (!strcmp(cmd, "convert")) {
		do_convert(num, x, y, 0);
	}

	else if (!strcmp(cmd, "quecvt")) {
		do_convert(num, x, y, 1);
	}

	else if (!strcmp(cmd, "unlock")) {
		do_unlock(num);
	}
	
	else if (!strcmp(cmd, "cancel")) {
		do_cancel(num);
	}

	else if (!strcmp(cmd, "rand")) {
		rand_loop(num);
	}

	else if (!strcmp(cmd, "ls")) {
		dump_lk(num);
	}

	else if (strlen(cmd) > 0) {
		printf("unknown cmd %s\n", cmd);
		print_help();
	}

	else {
		print_help();
	}
}

static void user_loop(void)
{
	int quit = 0;
	int maxi = 0;
	int rv, i;

	exit_on_error = 0;
	do_log_pre = 0;

	client_add(libdlm_fd, &maxi);
	client_add(STDIN_FILENO, &maxi);

	printf("Type exit to quit, help for usage\n");

	while (!quit) {
		rv = poll(pollfd, maxi + 1, -1);
		if (rv < 0 && errno == EINTR)
			continue;

		for (i = 0; i <= maxi; i++) {
			if (client[i].fd < 0)
				continue;

			if (pollfd[i].revents & POLLIN) {
				if (pollfd[i].fd == libdlm_fd)
					dlm_dispatch(libdlm_fd);
				else if (pollfd[i].fd == STDIN_FILENO) {
					user_cmd(&quit);
				}
			}

			if (pollfd[i].revents & (POLLHUP | POLLERR | POLLNVAL))
				client_dead(i);
		}
	}
}

static void print_usage(void)
{
	printf("Options:\n");
	printf("\n");
	printf("  -n <num>     The number of locks to work with, default %d\n", DEFAULT_LOCKS);
	printf("  -r <num>     The number of resources to work with, default %d\n", DEFAULT_RESOURCES);
	printf("  -i <num>     Iterations in looping stress test, default 0 is no limit\n");
	printf("  -S <num>     sleep every <num> iterations, default %d\n", DEFAULT_SLEEP_ITER);
	printf("  -s <num>     sleep <num> usecs, default %d\n", DEFAULT_SLEEP_TIME);
	printf("  -o           Open/close existing lockspace\n");
	printf("  -q           Quiet output, only print errors\n");
	printf("  -u           User command input\n");
}

static void decode_arguments(int argc, char **argv)
{
	int cont = 1;
	int optchar;

	while (cont) {
		optchar = getopt(argc, argv, "n:r:i:s:S:uqoh");

		switch (optchar) {

		case 'n':
			maxn = atoi(optarg);
			break;

		case 'r':
			maxr = atoi(optarg);
			break;

		case 'i':
			iterations = atoi(optarg);
			break;

		case 'o':
			openclose = 1;
			break;

		case 'q':
			quiet = 1;
			break;

		case 's':
			sleep_time = atoi(optarg);
			break;

		case 'S':
			sleep_iter = atoi(optarg);
			break;

		case 'u':
			user_sh = 1;
			break;

		case 'h':
			print_usage();
			exit(EXIT_SUCCESS);
			break;

		case 'V':
			printf("%s (built %s %s)\n", argv[0], __DATE__, __TIME__);
			exit(EXIT_SUCCESS);
			break;

		case ':':
		case '?':
			fprintf(stderr, "Please use '-h' for usage.\n");
			exit(EXIT_FAILURE);
			break;

		case EOF:
			cont = 0;
			break;

		default:
			fprintf(stderr, "unknown option: %c\n", optchar);
			exit(EXIT_FAILURE);
			break;
		};
	}
}

int main(int argc, char *argv[])
{
	uint32_t major, minor, patch;
	struct lk *lk;
	int i, rv, quit = 0;

	srandom(time(NULL));

	decode_arguments(argc, argv);

	if (maxn < maxr) {
		log_error("number of resources must be >= number of locks");
		return -1;
	}
	if (maxn % maxr) {
		log_error("number of locks must be multiple of number of resources");
		return -1;
	}

	log_debug("maxn = %d", maxn);
	log_debug("maxr = %d", maxr);
	log_debug("locks per resource = %d", maxn / maxr);

	client_init();

	locks = malloc(maxn * sizeof(struct lk));
	if (!locks) {
		log_error("no mem for %d locks", maxn);
		return 0;
	}
	memset(locks, 0, sizeof(*locks));

	lk = locks;
	for (i = 0; i < maxn; i++) {
		lk->num = i;
		lk->grmode = -1;
		lk->rqmode = -1;
		lk->unlocked_stable = 1;
		lk++;
	}

	rv = dlm_kernel_version(&major, &minor, &patch);
	if (rv < 0) {
		log_error("can't detect dlm in kernel %d", errno);
		return -1;
	}
	log_debug("dlm kernel version: %u.%u.%u", major, minor, patch);
	dlm_library_version(&major, &minor, &patch);
	log_debug("dlm library version: %u.%u.%u", major, minor, patch);

	if (openclose) {
		log_debug("dlm_open_lockspace...");

		dh = dlm_open_lockspace("test");
		if (!dh) {
			log_error("dlm_open_lockspace error %lu %d",
				  (unsigned long)dh, errno);
			return -ENOTCONN;
		}
	} else {
		log_debug("dlm_new_lockspace...");

		dh = dlm_new_lockspace("test", 0600,
				       timewarn ? DLM_LSFL_TIMEWARN : 0);
		if (!dh) {
			log_error("dlm_new_lockspace error %lu %d",
				  (unsigned long)dh, errno);
			return -ENOTCONN;
		}
	}

	rv = dlm_ls_get_fd(dh);
	if (rv < 0) {
		log_error("dlm_ls_get_fd error %d %d", rv, errno);
		dlm_release_lockspace("test", dh, 1);
		return rv;
	}
	libdlm_fd = rv;

	if (user_sh)
		user_loop();
	else
		rand_loop(iterations);

	if (openclose) {
		log_debug("dlm_close_lockspace");

		rv = dlm_close_lockspace(dh);
		if (rv < 0)
			log_error("dlm_close_lockspace error %d %d",
				  rv, errno);
	} else {
		log_debug("dlm_release_lockspace");

		rv = dlm_release_lockspace("test", dh, 1);
		if (rv < 0)
			log_error("dlm_release_lockspace error %d %d",
				  rv, errno);
	}

	return 0;
}