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