1/*
2   Unix SMB/CIFS implementation.
3   byte range lock tester - with local filesystem support
4   Copyright (C) Andrew Tridgell 1999
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2 of the License, or
9   (at your option) any later version.
10
11   This program is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with this program; if not, write to the Free Software
18   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19*/
20
21#define NO_SYSLOG
22
23#include "includes.h"
24
25static fstring password;
26static fstring username;
27static int got_pass;
28static int numops = 1000;
29static BOOL showall;
30static BOOL analyze;
31static BOOL hide_unlock_fails;
32static BOOL use_oplocks;
33
34#define FILENAME "\\locktest.dat"
35#define LOCKRANGE 100
36#define LOCKBASE 0
37
38/*
39#define LOCKBASE (0x40000000 - 50)
40*/
41
42#define READ_PCT 50
43#define LOCK_PCT 25
44#define UNLOCK_PCT 65
45#define RANGE_MULTIPLE 1
46
47#define NSERVERS 2
48#define NCONNECTIONS 2
49#define NUMFSTYPES 2
50#define NFILES 2
51#define LOCK_TIMEOUT 0
52
53#define FSTYPE_SMB 0
54#define FSTYPE_NFS 1
55
56struct record {
57	char r1, r2;
58	char conn, f, fstype;
59	unsigned start, len;
60	char needed;
61};
62
63static struct record *recorded;
64
65static int try_open(struct cli_state *c, char *nfs, int fstype, const char *fname, int flags)
66{
67	pstring path;
68
69	switch (fstype) {
70	case FSTYPE_SMB:
71		return cli_open(c, fname, flags, DENY_NONE);
72
73	case FSTYPE_NFS:
74		slprintf(path, sizeof(path), "%s%s", nfs, fname);
75		pstring_sub(path,"\\", "/");
76		return open(path, flags, 0666);
77	}
78
79	return -1;
80}
81
82static BOOL try_close(struct cli_state *c, int fstype, int fd)
83{
84	switch (fstype) {
85	case FSTYPE_SMB:
86		return cli_close(c, fd);
87
88	case FSTYPE_NFS:
89		return close(fd) == 0;
90	}
91
92	return False;
93}
94
95static BOOL try_lock(struct cli_state *c, int fstype,
96		     int fd, unsigned start, unsigned len,
97		     enum brl_type op)
98{
99	struct flock lock;
100
101	switch (fstype) {
102	case FSTYPE_SMB:
103		return cli_lock(c, fd, start, len, LOCK_TIMEOUT, op);
104
105	case FSTYPE_NFS:
106		lock.l_type = (op==READ_LOCK) ? F_RDLCK:F_WRLCK;
107		lock.l_whence = SEEK_SET;
108		lock.l_start = start;
109		lock.l_len = len;
110		lock.l_pid = getpid();
111		return fcntl(fd,F_SETLK,&lock) == 0;
112	}
113
114	return False;
115}
116
117static BOOL try_unlock(struct cli_state *c, int fstype,
118		       int fd, unsigned start, unsigned len)
119{
120	struct flock lock;
121
122	switch (fstype) {
123	case FSTYPE_SMB:
124		return cli_unlock(c, fd, start, len);
125
126	case FSTYPE_NFS:
127		lock.l_type = F_UNLCK;
128		lock.l_whence = SEEK_SET;
129		lock.l_start = start;
130		lock.l_len = len;
131		lock.l_pid = getpid();
132		return fcntl(fd,F_SETLK,&lock) == 0;
133	}
134
135	return False;
136}
137
138static void print_brl(SMB_DEV_T dev, SMB_INO_T ino, int pid,
139		      enum brl_type lock_type,
140		      br_off start, br_off size)
141{
142	printf("%6d   %05x:%05x    %s  %.0f:%.0f(%.0f)\n",
143	       (int)pid, (int)dev, (int)ino,
144	       lock_type==READ_LOCK?"R":"W",
145	       (double)start, (double)start+size-1,(double)size);
146
147}
148
149/*****************************************************
150return a connection to a server
151*******************************************************/
152static struct cli_state *connect_one(char *share)
153{
154	struct cli_state *c;
155	char *server_n;
156	fstring server;
157	fstring myname;
158	static int count;
159	NTSTATUS nt_status;
160
161	fstrcpy(server,share+2);
162	share = strchr_m(server,'\\');
163	if (!share) return NULL;
164	*share = 0;
165	share++;
166
167	server_n = server;
168
169	if (!got_pass) {
170		char *pass = getpass("Password: ");
171		if (pass) {
172			fstrcpy(password, pass);
173		}
174	}
175
176	slprintf(myname,sizeof(myname), "lock-%lu-%u", (unsigned long)getpid(), count++);
177
178	nt_status = cli_full_connection(&c, myname, server_n, NULL, 0, share, "?????",
179					username, lp_workgroup(), password, 0,
180					Undefined, NULL);
181
182	if (!NT_STATUS_IS_OK(nt_status)) {
183		DEBUG(0, ("cli_full_connection failed with error %s\n", nt_errstr(nt_status)));
184		return NULL;
185	}
186
187	c->use_oplocks = use_oplocks;
188
189	return c;
190}
191
192
193static void reconnect(struct cli_state *cli[NSERVERS][NCONNECTIONS],
194		      char *nfs[NSERVERS],
195		      int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
196		      char *share1, char *share2)
197{
198	int server, conn, f, fstype;
199	char *share[2];
200	share[0] = share1;
201	share[1] = share2;
202
203	fstype = FSTYPE_SMB;
204
205	for (server=0;server<NSERVERS;server++)
206	for (conn=0;conn<NCONNECTIONS;conn++) {
207		if (cli[server][conn]) {
208			for (f=0;f<NFILES;f++) {
209				cli_close(cli[server][conn], fnum[server][fstype][conn][f]);
210			}
211			cli_ulogoff(cli[server][conn]);
212			cli_shutdown(cli[server][conn]);
213		}
214		cli[server][conn] = connect_one(share[server]);
215		if (!cli[server][conn]) {
216			DEBUG(0,("Failed to connect to %s\n", share[server]));
217			exit(1);
218		}
219	}
220}
221
222
223
224static BOOL test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS],
225		     char *nfs[NSERVERS],
226		     int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
227		     struct record *rec)
228{
229	unsigned conn = rec->conn;
230	unsigned f = rec->f;
231	unsigned fstype = rec->fstype;
232	unsigned start = rec->start;
233	unsigned len = rec->len;
234	unsigned r1 = rec->r1;
235	unsigned r2 = rec->r2;
236	enum brl_type op;
237	int server;
238	BOOL ret[NSERVERS];
239
240	if (r1 < READ_PCT) {
241		op = READ_LOCK;
242	} else {
243		op = WRITE_LOCK;
244	}
245
246	if (r2 < LOCK_PCT) {
247		/* set a lock */
248		for (server=0;server<NSERVERS;server++) {
249			ret[server] = try_lock(cli[server][conn], fstype,
250					       fnum[server][fstype][conn][f],
251					       start, len, op);
252		}
253		if (showall || ret[0] != ret[1]) {
254			printf("lock   conn=%u fstype=%u f=%u range=%u:%u(%u) op=%s -> %u:%u\n",
255			       conn, fstype, f,
256			       start, start+len-1, len,
257			       op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
258			       ret[0], ret[1]);
259		}
260		if (showall) brl_forall(print_brl);
261		if (ret[0] != ret[1]) return False;
262	} else if (r2 < LOCK_PCT+UNLOCK_PCT) {
263		/* unset a lock */
264		for (server=0;server<NSERVERS;server++) {
265			ret[server] = try_unlock(cli[server][conn], fstype,
266						 fnum[server][fstype][conn][f],
267						 start, len);
268		}
269		if (showall || (!hide_unlock_fails && (ret[0] != ret[1]))) {
270			printf("unlock conn=%u fstype=%u f=%u range=%u:%u(%u)       -> %u:%u\n",
271			       conn, fstype, f,
272			       start, start+len-1, len,
273			       ret[0], ret[1]);
274		}
275		if (showall) brl_forall(print_brl);
276		if (!hide_unlock_fails && ret[0] != ret[1]) return False;
277	} else {
278		/* reopen the file */
279		for (server=0;server<NSERVERS;server++) {
280			try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
281			fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
282								 O_RDWR|O_CREAT);
283			if (fnum[server][fstype][conn][f] == -1) {
284				printf("failed to reopen on share1\n");
285				return False;
286			}
287		}
288		if (showall) {
289			printf("reopen conn=%u fstype=%u f=%u\n",
290			       conn, fstype, f);
291			brl_forall(print_brl);
292		}
293	}
294	return True;
295}
296
297static void close_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
298			char *nfs[NSERVERS],
299			int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
300{
301	int server, conn, f, fstype;
302
303	for (server=0;server<NSERVERS;server++)
304	for (fstype=0;fstype<NUMFSTYPES;fstype++)
305	for (conn=0;conn<NCONNECTIONS;conn++)
306	for (f=0;f<NFILES;f++) {
307		if (fnum[server][fstype][conn][f] != -1) {
308			try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
309			fnum[server][fstype][conn][f] = -1;
310		}
311	}
312	for (server=0;server<NSERVERS;server++) {
313		cli_unlink(cli[server][0], FILENAME);
314	}
315}
316
317static void open_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
318		       char *nfs[NSERVERS],
319		       int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
320{
321	int server, fstype, conn, f;
322
323	for (server=0;server<NSERVERS;server++)
324	for (fstype=0;fstype<NUMFSTYPES;fstype++)
325	for (conn=0;conn<NCONNECTIONS;conn++)
326	for (f=0;f<NFILES;f++) {
327		fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
328							 O_RDWR|O_CREAT);
329		if (fnum[server][fstype][conn][f] == -1) {
330			fprintf(stderr,"Failed to open fnum[%u][%u][%u][%u]\n",
331				server, fstype, conn, f);
332			exit(1);
333		}
334	}
335}
336
337
338static int retest(struct cli_state *cli[NSERVERS][NCONNECTIONS],
339		  char *nfs[NSERVERS],
340		  int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
341		  int n)
342{
343	int i;
344	printf("testing %u ...\n", n);
345	for (i=0; i<n; i++) {
346		if (i && i % 100 == 0) {
347			printf("%u\n", i);
348		}
349
350		if (recorded[i].needed &&
351		    !test_one(cli, nfs, fnum, &recorded[i])) return i;
352	}
353	return n;
354}
355
356
357/* each server has two connections open to it. Each connection has two file
358   descriptors open on the file - 8 file descriptors in total
359
360   we then do random locking ops in tamdem on the 4 fnums from each
361   server and ensure that the results match
362 */
363static void test_locks(char *share1, char *share2, char *nfspath1, char *nfspath2)
364{
365	struct cli_state *cli[NSERVERS][NCONNECTIONS];
366	char *nfs[NSERVERS];
367	int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES];
368	int n, i, n1;
369
370	nfs[0] = nfspath1;
371	nfs[1] = nfspath2;
372
373	ZERO_STRUCT(fnum);
374	ZERO_STRUCT(cli);
375
376	recorded = (struct record *)malloc(sizeof(*recorded) * numops);
377
378	for (n=0; n<numops; n++) {
379		recorded[n].conn = random() % NCONNECTIONS;
380		recorded[n].fstype = random() % NUMFSTYPES;
381		recorded[n].f = random() % NFILES;
382		recorded[n].start = LOCKBASE + ((unsigned)random() % (LOCKRANGE-1));
383		recorded[n].len = 1 +
384			random() % (LOCKRANGE-(recorded[n].start-LOCKBASE));
385		recorded[n].start *= RANGE_MULTIPLE;
386		recorded[n].len *= RANGE_MULTIPLE;
387		recorded[n].r1 = random() % 100;
388		recorded[n].r2 = random() % 100;
389		recorded[n].needed = True;
390	}
391
392	reconnect(cli, nfs, fnum, share1, share2);
393	open_files(cli, nfs, fnum);
394	n = retest(cli, nfs, fnum, numops);
395
396	if (n == numops || !analyze) return;
397	n++;
398
399	while (1) {
400		n1 = n;
401
402		close_files(cli, nfs, fnum);
403		reconnect(cli, nfs, fnum, share1, share2);
404		open_files(cli, nfs, fnum);
405
406		for (i=0;i<n-1;i++) {
407			int m;
408			recorded[i].needed = False;
409
410			close_files(cli, nfs, fnum);
411			open_files(cli, nfs, fnum);
412
413			m = retest(cli, nfs, fnum, n);
414			if (m == n) {
415				recorded[i].needed = True;
416			} else {
417				if (i < m) {
418					memmove(&recorded[i], &recorded[i+1],
419						(m-i)*sizeof(recorded[0]));
420				}
421				n = m;
422				i--;
423			}
424		}
425
426		if (n1 == n) break;
427	}
428
429	close_files(cli, nfs, fnum);
430	reconnect(cli, nfs, fnum, share1, share2);
431	open_files(cli, nfs, fnum);
432	showall = True;
433	n1 = retest(cli, nfs, fnum, n);
434	if (n1 != n-1) {
435		printf("ERROR - inconsistent result (%u %u)\n", n1, n);
436	}
437	close_files(cli, nfs, fnum);
438
439	for (i=0;i<n;i++) {
440		printf("{%u, %u, %u, %u, %u, %u, %u, %u},\n",
441		       recorded[i].r1,
442		       recorded[i].r2,
443		       recorded[i].conn,
444		       recorded[i].fstype,
445		       recorded[i].f,
446		       recorded[i].start,
447		       recorded[i].len,
448		       recorded[i].needed);
449	}
450}
451
452
453
454static void usage(void)
455{
456	printf(
457"Usage:\n\
458  locktest //server1/share1 //server2/share2 /path1 /path2 [options..]\n\
459  options:\n\
460        -U user%%pass\n\
461        -s seed\n\
462        -o numops\n\
463        -u          hide unlock fails\n\
464        -a          (show all ops)\n\
465        -O          use oplocks\n\
466");
467}
468
469/****************************************************************************
470  main program
471****************************************************************************/
472 int main(int argc,char *argv[])
473{
474	char *share1, *share2, *nfspath1, *nfspath2;
475	extern char *optarg;
476	extern int optind;
477	int opt;
478	char *p;
479	int seed;
480
481	setlinebuf(stdout);
482
483	dbf = x_stderr;
484
485	if (argc < 5 || argv[1][0] == '-') {
486		usage();
487		exit(1);
488	}
489
490	share1 = argv[1];
491	share2 = argv[2];
492	nfspath1 = argv[3];
493	nfspath2 = argv[4];
494
495	all_string_sub(share1,"/","\\",0);
496	all_string_sub(share2,"/","\\",0);
497
498	setup_logging(argv[0],True);
499
500	argc -= 4;
501	argv += 4;
502
503	lp_load(dyn_CONFIGFILE,True,False,False);
504	load_interfaces();
505
506	if (getenv("USER")) {
507		fstrcpy(username,getenv("USER"));
508	}
509
510	seed = time(NULL);
511
512	while ((opt = getopt(argc, argv, "U:s:ho:aAW:O")) != EOF) {
513		switch (opt) {
514		case 'U':
515			fstrcpy(username,optarg);
516			p = strchr_m(username,'%');
517			if (p) {
518				*p = 0;
519				fstrcpy(password, p+1);
520				got_pass = 1;
521			}
522			break;
523		case 's':
524			seed = atoi(optarg);
525			break;
526		case 'u':
527			hide_unlock_fails = True;
528			break;
529		case 'o':
530			numops = atoi(optarg);
531			break;
532		case 'O':
533			use_oplocks = True;
534			break;
535		case 'a':
536			showall = True;
537			break;
538		case 'A':
539			analyze = True;
540			break;
541		case 'h':
542			usage();
543			exit(1);
544		default:
545			printf("Unknown option %c (%d)\n", (char)opt, opt);
546			exit(1);
547		}
548	}
549
550	argc -= optind;
551	argv += optind;
552
553	DEBUG(0,("seed=%u\n", seed));
554	srandom(seed);
555
556	locking_init(1);
557	test_locks(share1, share2, nfspath1, nfspath2);
558
559	return(0);
560}
561