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