• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/router/samba-3.5.8/source4/torture/
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 3 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, see <http://www.gnu.org/licenses/>.
18*/
19
20#include "includes.h"
21#include "system/passwd.h"
22#include "lib/events/events.h"
23
24static fstring password;
25static fstring username;
26static int got_pass;
27static int numops = 1000;
28static bool showall;
29static bool analyze;
30static bool hide_unlock_fails;
31static bool use_oplocks;
32
33#define FILENAME "\\locktest.dat"
34#define LOCKRANGE 100
35#define LOCKBASE 0
36
37/*
38#define LOCKBASE (0x40000000 - 50)
39*/
40
41#define READ_PCT 50
42#define LOCK_PCT 25
43#define UNLOCK_PCT 65
44#define RANGE_MULTIPLE 1
45
46#define NSERVERS 2
47#define NCONNECTIONS 2
48#define NUMFSTYPES 2
49#define NFILES 2
50#define LOCK_TIMEOUT 0
51
52#define FSTYPE_SMB 0
53#define FSTYPE_NFS 1
54
55struct record {
56	char r1, r2;
57	char conn, f, fstype;
58	uint_t start, len;
59	char needed;
60};
61
62static struct record *recorded;
63
64static int try_open(struct smbcli_state *c, char *nfs, int fstype, const char *fname, int flags)
65{
66	char *path;
67	int ret;
68
69	switch (fstype) {
70	case FSTYPE_SMB:
71		return smbcli_open(c, fname, flags, DENY_NONE);
72
73	case FSTYPE_NFS:
74		asprintf(&path, "%s%s", nfs, fname);
75		string_replace(path,'\\', '/');
76		ret = open(path, flags, 0666);
77		SAFE_FREE(Path);
78		return ret;
79	}
80
81	return -1;
82}
83
84static bool try_close(struct smbcli_state *c, int fstype, int fd)
85{
86	switch (fstype) {
87	case FSTYPE_SMB:
88		return smbcli_close(c, fd);
89
90	case FSTYPE_NFS:
91		return close(fd) == 0;
92	}
93
94	return false;
95}
96
97static bool try_lock(struct smbcli_state *c, int fstype,
98		     int fd, uint_t start, uint_t len,
99		     enum brl_type op)
100{
101	struct flock lock;
102
103	switch (fstype) {
104	case FSTYPE_SMB:
105		return smbcli_lock(c, fd, start, len, LOCK_TIMEOUT, op);
106
107	case FSTYPE_NFS:
108		lock.l_type = (op==READ_LOCK) ? F_RDLCK:F_WRLCK;
109		lock.l_whence = SEEK_SET;
110		lock.l_start = start;
111		lock.l_len = len;
112		lock.l_pid = getpid();
113		return fcntl(fd,F_SETLK,&lock) == 0;
114	}
115
116	return false;
117}
118
119static bool try_unlock(struct smbcli_state *c, int fstype,
120		       int fd, uint_t start, uint_t len)
121{
122	struct flock lock;
123
124	switch (fstype) {
125	case FSTYPE_SMB:
126		return smbcli_unlock(c, fd, start, len);
127
128	case FSTYPE_NFS:
129		lock.l_type = F_UNLCK;
130		lock.l_whence = SEEK_SET;
131		lock.l_start = start;
132		lock.l_len = len;
133		lock.l_pid = getpid();
134		return fcntl(fd,F_SETLK,&lock) == 0;
135	}
136
137	return false;
138}
139
140/*****************************************************
141return a connection to a server
142*******************************************************/
143static struct smbcli_state *connect_one(TALLOC_CTX *mem_ctx,
144										char *share, const char **ports,
145					struct smb_options *options,
146					struct smb_options *session_options,
147					struct gensec_settings *gensec_settings,
148					struct tevent_context *ev)
149{
150	struct smbcli_state *c;
151	char *server_n;
152	char *server;
153	char *myname;
154	static int count;
155	NTSTATUS nt_status;
156
157	server = talloc_strdup(mem_ctx, share+2);
158	share = strchr_m(server,'\\');
159	if (!share) return NULL;
160	*share = 0;
161	share++;
162
163	server_n = server;
164
165	if (!got_pass) {
166		char *pass = getpass("Password: ");
167		if (pass) {
168			password = talloc_strdup(mem_ctx, pass);
169		}
170	}
171
172	myname = talloc_asprintf(mem_ctx, "lock-%u-%u", getpid(), count++);
173
174	nt_status = smbcli_full_connection(NULL,
175			   &c, myname, server_n, ports, share, NULL,
176			   username, lp_workgroup(), password, ev,
177			   options, session_options, gensec_settings);
178	if (!NT_STATUS_IS_OK(nt_status)) {
179		DEBUG(0, ("smbcli_full_connection failed with error %s\n", nt_errstr(nt_status)));
180		return NULL;
181	}
182
183	c->use_oplocks = use_oplocks;
184
185	return c;
186}
187
188
189static void reconnect(TALLOC_CTX *mem_ctx,
190					  struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
191		      char *nfs[NSERVERS],
192		      int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
193		      const char **ports,
194		      struct smbcli_options *options,
195		      struct smbcli_session_options *session_options,
196			  struct gensec_settings *gensec_settings,
197		      struct tevent_context *ev,
198		      char *share1, char *share2)
199{
200	int server, conn, f, fstype;
201	char *share[2];
202	share[0] = share1;
203	share[1] = share2;
204
205	fstype = FSTYPE_SMB;
206
207	for (server=0;server<NSERVERS;server++)
208	for (conn=0;conn<NCONNECTIONS;conn++) {
209		if (cli[server][conn]) {
210			for (f=0;f<NFILES;f++) {
211				smbcli_close(cli[server][conn], fnum[server][fstype][conn][f]);
212			}
213			smbcli_ulogoff(cli[server][conn]);
214			talloc_free(cli[server][conn]);
215		}
216		cli[server][conn] = connect_one(mem_ctx, share[server], ports, options, session_options, gensec_settings, ev);
217		if (!cli[server][conn]) {
218			DEBUG(0,("Failed to connect to %s\n", share[server]));
219			exit(1);
220		}
221	}
222}
223
224
225
226static bool test_one(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
227		     char *nfs[NSERVERS],
228		     int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
229		     struct record *rec)
230{
231	uint_t conn = rec->conn;
232	uint_t f = rec->f;
233	uint_t fstype = rec->fstype;
234	uint_t start = rec->start;
235	uint_t len = rec->len;
236	uint_t r1 = rec->r1;
237	uint_t r2 = rec->r2;
238	enum brl_type op;
239	int server;
240	bool ret[NSERVERS];
241
242	if (r1 < READ_PCT) {
243		op = READ_LOCK;
244	} else {
245		op = WRITE_LOCK;
246	}
247
248	if (r2 < LOCK_PCT) {
249		/* set a lock */
250		for (server=0;server<NSERVERS;server++) {
251			ret[server] = try_lock(cli[server][conn], fstype,
252					       fnum[server][fstype][conn][f],
253					       start, len, op);
254		}
255		if (showall || ret[0] != ret[1]) {
256			printf("lock   conn=%u fstype=%u f=%u range=%u:%u(%u) op=%s -> %u:%u\n",
257			       conn, fstype, f,
258			       start, start+len-1, len,
259			       op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
260			       ret[0], ret[1]);
261		}
262		if (ret[0] != ret[1]) return false;
263	} else if (r2 < LOCK_PCT+UNLOCK_PCT) {
264		/* unset a lock */
265		for (server=0;server<NSERVERS;server++) {
266			ret[server] = try_unlock(cli[server][conn], fstype,
267						 fnum[server][fstype][conn][f],
268						 start, len);
269		}
270		if (showall || (!hide_unlock_fails && (ret[0] != ret[1]))) {
271			printf("unlock conn=%u fstype=%u f=%u range=%u:%u(%u)       -> %u:%u\n",
272			       conn, fstype, f,
273			       start, start+len-1, len,
274			       ret[0], ret[1]);
275		}
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		}
292	}
293	return true;
294}
295
296static void close_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
297			char *nfs[NSERVERS],
298			int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
299{
300	int server, conn, f, fstype;
301
302	for (server=0;server<NSERVERS;server++)
303	for (fstype=0;fstype<NUMFSTYPES;fstype++)
304	for (conn=0;conn<NCONNECTIONS;conn++)
305	for (f=0;f<NFILES;f++) {
306		if (fnum[server][fstype][conn][f] != -1) {
307			try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
308			fnum[server][fstype][conn][f] = -1;
309		}
310	}
311	for (server=0;server<NSERVERS;server++) {
312		smbcli_unlink(cli[server][0], FILENAME);
313	}
314}
315
316static void open_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
317		       char *nfs[NSERVERS],
318		       int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
319{
320	int server, fstype, conn, f;
321
322	for (server=0;server<NSERVERS;server++)
323	for (fstype=0;fstype<NUMFSTYPES;fstype++)
324	for (conn=0;conn<NCONNECTIONS;conn++)
325	for (f=0;f<NFILES;f++) {
326		fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
327							 O_RDWR|O_CREAT);
328		if (fnum[server][fstype][conn][f] == -1) {
329			fprintf(stderr,"Failed to open fnum[%u][%u][%u][%u]\n",
330				server, fstype, conn, f);
331			exit(1);
332		}
333	}
334}
335
336
337static int retest(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
338		  char *nfs[NSERVERS],
339		  int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
340		  int n)
341{
342	int i;
343	printf("testing %u ...\n", n);
344	for (i=0; i<n; i++) {
345		if (i && i % 100 == 0) {
346			printf("%u\n", i);
347		}
348
349		if (recorded[i].needed &&
350		    !test_one(cli, nfs, fnum, &recorded[i])) return i;
351	}
352	return n;
353}
354
355
356/* each server has two connections open to it. Each connection has two file
357   descriptors open on the file - 8 file descriptors in total
358
359   we then do random locking ops in tamdem on the 4 fnums from each
360   server and ensure that the results match
361 */
362static void test_locks(TALLOC_CTX *mem_ctx, char *share1, char *share2,
363			char *nfspath1, char *nfspath2,
364			const char **ports,
365			struct smbcli_options *options,
366			struct smbcli_session_options *session_options,
367			struct gensec_settings *gensec_settings,
368			struct tevent_context *ev)
369{
370	struct smbcli_state *cli[NSERVERS][NCONNECTIONS];
371	char *nfs[NSERVERS];
372	int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES];
373	int n, i, n1;
374
375	nfs[0] = nfspath1;
376	nfs[1] = nfspath2;
377
378	ZERO_STRUCT(fnum);
379	ZERO_STRUCT(cli);
380
381	recorded = malloc_array_p(struct record, numops);
382
383	for (n=0; n<numops; n++) {
384		recorded[n].conn = random() % NCONNECTIONS;
385		recorded[n].fstype = random() % NUMFSTYPES;
386		recorded[n].f = random() % NFILES;
387		recorded[n].start = LOCKBASE + ((uint_t)random() % (LOCKRANGE-1));
388		recorded[n].len = 1 +
389			random() % (LOCKRANGE-(recorded[n].start-LOCKBASE));
390		recorded[n].start *= RANGE_MULTIPLE;
391		recorded[n].len *= RANGE_MULTIPLE;
392		recorded[n].r1 = random() % 100;
393		recorded[n].r2 = random() % 100;
394		recorded[n].needed = true;
395	}
396
397	reconnect(mem_ctx, cli, nfs, fnum, ports, options, session_options, gensec_settings, ev, share1, share2);
398	open_files(cli, nfs, fnum);
399	n = retest(cli, nfs, fnum, numops);
400
401	if (n == numops || !analyze) return;
402	n++;
403
404	while (1) {
405		n1 = n;
406
407		close_files(cli, nfs, fnum);
408		reconnect(mem_ctx, cli, nfs, fnum, ports, options, session_options, ev, share1, share2);
409		open_files(cli, nfs, fnum);
410
411		for (i=0;i<n-1;i++) {
412			int m;
413			recorded[i].needed = false;
414
415			close_files(cli, nfs, fnum);
416			open_files(cli, nfs, fnum);
417
418			m = retest(cli, nfs, fnum, n);
419			if (m == n) {
420				recorded[i].needed = true;
421			} else {
422				if (i < m) {
423					memmove(&recorded[i], &recorded[i+1],
424						(m-i)*sizeof(recorded[0]));
425				}
426				n = m;
427				i--;
428			}
429		}
430
431		if (n1 == n) break;
432	}
433
434	close_files(cli, nfs, fnum);
435	reconnect(mem_ctx, cli, nfs, fnum, ports, options, session_options, gensec_settings, ev, share1, share2);
436	open_files(cli, nfs, fnum);
437	showall = true;
438	n1 = retest(cli, nfs, fnum, n);
439	if (n1 != n-1) {
440		printf("ERROR - inconsistent result (%u %u)\n", n1, n);
441	}
442	close_files(cli, nfs, fnum);
443
444	for (i=0;i<n;i++) {
445		printf("{%u, %u, %u, %u, %u, %u, %u, %u},\n",
446		       recorded[i].r1,
447		       recorded[i].r2,
448		       recorded[i].conn,
449		       recorded[i].fstype,
450		       recorded[i].f,
451		       recorded[i].start,
452		       recorded[i].len,
453		       recorded[i].needed);
454	}
455}
456
457
458
459static void usage(void)
460{
461	printf(
462"Usage:\n\
463  locktest //server1/share1 //server2/share2 /path1 /path2 [options..]\n\
464  options:\n\
465        -U user%%pass\n\
466        -s seed\n\
467        -o numops\n\
468        -u          hide unlock fails\n\
469        -a          (show all ops)\n\
470        -O          use oplocks\n\
471");
472}
473
474/****************************************************************************
475  main program
476****************************************************************************/
477 int main(int argc,char *argv[])
478{
479	char *share1, *share2, *nfspath1, *nfspath2;
480	extern char *optarg;
481	extern int optind;
482	struct smbcli_options options;
483	struct smbcli_session_options session_options;
484	TALLOC_CTX *mem_ctx;
485	int opt;
486	char *p;
487	int seed;
488	struct loadparm_context *lp_ctx;
489	struct tevent_context *ev;
490
491	mem_ctx = talloc_autofree_context();
492
493	setlinebuf(stdout);
494
495	dbf = x_stderr;
496
497	if (argc < 5 || argv[1][0] == '-') {
498		usage();
499		exit(1);
500	}
501
502	share1 = argv[1];
503	share2 = argv[2];
504	nfspath1 = argv[3];
505	nfspath2 = argv[4];
506
507	all_string_sub(share1,"/","\\",0);
508	all_string_sub(share2,"/","\\",0);
509
510	setup_logging(argv[0], DEBUG_STDOUT);
511
512	argc -= 4;
513	argv += 4;
514
515	lp_ctx = loadparm_init(mem_ctx);
516	lp_load(lp_ctx, dyn_CONFIGFILE);
517
518	if (getenv("USER")) {
519		username = talloc_strdup(mem_ctx, getenv("USER"));
520	}
521
522	seed = time(NULL);
523
524	while ((opt = getopt(argc, argv, "U:s:ho:aAW:O")) != EOF) {
525		switch (opt) {
526		case 'U':
527			username = talloc_strdup(mem_ctx, optarg);
528			p = strchr_m(username,'%');
529			if (p) {
530				*p = 0;
531				password = talloc_strdup(mem_ctx, p+1);
532				got_pass = 1;
533			}
534			break;
535		case 's':
536			seed = atoi(optarg);
537			break;
538		case 'u':
539			hide_unlock_fails = true;
540			break;
541		case 'o':
542			numops = atoi(optarg);
543			break;
544		case 'O':
545			use_oplocks = true;
546			break;
547		case 'a':
548			showall = true;
549			break;
550		case 'A':
551			analyze = true;
552			break;
553		case 'h':
554			usage();
555			exit(1);
556		default:
557			printf("Unknown option %c (%d)\n", (char)opt, opt);
558			exit(1);
559		}
560	}
561
562	argc -= optind;
563	argv += optind;
564
565	DEBUG(0,("seed=%u\n", seed));
566	srandom(seed);
567
568	ev = s4_event_context_init(mem_ctx);
569
570	locking_init(1);
571	lp_smbcli_options(lp_ctx, &options);
572	lp_smbcli_session_options(lp_ctx, &session_options);
573	test_locks(mem_ctx, share1, share2, nfspath1, nfspath2,
574			   lp_smb_ports(lp_ctx),
575			   &options, &session_options, lp_gensec_settings(lp_ctx), ev);
576
577	return(0);
578}
579