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