r16988: Extend locktest to enable cluster testing byte range locks. -l <unclist>...
[amitay/samba.git] / source4 / 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 #include "includes.h"
22 #include "system/filesys.h"
23 #include "system/time.h"
24 #include "pstring.h"
25 #include "auth/gensec/gensec.h"
26 #include "libcli/libcli.h"
27
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 static uint_t lock_range = 100;
34 static uint_t lock_base = 0;
35 static uint_t min_length = 0;
36 static BOOL exact_error_codes;
37 static BOOL zero_zero;
38
39 #define FILENAME "\\locktest.dat"
40
41 #define READ_PCT 50
42 #define LOCK_PCT 45
43 #define UNLOCK_PCT 70
44 #define RANGE_MULTIPLE 1
45 #define NSERVERS 2
46 #define NCONNECTIONS 2
47 #define NFILES 2
48 #define LOCK_TIMEOUT 0
49
50 #define NASTY_POSIX_LOCK_HACK 0
51
52 static struct cli_credentials *servers[NSERVERS];
53
54 enum lock_op {OP_LOCK, OP_UNLOCK, OP_REOPEN};
55
56 struct record {
57         enum lock_op lock_op;
58         enum brl_type lock_type;
59         char conn, f;
60         uint64_t start, len;
61         char needed;
62 };
63
64 #define PRESETS 0
65
66 #if PRESETS
67 static struct record preset[] = {
68 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
69 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
70 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 0, 1},
71 {OP_UNLOCK, 0       , 0, 0, 2, 0, 1},
72 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
73
74 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
75 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
76 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
77 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
78
79 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
80 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 1, 1},
81 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
82 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
83
84 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
85 {OP_LOCK, WRITE_LOCK, 0, 0, 1, 1, 1},
86 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
87 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
88
89 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
90 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
91 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
92 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
93
94 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
95 {OP_LOCK, READ_LOCK, 0, 0, 3, 1, 1},
96 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
97 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
98
99 };
100 #endif
101
102 static struct record *recorded;
103
104 /***************************************************** 
105 return a connection to a server
106 *******************************************************/
107 static struct smbcli_state *connect_one(char *share, int snum, int conn)
108 {
109         struct smbcli_state *c;
110         fstring server, myname;
111         NTSTATUS status;
112         int retries = 10;
113
114         printf("connect_one(%s, %d, %d)\n", share, snum, conn);
115
116         fstrcpy(server,share+2);
117         share = strchr_m(server,'\\');
118         if (!share) return NULL;
119         *share = 0;
120         share++;
121
122         if (snum == 0) {
123                 char **unc_list = NULL;
124                 int num_unc_names;
125                 const char *p;
126                 p = lp_parm_string(-1, "torture", "unclist");
127                 if (p) {
128                         char *h, *s;
129                         unc_list = file_lines_load(p, &num_unc_names, NULL);
130                         if (!unc_list || num_unc_names <= 0) {
131                                 printf("Failed to load unc names list from '%s'\n", p);
132                                 exit(1);
133                         }
134
135                         if (!smbcli_parse_unc(unc_list[conn % num_unc_names],
136                                               NULL, &h, &s)) {
137                                 printf("Failed to parse UNC name %s\n",
138                                        unc_list[conn % num_unc_names]);
139                                 exit(1);
140                         }
141                         fstrcpy(server, h);
142                         fstrcpy(share, s);
143                 }
144         }
145
146
147         slprintf(myname,sizeof(myname), "lock-%u-%u", getpid(), snum);
148         cli_credentials_set_workstation(servers[snum], myname, CRED_SPECIFIED);
149
150         do {
151                 printf("\\\\%s\\%s\n", server, share);
152                 status = smbcli_full_connection(NULL, &c, 
153                                                 server, 
154                                                 share, NULL,
155                                                 servers[snum], NULL);
156                 if (!NT_STATUS_IS_OK(status)) {
157                         sleep(2);
158                 }
159         } while (!NT_STATUS_IS_OK(status) && retries--);
160
161         if (!NT_STATUS_IS_OK(status)) {
162                 return NULL;
163         }
164
165         return c;
166 }
167
168
169 static void reconnect(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], int fnum[NSERVERS][NCONNECTIONS][NFILES],
170                       char *share[NSERVERS])
171 {
172         int server, conn, f;
173
174         for (server=0;server<NSERVERS;server++)
175         for (conn=0;conn<NCONNECTIONS;conn++) {
176                 if (cli[server][conn]) {
177                         for (f=0;f<NFILES;f++) {
178                                 if (fnum[server][conn][f] != -1) {
179                                         smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
180                                         fnum[server][conn][f] = -1;
181                                 }
182                         }
183                         talloc_free(cli[server][conn]);
184                 }
185                 cli[server][conn] = connect_one(share[server], server, conn);
186                 if (!cli[server][conn]) {
187                         DEBUG(0,("Failed to connect to %s\n", share[server]));
188                         exit(1);
189                 }
190         }
191 }
192
193
194
195 static BOOL test_one(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], 
196                      int fnum[NSERVERS][NCONNECTIONS][NFILES],
197                      struct record *rec)
198 {
199         uint_t conn = rec->conn;
200         uint_t f = rec->f;
201         uint64_t start = rec->start;
202         uint64_t len = rec->len;
203         enum brl_type op = rec->lock_type;
204         int server;
205         BOOL ret[NSERVERS];
206         NTSTATUS status[NSERVERS];
207
208         switch (rec->lock_op) {
209         case OP_LOCK:
210                 /* set a lock */
211                 for (server=0;server<NSERVERS;server++) {
212                         ret[server] = NT_STATUS_IS_OK(smbcli_lock64(cli[server][conn]->tree, 
213                                                  fnum[server][conn][f],
214                                                  start, len, LOCK_TIMEOUT, op));
215                         status[server] = smbcli_nt_error(cli[server][conn]->tree);
216                         if (!exact_error_codes && 
217                             NT_STATUS_EQUAL(status[server], 
218                                             NT_STATUS_FILE_LOCK_CONFLICT)) {
219                                 status[server] = NT_STATUS_LOCK_NOT_GRANTED;
220                         }
221                 }
222                 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) {
223                         printf("lock   conn=%u f=%u range=%.0f(%.0f) op=%s -> %s:%s\n",
224                                conn, f, 
225                                (double)start, (double)len,
226                                op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
227                                nt_errstr(status[0]), nt_errstr(status[1]));
228                 }
229                 if (!NT_STATUS_EQUAL(status[0],status[1])) return False;
230                 break;
231                 
232         case OP_UNLOCK:
233                 /* unset a lock */
234                 for (server=0;server<NSERVERS;server++) {
235                         ret[server] = NT_STATUS_IS_OK(smbcli_unlock64(cli[server][conn]->tree, 
236                                                    fnum[server][conn][f],
237                                                    start, len));
238                         status[server] = smbcli_nt_error(cli[server][conn]->tree);
239                 }
240                 if (showall || 
241                     (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))) {
242                         printf("unlock conn=%u f=%u range=%.0f(%.0f)       -> %s:%s\n",
243                                conn, f, 
244                                (double)start, (double)len,
245                                nt_errstr(status[0]), nt_errstr(status[1]));
246                 }
247                 if (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1])) 
248                         return False;
249                 break;
250
251         case OP_REOPEN:
252                 /* reopen the file */
253                 for (server=0;server<NSERVERS;server++) {
254                         smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
255                         fnum[server][conn][f] = -1;
256                 }
257                 for (server=0;server<NSERVERS;server++) {
258                         fnum[server][conn][f] = smbcli_open(cli[server][conn]->tree, FILENAME,
259                                                          O_RDWR|O_CREAT,
260                                                          DENY_NONE);
261                         if (fnum[server][conn][f] == -1) {
262                                 printf("failed to reopen on share%d\n", server);
263                                 return False;
264                         }
265                 }
266                 if (showall) {
267                         printf("reopen conn=%u f=%u\n",
268                                conn, f);
269                 }
270                 break;
271         }
272
273         return True;
274 }
275
276 static void close_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], 
277                         int fnum[NSERVERS][NCONNECTIONS][NFILES])
278 {
279         int server, conn, f; 
280
281         for (server=0;server<NSERVERS;server++)
282         for (conn=0;conn<NCONNECTIONS;conn++)
283         for (f=0;f<NFILES;f++) {
284                 if (fnum[server][conn][f] != -1) {
285                         smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
286                         fnum[server][conn][f] = -1;
287                 }
288         }
289         for (server=0;server<NSERVERS;server++) {
290                 smbcli_unlink(cli[server][0]->tree, FILENAME);
291         }
292 }
293
294 static void open_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], 
295                        int fnum[NSERVERS][NCONNECTIONS][NFILES])
296 {
297         int server, conn, f; 
298
299         for (server=0;server<NSERVERS;server++)
300         for (conn=0;conn<NCONNECTIONS;conn++)
301         for (f=0;f<NFILES;f++) {
302                 fnum[server][conn][f] = smbcli_open(cli[server][conn]->tree, FILENAME,
303                                                  O_RDWR|O_CREAT,
304                                                  DENY_NONE);
305                 if (fnum[server][conn][f] == -1) {
306                         fprintf(stderr,"Failed to open fnum[%u][%u][%u]\n",
307                                 server, conn, f);
308                         exit(1);
309                 }
310         }
311 }
312
313
314 static int retest(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], 
315                    int fnum[NSERVERS][NCONNECTIONS][NFILES],
316                    int n)
317 {
318         int i;
319         printf("testing %u ...\n", n);
320         for (i=0; i<n; i++) {
321                 if (i && i % 100 == 0) {
322                         printf("%u\n", i);
323                 }
324
325                 if (recorded[i].needed &&
326                     !test_one(cli, fnum, &recorded[i])) return i;
327         }
328         return n;
329 }
330
331
332 /* each server has two connections open to it. Each connection has two file
333    descriptors open on the file - 8 file descriptors in total 
334
335    we then do random locking ops in tamdem on the 4 fnums from each
336    server and ensure that the results match
337  */
338 static void test_locks(char *share[NSERVERS])
339 {
340         struct smbcli_state *cli[NSERVERS][NCONNECTIONS];
341         int fnum[NSERVERS][NCONNECTIONS][NFILES];
342         int n, i, n1, skip, r1, r2; 
343
344         ZERO_STRUCT(fnum);
345         ZERO_STRUCT(cli);
346
347         recorded = malloc_array_p(struct record, numops);
348
349         for (n=0; n<numops; n++) {
350 #if PRESETS
351                 if (n < sizeof(preset) / sizeof(preset[0])) {
352                         recorded[n] = preset[n];
353                 } else {
354 #endif
355                         recorded[n].conn = random() % NCONNECTIONS;
356                         recorded[n].f = random() % NFILES;
357                         recorded[n].start = lock_base + ((uint_t)random() % (lock_range-1));
358                         recorded[n].len =  min_length +
359                                 random() % (lock_range-(recorded[n].start-lock_base));
360                         recorded[n].start *= RANGE_MULTIPLE;
361                         recorded[n].len *= RANGE_MULTIPLE;
362                         r1 = random() % 100;
363                         r2 = random() % 100;
364                         if (r1 < READ_PCT) {
365                                 recorded[n].lock_type = READ_LOCK;
366                         } else {
367                                 recorded[n].lock_type = WRITE_LOCK;
368                         }
369                         if (r2 < LOCK_PCT) {
370                                 recorded[n].lock_op = OP_LOCK;
371                         } else if (r2 < UNLOCK_PCT) {
372                                 recorded[n].lock_op = OP_UNLOCK;
373                         } else {
374                                 recorded[n].lock_op = OP_REOPEN;
375                         }
376                         recorded[n].needed = True;
377                         if (!zero_zero && recorded[n].start==0 && recorded[n].len==0) {
378                                 recorded[n].len = 1;
379                         }
380 #if PRESETS
381                 }
382 #endif
383         }
384
385         reconnect(cli, fnum, share);
386         open_files(cli, fnum);
387         n = retest(cli, fnum, numops);
388
389         if (n == numops || !analyze) return;
390         n++;
391
392         skip = n/2;
393
394         while (1) {
395                 n1 = n;
396
397                 close_files(cli, fnum);
398                 reconnect(cli, fnum, share);
399                 open_files(cli, fnum);
400
401                 for (i=0;i<n-skip;i+=skip) {
402                         int m, j;
403                         printf("excluding %d-%d\n", i, i+skip-1);
404                         for (j=i;j<i+skip;j++) {
405                                 recorded[j].needed = False;
406                         }
407
408                         close_files(cli, fnum);
409                         open_files(cli, fnum);
410
411                         m = retest(cli, fnum, n);
412                         if (m == n) {
413                                 for (j=i;j<i+skip;j++) {
414                                         recorded[j].needed = True;
415                                 }
416                         } else {
417                                 if (i+(skip-1) < m) {
418                                         memmove(&recorded[i], &recorded[i+skip],
419                                                 (m-(i+skip-1))*sizeof(recorded[0]));
420                                 }
421                                 n = m-(skip-1);
422                                 i--;
423                         }
424                 }
425
426                 if (skip > 1) {
427                         skip = skip/2;
428                         printf("skip=%d\n", skip);
429                         continue;
430                 }
431
432                 if (n1 == n) break;
433         }
434
435         close_files(cli, fnum);
436         reconnect(cli, fnum, share);
437         open_files(cli, fnum);
438         showall = True;
439         n1 = retest(cli, fnum, n);
440         if (n1 != n-1) {
441                 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
442         }
443         close_files(cli, fnum);
444
445         for (i=0;i<n;i++) {
446                 printf("{%d, %d, %u, %u, %.0f, %.0f, %u},\n",
447                        recorded[i].lock_op,
448                        recorded[i].lock_type,
449                        recorded[i].conn,
450                        recorded[i].f,
451                        (double)recorded[i].start,
452                        (double)recorded[i].len,
453                        recorded[i].needed);
454         }       
455 }
456
457
458
459 static void usage(void)
460 {
461         printf(
462 "Usage:\n\
463   locktest //server1/share1 //server2/share2 [options..]\n\
464   options:\n\
465         -U user%%pass        (may be specified twice)\n\
466         -s seed\n\
467         -o numops\n\
468         -u          hide unlock fails\n\
469         -a          (show all ops)\n\
470         -A          analyse for minimal ops\n\
471         -O          use oplocks\n\
472         -E          enable exact error code checking\n\
473         -Z          enable the zero/zero lock\n\
474         -R range    set lock range\n\
475         -B base     set lock base\n\
476         -M min      set min lock length\n\
477         -l filename unclist file\n\
478 ");
479 }
480
481 /****************************************************************************
482   main program
483 ****************************************************************************/
484  int main(int argc,char *argv[])
485 {
486         char *share[NSERVERS];
487         int opt;
488         int seed, server;
489         int username_count=0;
490
491         setlinebuf(stdout);
492
493         setup_logging("locktest", DEBUG_STDOUT);
494
495         if (argc < 3 || argv[1][0] == '-') {
496                 usage();
497                 exit(1);
498         }
499
500         setup_logging(argv[0], DEBUG_STDOUT);
501
502         for (server=0;server<NSERVERS;server++) {
503                 share[server] = argv[1+server];
504                 all_string_sub(share[server],"/","\\",0);
505         }
506
507         argc -= NSERVERS;
508         argv += NSERVERS;
509
510         lp_load();
511
512         servers[0] = cli_credentials_init(talloc_autofree_context());
513         servers[1] = cli_credentials_init(talloc_autofree_context());
514         cli_credentials_guess(servers[0]);
515         cli_credentials_guess(servers[1]);
516
517         seed = time(NULL);
518
519         while ((opt = getopt(argc, argv, "U:s:ho:aAW:OR:B:M:EZW:l:")) != EOF) {
520                 switch (opt) {
521                 case 'U':
522                         if (username_count == 2) {
523                                 usage();
524                                 exit(1);
525                         }
526                         cli_credentials_parse_string(servers[username_count], 
527                                                      optarg, CRED_SPECIFIED);
528                         username_count++;
529                         break;
530                 case 'R':
531                         lock_range = strtol(optarg, NULL, 0);
532                         break;
533                 case 'B':
534                         lock_base = strtol(optarg, NULL, 0);
535                         break;
536                 case 'M':
537                         min_length = strtol(optarg, NULL, 0);
538                         break;
539                 case 's':
540                         seed = atoi(optarg);
541                         break;
542                 case 'u':
543                         hide_unlock_fails = True;
544                         break;
545                 case 'o':
546                         numops = atoi(optarg);
547                         break;
548                 case 'O':
549                         use_oplocks = True;
550                         break;
551                 case 'a':
552                         showall = True;
553                         break;
554                 case 'A':
555                         analyze = True;
556                         break;
557                 case 'Z':
558                         zero_zero = True;
559                         break;
560                 case 'E':
561                         exact_error_codes = True;
562                         break;
563                 case 'l':
564                         lp_set_cmdline("torture:unclist", optarg);
565                         break;
566                 case 'W':
567                         lp_set_cmdline("workgroup", optarg);
568                         break;
569                 case 'h':
570                         usage();
571                         exit(1);
572                 default:
573                         printf("Unknown option %c (%d)\n", (char)opt, opt);
574                         exit(1);
575                 }
576         }
577
578         if (username_count == 0) {
579                 usage();
580                 return -1;
581         }
582         if (username_count == 1) {
583                 servers[1] = servers[0];
584         }
585
586         gensec_init();
587
588         argc -= optind;
589         argv += optind;
590
591         DEBUG(0,("seed=%u base=%d range=%d min_length=%d\n", 
592                  seed, lock_base, lock_range, min_length));
593         srandom(seed);
594
595         test_locks(share);
596
597         return(0);
598 }
599