be2d3022da525848be8014ce8a6061dc519c297b
[samba.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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22 #include "system/filesys.h"
23 #include "system/time.h"
24 #include "pstring.h"
25 #include "auth/gensec/gensec.h"
26
27 static int numops = 1000;
28 static BOOL showall;
29 static BOOL analyze;
30 static BOOL hide_unlock_fails;
31 static BOOL use_oplocks;
32 static uint_t lock_range = 100;
33 static uint_t lock_base = 0;
34 static uint_t min_length = 0;
35 static BOOL exact_error_codes;
36 static BOOL zero_zero;
37
38 #define FILENAME "\\locktest.dat"
39
40 #define READ_PCT 50
41 #define LOCK_PCT 45
42 #define UNLOCK_PCT 70
43 #define RANGE_MULTIPLE 1
44 #define NSERVERS 2
45 #define NCONNECTIONS 2
46 #define NFILES 2
47 #define LOCK_TIMEOUT 0
48
49 #define NASTY_POSIX_LOCK_HACK 0
50
51 static struct cli_credentials *servers[NSERVERS];
52
53 enum lock_op {OP_LOCK, OP_UNLOCK, OP_REOPEN};
54
55 struct record {
56         enum lock_op lock_op;
57         enum brl_type lock_type;
58         char conn, f;
59         uint64_t start, len;
60         char needed;
61 };
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)
107 {
108         struct smbcli_state *c;
109         fstring server, myname;
110         NTSTATUS status;
111         int retries = 10;
112
113         fstrcpy(server,share+2);
114         share = strchr_m(server,'\\');
115         if (!share) return NULL;
116         *share = 0;
117         share++;
118
119         slprintf(myname,sizeof(myname), "lock-%u-%u", getpid(), snum);
120         cli_credentials_set_workstation(servers[snum], myname, CRED_SPECIFIED);
121
122         do {
123                 status = smbcli_full_connection(NULL, &c, 
124                                                 server, 
125                                                 share, NULL,
126                                                 servers[snum], NULL);
127                 if (!NT_STATUS_IS_OK(status)) {
128                         sleep(2);
129                 }
130         } while (!NT_STATUS_IS_OK(status) && retries--);
131
132         if (!NT_STATUS_IS_OK(status)) {
133                 return NULL;
134         }
135
136         return c;
137 }
138
139
140 static void reconnect(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], int fnum[NSERVERS][NCONNECTIONS][NFILES],
141                       char *share[NSERVERS])
142 {
143         int server, conn, f;
144
145         for (server=0;server<NSERVERS;server++)
146         for (conn=0;conn<NCONNECTIONS;conn++) {
147                 if (cli[server][conn]) {
148                         for (f=0;f<NFILES;f++) {
149                                 if (fnum[server][conn][f] != -1) {
150                                         smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
151                                         fnum[server][conn][f] = -1;
152                                 }
153                         }
154                         talloc_free(cli[server][conn]);
155                 }
156                 cli[server][conn] = connect_one(share[server], server);
157                 if (!cli[server][conn]) {
158                         DEBUG(0,("Failed to connect to %s\n", share[server]));
159                         exit(1);
160                 }
161         }
162 }
163
164
165
166 static BOOL test_one(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], 
167                      int fnum[NSERVERS][NCONNECTIONS][NFILES],
168                      struct record *rec)
169 {
170         uint_t conn = rec->conn;
171         uint_t f = rec->f;
172         uint64_t start = rec->start;
173         uint64_t len = rec->len;
174         enum brl_type op = rec->lock_type;
175         int server;
176         BOOL ret[NSERVERS];
177         NTSTATUS status[NSERVERS];
178
179         switch (rec->lock_op) {
180         case OP_LOCK:
181                 /* set a lock */
182                 for (server=0;server<NSERVERS;server++) {
183                         ret[server] = NT_STATUS_IS_OK(smbcli_lock64(cli[server][conn]->tree, 
184                                                  fnum[server][conn][f],
185                                                  start, len, LOCK_TIMEOUT, op));
186                         status[server] = smbcli_nt_error(cli[server][conn]->tree);
187                         if (!exact_error_codes && 
188                             NT_STATUS_EQUAL(status[server], 
189                                             NT_STATUS_FILE_LOCK_CONFLICT)) {
190                                 status[server] = NT_STATUS_LOCK_NOT_GRANTED;
191                         }
192                 }
193                 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) {
194                         printf("lock   conn=%u f=%u range=%.0f(%.0f) op=%s -> %s:%s\n",
195                                conn, f, 
196                                (double)start, (double)len,
197                                op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
198                                nt_errstr(status[0]), nt_errstr(status[1]));
199                 }
200                 if (!NT_STATUS_EQUAL(status[0],status[1])) return False;
201                 break;
202                 
203         case OP_UNLOCK:
204                 /* unset a lock */
205                 for (server=0;server<NSERVERS;server++) {
206                         ret[server] = NT_STATUS_IS_OK(smbcli_unlock64(cli[server][conn]->tree, 
207                                                    fnum[server][conn][f],
208                                                    start, len));
209                         status[server] = smbcli_nt_error(cli[server][conn]->tree);
210                 }
211                 if (showall || 
212                     (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))) {
213                         printf("unlock conn=%u f=%u range=%.0f(%.0f)       -> %s:%s\n",
214                                conn, f, 
215                                (double)start, (double)len,
216                                nt_errstr(status[0]), nt_errstr(status[1]));
217                 }
218                 if (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1])) 
219                         return False;
220                 break;
221
222         case OP_REOPEN:
223                 /* reopen the file */
224                 for (server=0;server<NSERVERS;server++) {
225                         smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
226                         fnum[server][conn][f] = -1;
227                 }
228                 for (server=0;server<NSERVERS;server++) {
229                         fnum[server][conn][f] = smbcli_open(cli[server][conn]->tree, FILENAME,
230                                                          O_RDWR|O_CREAT,
231                                                          DENY_NONE);
232                         if (fnum[server][conn][f] == -1) {
233                                 printf("failed to reopen on share%d\n", server);
234                                 return False;
235                         }
236                 }
237                 if (showall) {
238                         printf("reopen conn=%u f=%u\n",
239                                conn, f);
240                 }
241                 break;
242         }
243
244         return True;
245 }
246
247 static void close_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], 
248                         int fnum[NSERVERS][NCONNECTIONS][NFILES])
249 {
250         int server, conn, f; 
251
252         for (server=0;server<NSERVERS;server++)
253         for (conn=0;conn<NCONNECTIONS;conn++)
254         for (f=0;f<NFILES;f++) {
255                 if (fnum[server][conn][f] != -1) {
256                         smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
257                         fnum[server][conn][f] = -1;
258                 }
259         }
260         for (server=0;server<NSERVERS;server++) {
261                 smbcli_unlink(cli[server][0]->tree, FILENAME);
262         }
263 }
264
265 static void open_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], 
266                        int fnum[NSERVERS][NCONNECTIONS][NFILES])
267 {
268         int server, conn, f; 
269
270         for (server=0;server<NSERVERS;server++)
271         for (conn=0;conn<NCONNECTIONS;conn++)
272         for (f=0;f<NFILES;f++) {
273                 fnum[server][conn][f] = smbcli_open(cli[server][conn]->tree, FILENAME,
274                                                  O_RDWR|O_CREAT,
275                                                  DENY_NONE);
276                 if (fnum[server][conn][f] == -1) {
277                         fprintf(stderr,"Failed to open fnum[%u][%u][%u]\n",
278                                 server, conn, f);
279                         exit(1);
280                 }
281         }
282 }
283
284
285 static int retest(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], 
286                    int fnum[NSERVERS][NCONNECTIONS][NFILES],
287                    int n)
288 {
289         int i;
290         printf("testing %u ...\n", n);
291         for (i=0; i<n; i++) {
292                 if (i && i % 100 == 0) {
293                         printf("%u\n", i);
294                 }
295
296                 if (recorded[i].needed &&
297                     !test_one(cli, fnum, &recorded[i])) return i;
298         }
299         return n;
300 }
301
302
303 /* each server has two connections open to it. Each connection has two file
304    descriptors open on the file - 8 file descriptors in total 
305
306    we then do random locking ops in tamdem on the 4 fnums from each
307    server and ensure that the results match
308  */
309 static void test_locks(char *share[NSERVERS])
310 {
311         struct smbcli_state *cli[NSERVERS][NCONNECTIONS];
312         int fnum[NSERVERS][NCONNECTIONS][NFILES];
313         int n, i, n1, skip, r1, r2; 
314
315         ZERO_STRUCT(fnum);
316         ZERO_STRUCT(cli);
317
318         recorded = malloc_array_p(struct record, numops);
319
320         for (n=0; n<numops; n++) {
321 #if PRESETS
322                 if (n < sizeof(preset) / sizeof(preset[0])) {
323                         recorded[n] = preset[n];
324                 } else {
325 #endif
326                         recorded[n].conn = random() % NCONNECTIONS;
327                         recorded[n].f = random() % NFILES;
328                         recorded[n].start = lock_base + ((uint_t)random() % (lock_range-1));
329                         recorded[n].len =  min_length +
330                                 random() % (lock_range-(recorded[n].start-lock_base));
331                         recorded[n].start *= RANGE_MULTIPLE;
332                         recorded[n].len *= RANGE_MULTIPLE;
333                         r1 = random() % 100;
334                         r2 = random() % 100;
335                         if (r1 < READ_PCT) {
336                                 recorded[n].lock_type = READ_LOCK;
337                         } else {
338                                 recorded[n].lock_type = WRITE_LOCK;
339                         }
340                         if (r2 < LOCK_PCT) {
341                                 recorded[n].lock_op = OP_LOCK;
342                         } else if (r2 < UNLOCK_PCT) {
343                                 recorded[n].lock_op = OP_UNLOCK;
344                         } else {
345                                 recorded[n].lock_op = OP_REOPEN;
346                         }
347                         recorded[n].needed = True;
348                         if (!zero_zero && recorded[n].start==0 && recorded[n].len==0) {
349                                 recorded[n].len = 1;
350                         }
351 #if PRESETS
352                 }
353 #endif
354         }
355
356         reconnect(cli, fnum, share);
357         open_files(cli, fnum);
358         n = retest(cli, fnum, numops);
359
360         if (n == numops || !analyze) return;
361         n++;
362
363         skip = n/2;
364
365         while (1) {
366                 n1 = n;
367
368                 close_files(cli, fnum);
369                 reconnect(cli, fnum, share);
370                 open_files(cli, fnum);
371
372                 for (i=0;i<n-skip;i+=skip) {
373                         int m, j;
374                         printf("excluding %d-%d\n", i, i+skip-1);
375                         for (j=i;j<i+skip;j++) {
376                                 recorded[j].needed = False;
377                         }
378
379                         close_files(cli, fnum);
380                         open_files(cli, fnum);
381
382                         m = retest(cli, fnum, n);
383                         if (m == n) {
384                                 for (j=i;j<i+skip;j++) {
385                                         recorded[j].needed = True;
386                                 }
387                         } else {
388                                 if (i+(skip-1) < m) {
389                                         memmove(&recorded[i], &recorded[i+skip],
390                                                 (m-(i+skip-1))*sizeof(recorded[0]));
391                                 }
392                                 n = m-(skip-1);
393                                 i--;
394                         }
395                 }
396
397                 if (skip > 1) {
398                         skip = skip/2;
399                         printf("skip=%d\n", skip);
400                         continue;
401                 }
402
403                 if (n1 == n) break;
404         }
405
406         close_files(cli, fnum);
407         reconnect(cli, fnum, share);
408         open_files(cli, fnum);
409         showall = True;
410         n1 = retest(cli, fnum, n);
411         if (n1 != n-1) {
412                 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
413         }
414         close_files(cli, fnum);
415
416         for (i=0;i<n;i++) {
417                 printf("{%d, %d, %u, %u, %.0f, %.0f, %u},\n",
418                        recorded[i].lock_op,
419                        recorded[i].lock_type,
420                        recorded[i].conn,
421                        recorded[i].f,
422                        (double)recorded[i].start,
423                        (double)recorded[i].len,
424                        recorded[i].needed);
425         }       
426 }
427
428
429
430 static void usage(void)
431 {
432         printf(
433 "Usage:\n\
434   locktest //server1/share1 //server2/share2 [options..]\n\
435   options:\n\
436         -U user%%pass        (may be specified twice)\n\
437         -s seed\n\
438         -o numops\n\
439         -u          hide unlock fails\n\
440         -a          (show all ops)\n\
441         -A          analyse for minimal ops\n\
442         -O          use oplocks\n\
443         -E          enable exact error code checking\n\
444         -Z          enable the zero/zero lock\n\
445         -R range    set lock range\n\
446         -B base     set lock base\n\
447         -M min      set min lock length\n\
448 ");
449 }
450
451 /****************************************************************************
452   main program
453 ****************************************************************************/
454  int main(int argc,char *argv[])
455 {
456         char *share[NSERVERS];
457         int opt;
458         int seed, server;
459         int username_count=0;
460
461         setlinebuf(stdout);
462
463         setup_logging("locktest", DEBUG_STDOUT);
464
465         if (argc < 3 || argv[1][0] == '-') {
466                 usage();
467                 exit(1);
468         }
469
470         setup_logging(argv[0], DEBUG_STDOUT);
471
472         for (server=0;server<NSERVERS;server++) {
473                 share[server] = argv[1+server];
474                 all_string_sub(share[server],"/","\\",0);
475         }
476
477         argc -= NSERVERS;
478         argv += NSERVERS;
479
480         lp_load();
481
482         servers[0] = cli_credentials_init(talloc_autofree_context());
483         servers[1] = cli_credentials_init(talloc_autofree_context());
484         cli_credentials_guess(servers[0]);
485         cli_credentials_guess(servers[1]);
486
487         seed = time(NULL);
488
489         while ((opt = getopt(argc, argv, "U:s:ho:aAW:OR:B:M:EZW:")) != EOF) {
490                 switch (opt) {
491                 case 'U':
492                         if (username_count == 2) {
493                                 usage();
494                                 exit(1);
495                         }
496                         cli_credentials_parse_string(servers[username_count], 
497                                                      optarg, CRED_SPECIFIED);
498                         username_count++;
499                         break;
500                 case 'R':
501                         lock_range = strtol(optarg, NULL, 0);
502                         break;
503                 case 'B':
504                         lock_base = strtol(optarg, NULL, 0);
505                         break;
506                 case 'M':
507                         min_length = strtol(optarg, NULL, 0);
508                         break;
509                 case 's':
510                         seed = atoi(optarg);
511                         break;
512                 case 'u':
513                         hide_unlock_fails = True;
514                         break;
515                 case 'o':
516                         numops = atoi(optarg);
517                         break;
518                 case 'O':
519                         use_oplocks = True;
520                         break;
521                 case 'a':
522                         showall = True;
523                         break;
524                 case 'A':
525                         analyze = True;
526                         break;
527                 case 'Z':
528                         zero_zero = True;
529                         break;
530                 case 'E':
531                         exact_error_codes = True;
532                         break;
533                 case 'W':
534                         lp_set_cmdline("workgroup", optarg);
535                         break;
536                 case 'h':
537                         usage();
538                         exit(1);
539                 default:
540                         printf("Unknown option %c (%d)\n", (char)opt, opt);
541                         exit(1);
542                 }
543         }
544
545         if (username_count == 0) {
546                 usage();
547                 return -1;
548         }
549         if (username_count == 1) {
550                 servers[1] = servers[0];
551         }
552
553         gensec_init();
554
555         argc -= optind;
556         argv += optind;
557
558         DEBUG(0,("seed=%u base=%d range=%d min_length=%d\n", 
559                  seed, lock_base, lock_range, min_length));
560         srandom(seed);
561
562         test_locks(share);
563
564         return(0);
565 }
566