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