Removed version number from file header.
[ira/wip.git] / source3 / torture / locktest.c
1 /* 
2    Unix SMB/CIFS implementation.
3    randomised byte range lock tester
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
25 static fstring password;
26 static fstring username;
27 static int got_pass;
28 static int numops = 1000;
29 static BOOL showall;
30 static BOOL analyze;
31 static BOOL hide_unlock_fails;
32 static BOOL use_oplocks;
33
34 #define FILENAME "\\locktest.dat"
35 #define LOCKRANGE 100
36 #define LOCKBASE 0
37 #define MINLENGTH 0
38
39 #define ZERO_ZERO 0
40
41 /*
42 #define LOCKBASE (0x40000000 - 50)
43 */
44
45 #define READ_PCT 50
46 #define LOCK_PCT 45
47 #define UNLOCK_PCT 70
48 #define RANGE_MULTIPLE 1
49 #define NSERVERS 2
50 #define NCONNECTIONS 2
51 #define NFILES 2
52 #define LOCK_TIMEOUT 0
53
54 #define NASTY_POSIX_LOCK_HACK 0
55
56 enum lock_op {OP_LOCK, OP_UNLOCK, OP_REOPEN};
57
58 struct record {
59         enum lock_op lock_op;
60         enum brl_type lock_type;
61         char conn, f;
62         SMB_BIG_UINT start, len;
63         char needed;
64 };
65
66 #define PRESETS 0
67
68 #if PRESETS
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},
75
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},
80
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},
85
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},
90
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},
95
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},
100
101 };
102 #endif
103
104 static struct record *recorded;
105
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)
109 {
110 #if NASTY_POSIX_LOCK_HACK
111         {
112                 pstring cmd;
113                 static SMB_INO_T lastino;
114
115                 if (lastino != ino) {
116                         slprintf(cmd, sizeof(cmd), 
117                                  "egrep POSIX.*%u /proc/locks", (int)ino);
118                         system(cmd);
119                 }
120                 lastino = ino;
121         }
122 #endif
123
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);
128
129 }
130
131
132 static void show_locks(void)
133 {
134         brl_forall(print_brl);
135         /* system("cat /proc/locks"); */
136 }
137
138
139 /***************************************************** 
140 return a connection to a server
141 *******************************************************/
142 struct cli_state *connect_one(char *share)
143 {
144         struct cli_state *c;
145         struct nmb_name called, calling;
146         char *server_n;
147         fstring server;
148         struct in_addr ip;
149         fstring myname;
150         static int count;
151
152         fstrcpy(server,share+2);
153         share = strchr_m(server,'\\');
154         if (!share) return NULL;
155         *share = 0;
156         share++;
157
158         server_n = server;
159         
160         zero_ip(&ip);
161
162         slprintf(myname,sizeof(myname), "lock-%u-%u", getpid(), count++);
163
164         make_nmb_name(&calling, myname, 0x0);
165         make_nmb_name(&called , server, 0x20);
166
167  again:
168         zero_ip(&ip);
169
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));
173                 return NULL;
174         }
175
176         if (!cli_session_request(c, &calling, &called)) {
177                 DEBUG(0,("session request to %s failed\n", called.name));
178                 cli_shutdown(c);
179                 if (strcmp(called.name, "*SMBSERVER")) {
180                         make_nmb_name(&called , "*SMBSERVER", 0x20);
181                         goto again;
182                 }
183                 return NULL;
184         }
185
186         DEBUG(4,(" session request ok\n"));
187
188         if (!cli_negprot(c)) {
189                 DEBUG(0,("protocol negotiation failed\n"));
190                 cli_shutdown(c);
191                 return NULL;
192         }
193
194         if (!got_pass) {
195                 char *pass = getpass("Password: ");
196                 if (pass) {
197                         pstrcpy(password, pass);
198                 }
199         }
200
201         if (!cli_session_setup(c, username, 
202                                password, strlen(password),
203                                password, strlen(password),
204                                lp_workgroup())) {
205                 DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
206                 return NULL;
207         }
208
209         /*
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.
215          */
216
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));
220         
221         DEBUG(4,(" session setup ok\n"));
222
223         if (!cli_send_tconX(c, share, "?????",
224                             password, strlen(password)+1)) {
225                 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c)));
226                 cli_shutdown(c);
227                 return NULL;
228         }
229
230         DEBUG(4,(" tconx ok\n"));
231
232         c->use_oplocks = use_oplocks;
233
234         return c;
235 }
236
237
238 static void reconnect(struct cli_state *cli[NSERVERS][NCONNECTIONS], int fnum[NSERVERS][NCONNECTIONS][NFILES],
239                       char *share[NSERVERS])
240 {
241         int server, conn, f;
242
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]);
248                         }
249                         cli_ulogoff(cli[server][conn]);
250                         cli_shutdown(cli[server][conn]);
251                 }
252                 cli[server][conn] = connect_one(share[server]);
253                 if (!cli[server][conn]) {
254                         DEBUG(0,("Failed to connect to %s\n", share[server]));
255                         exit(1);
256                 }
257         }
258 }
259
260
261
262 static BOOL test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS], 
263                      int fnum[NSERVERS][NCONNECTIONS][NFILES],
264                      struct record *rec)
265 {
266         unsigned conn = rec->conn;
267         unsigned f = rec->f;
268         SMB_BIG_UINT start = rec->start;
269         SMB_BIG_UINT len = rec->len;
270         enum brl_type op = rec->lock_type;
271         int server;
272         BOOL ret[NSERVERS];
273
274         switch (rec->lock_op) {
275         case OP_LOCK:
276                 /* set a lock */
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);
281                 }
282                 if (showall || ret[0] != ret[1]) {
283                         printf("lock   conn=%u f=%u range=%.0f(%.0f) op=%s -> %u:%u\n",
284                                conn, f, 
285                                (double)start, (double)len,
286                                op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
287                                ret[0], ret[1]);
288                 }
289                 if (showall || ret[0] != ret[1]) show_locks();
290                 if (ret[0] != ret[1]) return False;
291                 break;
292                 
293         case OP_UNLOCK:
294                 /* unset a lock */
295                 for (server=0;server<NSERVERS;server++) {
296                         ret[server] = cli_unlock64(cli[server][conn], 
297                                                    fnum[server][conn][f],
298                                                    start, len);
299                 }
300                 if (showall || (!hide_unlock_fails && (ret[0] != ret[1]))) {
301                         printf("unlock conn=%u f=%u range=%.0f(%.0f)       -> %u:%u\n",
302                                conn, f, 
303                                (double)start, (double)len,
304                                ret[0], ret[1]);
305                 }
306                 if (showall || ret[0] != ret[1]) show_locks();
307                 if (!hide_unlock_fails && ret[0] != ret[1]) return False;
308                 break;
309
310         case OP_REOPEN:
311                 /* reopen the file */
312                 for (server=0;server<NSERVERS;server++) {
313                         cli_close(cli[server][conn], fnum[server][conn][f]);
314                 }
315                 for (server=0;server<NSERVERS;server++) {
316                         fnum[server][conn][f] = cli_open(cli[server][conn], FILENAME,
317                                                          O_RDWR|O_CREAT,
318                                                          DENY_NONE);
319                         if (fnum[server][conn][f] == -1) {
320                                 printf("failed to reopen on share%d\n", server);
321                                 return False;
322                         }
323                 }
324                 if (showall) {
325                         printf("reopen conn=%u f=%u\n",
326                                conn, f);
327                         show_locks();
328                 }
329                 break;
330         }
331
332         return True;
333 }
334
335 static void close_files(struct cli_state *cli[NSERVERS][NCONNECTIONS], 
336                         int fnum[NSERVERS][NCONNECTIONS][NFILES])
337 {
338         int server, conn, f; 
339
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;
346                 }
347         }
348         for (server=0;server<NSERVERS;server++) {
349                 cli_unlink(cli[server][0], FILENAME);
350         }
351 }
352
353 static void open_files(struct cli_state *cli[NSERVERS][NCONNECTIONS], 
354                        int fnum[NSERVERS][NCONNECTIONS][NFILES])
355 {
356         int server, conn, f; 
357
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,
362                                                  O_RDWR|O_CREAT,
363                                                  DENY_NONE);
364                 if (fnum[server][conn][f] == -1) {
365                         fprintf(stderr,"Failed to open fnum[%u][%u][%u]\n",
366                                 server, conn, f);
367                         exit(1);
368                 }
369         }
370 }
371
372
373 static int retest(struct cli_state *cli[NSERVERS][NCONNECTIONS], 
374                    int fnum[NSERVERS][NCONNECTIONS][NFILES],
375                    int n)
376 {
377         int i;
378         printf("testing %u ...\n", n);
379         for (i=0; i<n; i++) {
380                 if (i && i % 100 == 0) {
381                         printf("%u\n", i);
382                 }
383
384                 if (recorded[i].needed &&
385                     !test_one(cli, fnum, &recorded[i])) return i;
386         }
387         return n;
388 }
389
390
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 
393
394    we then do random locking ops in tamdem on the 4 fnums from each
395    server and ensure that the results match
396  */
397 static void test_locks(char *share[NSERVERS])
398 {
399         struct cli_state *cli[NSERVERS][NCONNECTIONS];
400         int fnum[NSERVERS][NCONNECTIONS][NFILES];
401         int n, i, n1, skip, r1, r2; 
402
403         ZERO_STRUCT(fnum);
404         ZERO_STRUCT(cli);
405
406         recorded = (struct record *)malloc(sizeof(*recorded) * numops);
407
408         for (n=0; n<numops; n++) {
409 #if PRESETS
410                 if (n < sizeof(preset) / sizeof(preset[0])) {
411                         recorded[n] = preset[n];
412                 } else {
413 #endif
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;
421                         r1 = random() % 100;
422                         r2 = random() % 100;
423                         if (r1 < READ_PCT) {
424                                 recorded[n].lock_type = READ_LOCK;
425                         } else {
426                                 recorded[n].lock_type = WRITE_LOCK;
427                         }
428                         if (r2 < LOCK_PCT) {
429                                 recorded[n].lock_op = OP_LOCK;
430                         } else if (r2 < UNLOCK_PCT) {
431                                 recorded[n].lock_op = OP_UNLOCK;
432                         } else {
433                                 recorded[n].lock_op = OP_REOPEN;
434                         }
435                         recorded[n].needed = True;
436 #if !ZERO_ZERO
437                         if (recorded[n].start == 0 &&
438                             recorded[n].len == 0) {
439                                 recorded[n].len = 1;
440                         }
441 #endif
442 #if PRESETS
443                 }
444 #endif
445         }
446
447         reconnect(cli, fnum, share);
448         open_files(cli, fnum);
449         n = retest(cli, fnum, numops);
450
451         if (n == numops || !analyze) return;
452         n++;
453
454         skip = n/2;
455
456         while (1) {
457                 n1 = n;
458
459                 close_files(cli, fnum);
460                 reconnect(cli, fnum, share);
461                 open_files(cli, fnum);
462
463                 for (i=0;i<n-skip;i+=skip) {
464                         int m, j;
465                         printf("excluding %d-%d\n", i, i+skip-1);
466                         for (j=i;j<i+skip;j++) {
467                                 recorded[j].needed = False;
468                         }
469
470                         close_files(cli, fnum);
471                         open_files(cli, fnum);
472
473                         m = retest(cli, fnum, n);
474                         if (m == n) {
475                                 for (j=i;j<i+skip;j++) {
476                                         recorded[j].needed = True;
477                                 }
478                         } else {
479                                 if (i+(skip-1) < m) {
480                                         memmove(&recorded[i], &recorded[i+skip],
481                                                 (m-(i+skip-1))*sizeof(recorded[0]));
482                                 }
483                                 n = m-(skip-1);
484                                 i--;
485                         }
486                 }
487
488                 if (skip > 1) {
489                         skip = skip/2;
490                         printf("skip=%d\n", skip);
491                         continue;
492                 }
493
494                 if (n1 == n) break;
495         }
496
497         close_files(cli, fnum);
498         reconnect(cli, fnum, share);
499         open_files(cli, fnum);
500         showall = True;
501         n1 = retest(cli, fnum, n);
502         if (n1 != n-1) {
503                 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
504         }
505         close_files(cli, fnum);
506
507         for (i=0;i<n;i++) {
508                 printf("{%d, %d, %u, %u, %.0f, %.0f, %u},\n",
509                        recorded[i].lock_op,
510                        recorded[i].lock_type,
511                        recorded[i].conn,
512                        recorded[i].f,
513                        (double)recorded[i].start,
514                        (double)recorded[i].len,
515                        recorded[i].needed);
516         }       
517 }
518
519
520
521 static void usage(void)
522 {
523         printf(
524 "Usage:\n\
525   locktest //server1/share1 //server2/share2 [options..]\n\
526   options:\n\
527         -U user%%pass\n\
528         -s seed\n\
529         -o numops\n\
530         -u          hide unlock fails\n\
531         -a          (show all ops)\n\
532         -A          analyse for minimal ops\n\
533         -O          use oplocks\n\
534 ");
535 }
536
537 /****************************************************************************
538   main program
539 ****************************************************************************/
540  int main(int argc,char *argv[])
541 {
542         char *share[NSERVERS];
543         extern char *optarg;
544         extern int optind;
545         int opt;
546         char *p;
547         int seed, server;
548
549         setlinebuf(stdout);
550
551         dbf = x_stderr;
552
553         if (argc < 3 || argv[1][0] == '-') {
554                 usage();
555                 exit(1);
556         }
557
558         setup_logging(argv[0],True);
559
560         for (server=0;server<NSERVERS;server++) {
561                 share[server] = argv[1+server];
562                 all_string_sub(share[server],"/","\\",0);
563         }
564
565         argc -= NSERVERS;
566         argv += NSERVERS;
567
568         lp_load(dyn_CONFIGFILE,True,False,False);
569         load_interfaces();
570
571         if (getenv("USER")) {
572                 pstrcpy(username,getenv("USER"));
573         }
574
575         seed = time(NULL);
576
577         while ((opt = getopt(argc, argv, "U:s:ho:aAW:O")) != EOF) {
578                 switch (opt) {
579                 case 'U':
580                         pstrcpy(username,optarg);
581                         p = strchr_m(username,'%');
582                         if (p) {
583                                 *p = 0;
584                                 pstrcpy(password, p+1);
585                                 got_pass = 1;
586                         }
587                         break;
588                 case 's':
589                         seed = atoi(optarg);
590                         break;
591                 case 'u':
592                         hide_unlock_fails = True;
593                         break;
594                 case 'o':
595                         numops = atoi(optarg);
596                         break;
597                 case 'O':
598                         use_oplocks = True;
599                         break;
600                 case 'a':
601                         showall = True;
602                         break;
603                 case 'A':
604                         analyze = True;
605                         break;
606                 case 'h':
607                         usage();
608                         exit(1);
609                 default:
610                         printf("Unknown option %c (%d)\n", (char)opt, opt);
611                         exit(1);
612                 }
613         }
614
615         argc -= optind;
616         argv += optind;
617
618         DEBUG(0,("seed=%u\n", seed));
619         srandom(seed);
620
621         test_locks(share);
622
623         return(0);
624 }