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