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