2 Unix SMB/CIFS implementation.
3 randomised byte range lock tester
4 Copyright (C) Andrew Tridgell 1999
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.
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.
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.
25 static fstring password;
26 static fstring username;
28 static int numops = 1000;
31 static BOOL hide_unlock_fails;
32 static BOOL use_oplocks;
34 #define FILENAME "\\locktest.dat"
42 #define LOCKBASE (0x40000000 - 50)
48 #define RANGE_MULTIPLE 1
50 #define NCONNECTIONS 2
52 #define LOCK_TIMEOUT 0
54 #define NASTY_POSIX_LOCK_HACK 0
56 enum lock_op {OP_LOCK, OP_UNLOCK, OP_REOPEN};
60 enum brl_type lock_type;
62 SMB_BIG_UINT start, len;
69 static struct record preset[] = {
70 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
71 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
72 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 0, 1},
73 {OP_UNLOCK, 0 , 0, 0, 2, 0, 1},
74 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
76 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
77 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
78 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
79 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
81 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
82 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 1, 1},
83 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
84 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
86 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
87 {OP_LOCK, WRITE_LOCK, 0, 0, 1, 1, 1},
88 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
89 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
91 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
92 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
93 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
94 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
96 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
97 {OP_LOCK, READ_LOCK, 0, 0, 3, 1, 1},
98 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
99 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
104 static struct record *recorded;
106 static void print_brl(SMB_DEV_T dev, SMB_INO_T ino, int pid,
107 enum brl_type lock_type,
108 br_off start, br_off size)
110 #if NASTY_POSIX_LOCK_HACK
113 static SMB_INO_T lastino;
115 if (lastino != ino) {
116 slprintf(cmd, sizeof(cmd),
117 "egrep POSIX.*%u /proc/locks", (int)ino);
124 printf("%6d %05x:%05x %s %.0f:%.0f(%.0f)\n",
125 (int)pid, (int)dev, (int)ino,
126 lock_type==READ_LOCK?"R":"W",
127 (double)start, (double)start+size-1,(double)size);
132 static void show_locks(void)
134 brl_forall(print_brl);
135 /* system("cat /proc/locks"); */
139 /*****************************************************
140 return a connection to a server
141 *******************************************************/
142 struct cli_state *connect_one(char *share)
145 struct nmb_name called, calling;
152 fstrcpy(server,share+2);
153 share = strchr_m(server,'\\');
154 if (!share) return NULL;
162 slprintf(myname,sizeof(myname), "lock-%u-%u", getpid(), count++);
164 make_nmb_name(&calling, myname, 0x0);
165 make_nmb_name(&called , server, 0x20);
170 /* have to open a new connection */
171 if (!(c=cli_initialise(NULL)) || !cli_connect(c, server_n, &ip)) {
172 DEBUG(0,("Connection to %s failed\n", server_n));
176 if (!cli_session_request(c, &calling, &called)) {
177 DEBUG(0,("session request to %s failed\n", called.name));
179 if (strcmp(called.name, "*SMBSERVER")) {
180 make_nmb_name(&called , "*SMBSERVER", 0x20);
186 DEBUG(4,(" session request ok\n"));
188 if (!cli_negprot(c)) {
189 DEBUG(0,("protocol negotiation failed\n"));
195 char *pass = getpass("Password: ");
197 pstrcpy(password, pass);
201 if (!cli_session_setup(c, username,
202 password, strlen(password),
203 password, strlen(password),
205 DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
210 * These next two lines are needed to emulate
211 * old client behaviour for people who have
212 * scripts based on client output.
213 * QUESTION ? Do we want to have a 'client compatibility
214 * mode to turn these on/off ? JRA.
217 if (*c->server_domain || *c->server_os || *c->server_type)
218 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
219 c->server_domain,c->server_os,c->server_type));
221 DEBUG(4,(" session setup ok\n"));
223 if (!cli_send_tconX(c, share, "?????",
224 password, strlen(password)+1)) {
225 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c)));
230 DEBUG(4,(" tconx ok\n"));
232 c->use_oplocks = use_oplocks;
238 static void reconnect(struct cli_state *cli[NSERVERS][NCONNECTIONS], int fnum[NSERVERS][NCONNECTIONS][NFILES],
239 char *share[NSERVERS])
243 for (server=0;server<NSERVERS;server++)
244 for (conn=0;conn<NCONNECTIONS;conn++) {
245 if (cli[server][conn]) {
246 for (f=0;f<NFILES;f++) {
247 cli_close(cli[server][conn], fnum[server][conn][f]);
249 cli_ulogoff(cli[server][conn]);
250 cli_shutdown(cli[server][conn]);
252 cli[server][conn] = connect_one(share[server]);
253 if (!cli[server][conn]) {
254 DEBUG(0,("Failed to connect to %s\n", share[server]));
262 static BOOL test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS],
263 int fnum[NSERVERS][NCONNECTIONS][NFILES],
266 unsigned conn = rec->conn;
268 SMB_BIG_UINT start = rec->start;
269 SMB_BIG_UINT len = rec->len;
270 enum brl_type op = rec->lock_type;
274 switch (rec->lock_op) {
277 for (server=0;server<NSERVERS;server++) {
278 ret[server] = cli_lock64(cli[server][conn],
279 fnum[server][conn][f],
280 start, len, LOCK_TIMEOUT, op);
282 if (showall || ret[0] != ret[1]) {
283 printf("lock conn=%u f=%u range=%.0f(%.0f) op=%s -> %u:%u\n",
285 (double)start, (double)len,
286 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
289 if (showall || ret[0] != ret[1]) show_locks();
290 if (ret[0] != ret[1]) return False;
295 for (server=0;server<NSERVERS;server++) {
296 ret[server] = cli_unlock64(cli[server][conn],
297 fnum[server][conn][f],
300 if (showall || (!hide_unlock_fails && (ret[0] != ret[1]))) {
301 printf("unlock conn=%u f=%u range=%.0f(%.0f) -> %u:%u\n",
303 (double)start, (double)len,
306 if (showall || ret[0] != ret[1]) show_locks();
307 if (!hide_unlock_fails && ret[0] != ret[1]) return False;
311 /* reopen the file */
312 for (server=0;server<NSERVERS;server++) {
313 cli_close(cli[server][conn], fnum[server][conn][f]);
315 for (server=0;server<NSERVERS;server++) {
316 fnum[server][conn][f] = cli_open(cli[server][conn], FILENAME,
319 if (fnum[server][conn][f] == -1) {
320 printf("failed to reopen on share%d\n", server);
325 printf("reopen conn=%u f=%u\n",
335 static void close_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
336 int fnum[NSERVERS][NCONNECTIONS][NFILES])
340 for (server=0;server<NSERVERS;server++)
341 for (conn=0;conn<NCONNECTIONS;conn++)
342 for (f=0;f<NFILES;f++) {
343 if (fnum[server][conn][f] != -1) {
344 cli_close(cli[server][conn], fnum[server][conn][f]);
345 fnum[server][conn][f] = -1;
348 for (server=0;server<NSERVERS;server++) {
349 cli_unlink(cli[server][0], FILENAME);
353 static void open_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
354 int fnum[NSERVERS][NCONNECTIONS][NFILES])
358 for (server=0;server<NSERVERS;server++)
359 for (conn=0;conn<NCONNECTIONS;conn++)
360 for (f=0;f<NFILES;f++) {
361 fnum[server][conn][f] = cli_open(cli[server][conn], FILENAME,
364 if (fnum[server][conn][f] == -1) {
365 fprintf(stderr,"Failed to open fnum[%u][%u][%u]\n",
373 static int retest(struct cli_state *cli[NSERVERS][NCONNECTIONS],
374 int fnum[NSERVERS][NCONNECTIONS][NFILES],
378 printf("testing %u ...\n", n);
379 for (i=0; i<n; i++) {
380 if (i && i % 100 == 0) {
384 if (recorded[i].needed &&
385 !test_one(cli, fnum, &recorded[i])) return i;
391 /* each server has two connections open to it. Each connection has two file
392 descriptors open on the file - 8 file descriptors in total
394 we then do random locking ops in tamdem on the 4 fnums from each
395 server and ensure that the results match
397 static void test_locks(char *share[NSERVERS])
399 struct cli_state *cli[NSERVERS][NCONNECTIONS];
400 int fnum[NSERVERS][NCONNECTIONS][NFILES];
401 int n, i, n1, skip, r1, r2;
406 recorded = (struct record *)malloc(sizeof(*recorded) * numops);
408 for (n=0; n<numops; n++) {
410 if (n < sizeof(preset) / sizeof(preset[0])) {
411 recorded[n] = preset[n];
414 recorded[n].conn = random() % NCONNECTIONS;
415 recorded[n].f = random() % NFILES;
416 recorded[n].start = LOCKBASE + ((unsigned)random() % (LOCKRANGE-1));
417 recorded[n].len = MINLENGTH +
418 random() % (LOCKRANGE-(recorded[n].start-LOCKBASE));
419 recorded[n].start *= RANGE_MULTIPLE;
420 recorded[n].len *= RANGE_MULTIPLE;
424 recorded[n].lock_type = READ_LOCK;
426 recorded[n].lock_type = WRITE_LOCK;
429 recorded[n].lock_op = OP_LOCK;
430 } else if (r2 < UNLOCK_PCT) {
431 recorded[n].lock_op = OP_UNLOCK;
433 recorded[n].lock_op = OP_REOPEN;
435 recorded[n].needed = True;
437 if (recorded[n].start == 0 &&
438 recorded[n].len == 0) {
447 reconnect(cli, fnum, share);
448 open_files(cli, fnum);
449 n = retest(cli, fnum, numops);
451 if (n == numops || !analyze) return;
459 close_files(cli, fnum);
460 reconnect(cli, fnum, share);
461 open_files(cli, fnum);
463 for (i=0;i<n-skip;i+=skip) {
465 printf("excluding %d-%d\n", i, i+skip-1);
466 for (j=i;j<i+skip;j++) {
467 recorded[j].needed = False;
470 close_files(cli, fnum);
471 open_files(cli, fnum);
473 m = retest(cli, fnum, n);
475 for (j=i;j<i+skip;j++) {
476 recorded[j].needed = True;
479 if (i+(skip-1) < m) {
480 memmove(&recorded[i], &recorded[i+skip],
481 (m-(i+skip-1))*sizeof(recorded[0]));
490 printf("skip=%d\n", skip);
497 close_files(cli, fnum);
498 reconnect(cli, fnum, share);
499 open_files(cli, fnum);
501 n1 = retest(cli, fnum, n);
503 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
505 close_files(cli, fnum);
508 printf("{%d, %d, %u, %u, %.0f, %.0f, %u},\n",
510 recorded[i].lock_type,
513 (double)recorded[i].start,
514 (double)recorded[i].len,
521 static void usage(void)
525 locktest //server1/share1 //server2/share2 [options..]\n\
530 -u hide unlock fails\n\
532 -A analyse for minimal ops\n\
537 /****************************************************************************
539 ****************************************************************************/
540 int main(int argc,char *argv[])
542 char *share[NSERVERS];
553 if (argc < 3 || argv[1][0] == '-') {
558 setup_logging(argv[0],True);
560 for (server=0;server<NSERVERS;server++) {
561 share[server] = argv[1+server];
562 all_string_sub(share[server],"/","\\",0);
568 lp_load(dyn_CONFIGFILE,True,False,False);
571 if (getenv("USER")) {
572 pstrcpy(username,getenv("USER"));
577 while ((opt = getopt(argc, argv, "U:s:ho:aAW:O")) != EOF) {
580 pstrcpy(username,optarg);
581 p = strchr_m(username,'%');
584 pstrcpy(password, p+1);
592 hide_unlock_fails = True;
595 numops = atoi(optarg);
610 printf("Unknown option %c (%d)\n", (char)opt, opt);
618 DEBUG(0,("seed=%u\n", seed));