r25290: remove wrong comment
[kai/samba.git] / source / 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
29 static int numops = 1000;
30 static BOOL showall;
31 static BOOL analyze;
32 static BOOL hide_unlock_fails;
33 static BOOL use_oplocks;
34 static uint_t lock_range = 100;
35 static uint_t lock_base = 0;
36 static uint_t min_length = 0;
37 static BOOL exact_error_codes;
38 static BOOL zero_zero;
39
40 #define FILENAME "\\locktest.dat"
41
42 #define READ_PCT 50
43 #define LOCK_PCT 45
44 #define UNLOCK_PCT 70
45 #define RANGE_MULTIPLE 1
46 #define NSERVERS 2
47 #define NCONNECTIONS 2
48 #define NFILES 2
49 #define LOCK_TIMEOUT 0
50
51 static struct cli_credentials *servers[NSERVERS];
52
53 enum lock_op {OP_LOCK, OP_UNLOCK, OP_REOPEN};
54
55 struct record {
56         enum lock_op lock_op;
57         enum brl_type lock_type;
58         char conn, f;
59         uint64_t start, len;
60         char needed;
61         uint16_t pid;
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(NULL, "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                         NTSTATUS res;
213                         struct smbcli_tree *tree=cli[server][conn]->tree;
214                         int fn=fnum[server][conn][f];
215
216                         if (!(tree->session->transport->negotiate.capabilities & CAP_LARGE_FILES)) {
217                                 res=smbcli_lock(tree, fn, start, len, LOCK_TIMEOUT, rec->lock_op);
218                         } else {
219                                 union smb_lock parms;
220                                 int ltype;
221                                 struct smb_lock_entry lock[1];
222
223                                 parms.lockx.level = RAW_LOCK_LOCKX;
224                                 parms.lockx.in.file.fnum = fn;
225         
226                                 ltype = (rec->lock_op == READ_LOCK? 1 : 0);
227                                 ltype |= LOCKING_ANDX_LARGE_FILES;
228                                 parms.lockx.in.mode = ltype;
229                                 parms.lockx.in.timeout = LOCK_TIMEOUT;
230                                 parms.lockx.in.ulock_cnt = 0;
231                                 parms.lockx.in.lock_cnt = 1;
232                                 lock[0].pid = rec->pid;
233                                 lock[0].offset = start;
234                                 lock[0].count = len;
235                                 parms.lockx.in.locks = &lock[0];
236
237                                 res = smb_raw_lock(tree, &parms);
238                         }
239
240                         ret[server] = NT_STATUS_IS_OK(res); 
241                         status[server] = smbcli_nt_error(cli[server][conn]->tree);
242                         if (!exact_error_codes && 
243                             NT_STATUS_EQUAL(status[server], 
244                                             NT_STATUS_FILE_LOCK_CONFLICT)) {
245                                 status[server] = NT_STATUS_LOCK_NOT_GRANTED;
246                         }
247                 }
248                 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) {
249                         printf("lock   conn=%u f=%u range=%.0f(%.0f) op=%s -> %s:%s\n",
250                                conn, f, 
251                                (double)start, (double)len,
252                                op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
253                                nt_errstr(status[0]), nt_errstr(status[1]));
254                 }
255                 if (!NT_STATUS_EQUAL(status[0],status[1])) return False;
256                 break;
257                 
258         case OP_UNLOCK:
259                 /* unset a lock */
260                 for (server=0;server<NSERVERS;server++) {
261                         NTSTATUS res;
262                         struct smbcli_tree *tree=cli[server][conn]->tree;
263                         int fn=fnum[server][conn][f];
264
265
266                         if (!(tree->session->transport->negotiate.capabilities & CAP_LARGE_FILES)) {
267                                 res=smbcli_unlock(tree, fn, start, len);
268                         } else {
269                                 union smb_lock parms;
270                                 struct smb_lock_entry lock[1];
271
272                                 parms.lockx.level = RAW_LOCK_LOCKX;
273                                 parms.lockx.in.file.fnum = fn;
274                                 parms.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
275                                 parms.lockx.in.timeout = 0;
276                                 parms.lockx.in.ulock_cnt = 1;
277                                 parms.lockx.in.lock_cnt = 0;
278                                 lock[0].pid = rec->pid;
279                                 lock[0].count = len;
280                                 lock[0].offset = start;
281                                 parms.lockx.in.locks = &lock[0];
282
283                                 res = smb_raw_lock(tree, &parms);
284                         }
285
286                         ret[server] = NT_STATUS_IS_OK(res);
287                         status[server] = smbcli_nt_error(cli[server][conn]->tree);
288                 }
289                 if (showall || 
290                     (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))) {
291                         printf("unlock conn=%u f=%u range=%.0f(%.0f)       -> %s:%s\n",
292                                conn, f, 
293                                (double)start, (double)len,
294                                nt_errstr(status[0]), nt_errstr(status[1]));
295                 }
296                 if (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1])) 
297                         return False;
298                 break;
299
300         case OP_REOPEN:
301                 /* reopen the file */
302                 for (server=0;server<NSERVERS;server++) {
303                         smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
304                         fnum[server][conn][f] = -1;
305                 }
306                 for (server=0;server<NSERVERS;server++) {
307                         fnum[server][conn][f] = smbcli_open(cli[server][conn]->tree, FILENAME,
308                                                          O_RDWR|O_CREAT,
309                                                          DENY_NONE);
310                         if (fnum[server][conn][f] == -1) {
311                                 printf("failed to reopen on share%d\n", server);
312                                 return False;
313                         }
314                 }
315                 if (showall) {
316                         printf("reopen conn=%u f=%u\n",
317                                conn, f);
318                 }
319                 break;
320         }
321
322         return True;
323 }
324
325 static void close_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], 
326                         int fnum[NSERVERS][NCONNECTIONS][NFILES])
327 {
328         int server, conn, f; 
329
330         for (server=0;server<NSERVERS;server++)
331         for (conn=0;conn<NCONNECTIONS;conn++)
332         for (f=0;f<NFILES;f++) {
333                 if (fnum[server][conn][f] != -1) {
334                         smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
335                         fnum[server][conn][f] = -1;
336                 }
337         }
338         for (server=0;server<NSERVERS;server++) {
339                 smbcli_unlink(cli[server][0]->tree, FILENAME);
340         }
341 }
342
343 static void open_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], 
344                        int fnum[NSERVERS][NCONNECTIONS][NFILES])
345 {
346         int server, conn, f; 
347
348         for (server=0;server<NSERVERS;server++)
349         for (conn=0;conn<NCONNECTIONS;conn++)
350         for (f=0;f<NFILES;f++) {
351                 fnum[server][conn][f] = smbcli_open(cli[server][conn]->tree, FILENAME,
352                                                  O_RDWR|O_CREAT,
353                                                  DENY_NONE);
354                 if (fnum[server][conn][f] == -1) {
355                         fprintf(stderr,"Failed to open fnum[%u][%u][%u]\n",
356                                 server, conn, f);
357                         exit(1);
358                 }
359         }
360 }
361
362
363 static int retest(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], 
364                    int fnum[NSERVERS][NCONNECTIONS][NFILES],
365                    int n)
366 {
367         int i;
368         printf("testing %u ...\n", n);
369         for (i=0; i<n; i++) {
370                 if (i && i % 100 == 0) {
371                         printf("%u\n", i);
372                 }
373
374                 if (recorded[i].needed &&
375                     !test_one(cli, fnum, &recorded[i])) return i;
376         }
377         return n;
378 }
379
380
381 /* each server has two connections open to it. Each connection has two file
382    descriptors open on the file - 8 file descriptors in total 
383
384    we then do random locking ops in tamdem on the 4 fnums from each
385    server and ensure that the results match
386  */
387 static void test_locks(char *share[NSERVERS])
388 {
389         struct smbcli_state *cli[NSERVERS][NCONNECTIONS];
390         int fnum[NSERVERS][NCONNECTIONS][NFILES];
391         int n, i, n1, skip, r1, r2; 
392
393         ZERO_STRUCT(fnum);
394         ZERO_STRUCT(cli);
395
396         recorded = malloc_array_p(struct record, numops);
397
398         for (n=0; n<numops; n++) {
399 #if PRESETS
400                 if (n < sizeof(preset) / sizeof(preset[0])) {
401                         recorded[n] = preset[n];
402                 } else {
403 #endif
404                         recorded[n].conn = random() % NCONNECTIONS;
405                         recorded[n].f = random() % NFILES;
406                         recorded[n].start = lock_base + ((uint_t)random() % (lock_range-1));
407                         recorded[n].len =  min_length +
408                                 random() % (lock_range-(recorded[n].start-lock_base));
409                         recorded[n].start *= RANGE_MULTIPLE;
410                         recorded[n].len *= RANGE_MULTIPLE;
411                         recorded[n].pid = random()%3;
412                         if (recorded[n].pid == 2) {
413                                 recorded[n].pid = 0xFFFF; /* see if its magic */
414                         }
415                         r1 = random() % 100;
416                         r2 = random() % 100;
417                         if (r1 < READ_PCT) {
418                                 recorded[n].lock_type = READ_LOCK;
419                         } else {
420                                 recorded[n].lock_type = WRITE_LOCK;
421                         }
422                         if (r2 < LOCK_PCT) {
423                                 recorded[n].lock_op = OP_LOCK;
424                         } else if (r2 < UNLOCK_PCT) {
425                                 recorded[n].lock_op = OP_UNLOCK;
426                         } else {
427                                 recorded[n].lock_op = OP_REOPEN;
428                         }
429                         recorded[n].needed = True;
430                         if (!zero_zero && recorded[n].start==0 && recorded[n].len==0) {
431                                 recorded[n].len = 1;
432                         }
433 #if PRESETS
434                 }
435 #endif
436         }
437
438         reconnect(cli, fnum, share);
439         open_files(cli, fnum);
440         n = retest(cli, fnum, numops);
441
442         if (n == numops || !analyze) return;
443         n++;
444
445         skip = n/2;
446
447         while (1) {
448                 n1 = n;
449
450                 close_files(cli, fnum);
451                 reconnect(cli, fnum, share);
452                 open_files(cli, fnum);
453
454                 for (i=0;i<n-skip;i+=skip) {
455                         int m, j;
456                         printf("excluding %d-%d\n", i, i+skip-1);
457                         for (j=i;j<i+skip;j++) {
458                                 recorded[j].needed = False;
459                         }
460
461                         close_files(cli, fnum);
462                         open_files(cli, fnum);
463
464                         m = retest(cli, fnum, n);
465                         if (m == n) {
466                                 for (j=i;j<i+skip;j++) {
467                                         recorded[j].needed = True;
468                                 }
469                         } else {
470                                 if (i+(skip-1) < m) {
471                                         memmove(&recorded[i], &recorded[i+skip],
472                                                 (m-(i+skip-1))*sizeof(recorded[0]));
473                                 }
474                                 n = m-(skip-1);
475                                 i--;
476                         }
477                 }
478
479                 if (skip > 1) {
480                         skip = skip/2;
481                         printf("skip=%d\n", skip);
482                         continue;
483                 }
484
485                 if (n1 == n) break;
486         }
487
488         close_files(cli, fnum);
489         reconnect(cli, fnum, share);
490         open_files(cli, fnum);
491         showall = True;
492         n1 = retest(cli, fnum, n);
493         if (n1 != n-1) {
494                 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
495         }
496         close_files(cli, fnum);
497
498         for (i=0;i<n;i++) {
499                 printf("{%d, %d, %u, %u, %.0f, %.0f, %u},\n",
500                        recorded[i].lock_op,
501                        recorded[i].lock_type,
502                        recorded[i].conn,
503                        recorded[i].f,
504                        (double)recorded[i].start,
505                        (double)recorded[i].len,
506                        recorded[i].needed);
507         }       
508 }
509
510
511
512 static void usage(void)
513 {
514         printf(
515 "Usage:\n\
516   locktest //server1/share1 //server2/share2 [options..]\n\
517   options:\n\
518         -U user%%pass        (may be specified twice)\n\
519         -s seed\n\
520         -o numops\n\
521         -u          hide unlock fails\n\
522         -a          (show all ops)\n\
523         -A          analyse for minimal ops\n\
524         -O          use oplocks\n\
525         -E          enable exact error code checking\n\
526         -Z          enable the zero/zero lock\n\
527         -R range    set lock range\n\
528         -B base     set lock base\n\
529         -M min      set min lock length\n\
530         -l filename unclist file\n\
531 ");
532 }
533
534 /****************************************************************************
535   main program
536 ****************************************************************************/
537  int main(int argc,char *argv[])
538 {
539         char *share[NSERVERS];
540         int opt;
541         int seed, server;
542         int username_count=0;
543
544         setlinebuf(stdout);
545
546         setup_logging("locktest", DEBUG_STDOUT);
547
548         if (argc < 3 || argv[1][0] == '-') {
549                 usage();
550                 exit(1);
551         }
552
553         setup_logging(argv[0], DEBUG_STDOUT);
554
555         for (server=0;server<NSERVERS;server++) {
556                 share[server] = argv[1+server];
557                 all_string_sub(share[server],"/","\\",0);
558         }
559
560         argc -= NSERVERS;
561         argv += NSERVERS;
562
563         lp_load();
564
565         servers[0] = cli_credentials_init(talloc_autofree_context());
566         servers[1] = cli_credentials_init(talloc_autofree_context());
567         cli_credentials_guess(servers[0]);
568         cli_credentials_guess(servers[1]);
569
570         seed = time(NULL);
571
572         while ((opt = getopt(argc, argv, "U:s:ho:aAW:OR:B:M:EZW:l:")) != EOF) {
573                 switch (opt) {
574                 case 'U':
575                         if (username_count == 2) {
576                                 usage();
577                                 exit(1);
578                         }
579                         cli_credentials_parse_string(servers[username_count], 
580                                                      optarg, CRED_SPECIFIED);
581                         username_count++;
582                         break;
583                 case 'R':
584                         lock_range = strtol(optarg, NULL, 0);
585                         break;
586                 case 'B':
587                         lock_base = strtol(optarg, NULL, 0);
588                         break;
589                 case 'M':
590                         min_length = strtol(optarg, NULL, 0);
591                         break;
592                 case 's':
593                         seed = atoi(optarg);
594                         break;
595                 case 'u':
596                         hide_unlock_fails = True;
597                         break;
598                 case 'o':
599                         numops = atoi(optarg);
600                         break;
601                 case 'O':
602                         use_oplocks = True;
603                         break;
604                 case 'a':
605                         showall = True;
606                         break;
607                 case 'A':
608                         analyze = True;
609                         break;
610                 case 'Z':
611                         zero_zero = True;
612                         break;
613                 case 'E':
614                         exact_error_codes = True;
615                         break;
616                 case 'l':
617                         lp_set_cmdline("torture:unclist", optarg);
618                         break;
619                 case 'W':
620                         lp_set_cmdline("workgroup", optarg);
621                         break;
622                 case 'h':
623                         usage();
624                         exit(1);
625                 default:
626                         printf("Unknown option %c (%d)\n", (char)opt, opt);
627                         exit(1);
628                 }
629         }
630
631         if (username_count == 0) {
632                 usage();
633                 return -1;
634         }
635         if (username_count == 1) {
636                 servers[1] = servers[0];
637         }
638
639         gensec_init();
640
641         argc -= optind;
642         argv += optind;
643
644         DEBUG(0,("seed=%u base=%d range=%d min_length=%d\n", 
645                  seed, lock_base, lock_range, min_length));
646         srandom(seed);
647
648         test_locks(share);
649
650         return(0);
651 }
652