This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.
[sfrench/samba-autobuild/.git] / source / torture / locktest2.c
1 /* 
2    Unix SMB/CIFS implementation.
3    byte range lock tester - with local filesystem support
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 #define NO_SYSLOG
22
23 #include "includes.h"
24
25 static fstring password;
26 static fstring username;
27 static int got_pass;
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
34 #define FILENAME "\\locktest.dat"
35 #define LOCKRANGE 100
36 #define LOCKBASE 0
37
38 /*
39 #define LOCKBASE (0x40000000 - 50)
40 */
41
42 #define READ_PCT 50
43 #define LOCK_PCT 25
44 #define UNLOCK_PCT 65
45 #define RANGE_MULTIPLE 1
46
47 #define NSERVERS 2
48 #define NCONNECTIONS 2
49 #define NUMFSTYPES 2
50 #define NFILES 2
51 #define LOCK_TIMEOUT 0
52
53 #define FSTYPE_SMB 0
54 #define FSTYPE_NFS 1
55
56 struct record {
57         char r1, r2;
58         char conn, f, fstype;
59         unsigned start, len;
60         char needed;
61 };
62
63 static struct record *recorded;
64
65 static int try_open(struct cli_state *c, char *nfs, int fstype, char *fname, int flags)
66 {
67         pstring path;
68
69         switch (fstype) {
70         case FSTYPE_SMB:
71                 return cli_open(c, fname, flags, DENY_NONE);
72
73         case FSTYPE_NFS:
74                 slprintf(path, sizeof(path), "%s%s", nfs, fname);
75                 pstring_sub(path,"\\", "/");
76                 return open(path, flags, 0666);
77         }
78
79         return -1;
80 }
81
82 static BOOL try_close(struct cli_state *c, int fstype, int fd)
83 {
84         switch (fstype) {
85         case FSTYPE_SMB:
86                 return cli_close(c, fd);
87
88         case FSTYPE_NFS:
89                 return close(fd) == 0;
90         }
91
92         return False;
93 }
94
95 static BOOL try_lock(struct cli_state *c, int fstype, 
96                      int fd, unsigned start, unsigned len,
97                      enum brl_type op)
98 {
99         struct flock lock;
100
101         switch (fstype) {
102         case FSTYPE_SMB:
103                 return cli_lock(c, fd, start, len, LOCK_TIMEOUT, op);
104
105         case FSTYPE_NFS:
106                 lock.l_type = (op==READ_LOCK) ? F_RDLCK:F_WRLCK;
107                 lock.l_whence = SEEK_SET;
108                 lock.l_start = start;
109                 lock.l_len = len;
110                 lock.l_pid = getpid();
111                 return fcntl(fd,F_SETLK,&lock) == 0;
112         }
113
114         return False;
115 }
116
117 static BOOL try_unlock(struct cli_state *c, int fstype, 
118                        int fd, unsigned start, unsigned len)
119 {
120         struct flock lock;
121
122         switch (fstype) {
123         case FSTYPE_SMB:
124                 return cli_unlock(c, fd, start, len);
125
126         case FSTYPE_NFS:
127                 lock.l_type = F_UNLCK;
128                 lock.l_whence = SEEK_SET;
129                 lock.l_start = start;
130                 lock.l_len = len;
131                 lock.l_pid = getpid();
132                 return fcntl(fd,F_SETLK,&lock) == 0;
133         }
134
135         return False;
136 }       
137
138 static void print_brl(SMB_DEV_T dev, SMB_INO_T ino, int pid, 
139                       enum brl_type lock_type,
140                       br_off start, br_off size)
141 {
142         printf("%6d   %05x:%05x    %s  %.0f:%.0f(%.0f)\n", 
143                (int)pid, (int)dev, (int)ino, 
144                lock_type==READ_LOCK?"R":"W",
145                (double)start, (double)start+size-1,(double)size);
146
147 }
148
149 /***************************************************** 
150 return a connection to a server
151 *******************************************************/
152 struct cli_state *connect_one(char *share)
153 {
154         struct cli_state *c;
155         struct nmb_name called, calling;
156         char *server_n;
157         fstring server;
158         struct in_addr ip;
159         fstring myname;
160         static int count;
161
162         fstrcpy(server,share+2);
163         share = strchr_m(server,'\\');
164         if (!share) return NULL;
165         *share = 0;
166         share++;
167
168         server_n = server;
169         
170         zero_ip(&ip);
171
172         slprintf(myname,sizeof(myname), "lock-%u-%u", getpid(), count++);
173
174         make_nmb_name(&calling, myname, 0x0);
175         make_nmb_name(&called , server, 0x20);
176
177  again:
178         zero_ip(&ip);
179
180         /* have to open a new connection */
181         if (!(c=cli_initialise(NULL)) || !cli_connect(c, server_n, &ip)) {
182                 DEBUG(0,("Connection to %s failed\n", server_n));
183                 return NULL;
184         }
185
186         if (!cli_session_request(c, &calling, &called)) {
187                 DEBUG(0,("session request to %s failed\n", called.name));
188                 cli_shutdown(c);
189                 if (strcmp(called.name, "*SMBSERVER")) {
190                         make_nmb_name(&called , "*SMBSERVER", 0x20);
191                         goto again;
192                 }
193                 return NULL;
194         }
195
196         DEBUG(4,(" session request ok\n"));
197
198         if (!cli_negprot(c)) {
199                 DEBUG(0,("protocol negotiation failed\n"));
200                 cli_shutdown(c);
201                 return NULL;
202         }
203
204         if (!got_pass) {
205                 char *pass = getpass("Password: ");
206                 if (pass) {
207                         pstrcpy(password, pass);
208                 }
209         }
210
211         if (!cli_session_setup(c, username, 
212                                password, strlen(password),
213                                password, strlen(password),
214                                lp_workgroup())) {
215                 DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
216                 return NULL;
217         }
218
219         /*
220          * These next two lines are needed to emulate
221          * old client behaviour for people who have
222          * scripts based on client output.
223          * QUESTION ? Do we want to have a 'client compatibility
224          * mode to turn these on/off ? JRA.
225          */
226
227         if (*c->server_domain || *c->server_os || *c->server_type)
228                 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
229                         c->server_domain,c->server_os,c->server_type));
230         
231         DEBUG(4,(" session setup ok\n"));
232
233         if (!cli_send_tconX(c, share, "?????",
234                             password, strlen(password)+1)) {
235                 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c)));
236                 cli_shutdown(c);
237                 return NULL;
238         }
239
240         DEBUG(4,(" tconx ok\n"));
241
242         c->use_oplocks = use_oplocks;
243
244         return c;
245 }
246
247
248 static void reconnect(struct cli_state *cli[NSERVERS][NCONNECTIONS], 
249                       char *nfs[NSERVERS], 
250                       int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
251                       char *share1, char *share2)
252 {
253         int server, conn, f, fstype;
254         char *share[2];
255         share[0] = share1;
256         share[1] = share2;
257
258         fstype = FSTYPE_SMB;
259
260         for (server=0;server<NSERVERS;server++)
261         for (conn=0;conn<NCONNECTIONS;conn++) {
262                 if (cli[server][conn]) {
263                         for (f=0;f<NFILES;f++) {
264                                 cli_close(cli[server][conn], fnum[server][fstype][conn][f]);
265                         }
266                         cli_ulogoff(cli[server][conn]);
267                         cli_shutdown(cli[server][conn]);
268                 }
269                 cli[server][conn] = connect_one(share[server]);
270                 if (!cli[server][conn]) {
271                         DEBUG(0,("Failed to connect to %s\n", share[server]));
272                         exit(1);
273                 }
274         }
275 }
276
277
278
279 static BOOL test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS], 
280                      char *nfs[NSERVERS],
281                      int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
282                      struct record *rec)
283 {
284         unsigned conn = rec->conn;
285         unsigned f = rec->f;
286         unsigned fstype = rec->fstype;
287         unsigned start = rec->start;
288         unsigned len = rec->len;
289         unsigned r1 = rec->r1;
290         unsigned r2 = rec->r2;
291         enum brl_type op;
292         int server;
293         BOOL ret[NSERVERS];
294
295         if (r1 < READ_PCT) {
296                 op = READ_LOCK; 
297         } else {
298                 op = WRITE_LOCK; 
299         }
300
301         if (r2 < LOCK_PCT) {
302                 /* set a lock */
303                 for (server=0;server<NSERVERS;server++) {
304                         ret[server] = try_lock(cli[server][conn], fstype,
305                                                fnum[server][fstype][conn][f],
306                                                start, len, op);
307                 }
308                 if (showall || ret[0] != ret[1]) {
309                         printf("lock   conn=%u fstype=%u f=%u range=%u:%u(%u) op=%s -> %u:%u\n",
310                                conn, fstype, f, 
311                                start, start+len-1, len,
312                                op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
313                                ret[0], ret[1]);
314                 }
315                 if (showall) brl_forall(print_brl);
316                 if (ret[0] != ret[1]) return False;
317         } else if (r2 < LOCK_PCT+UNLOCK_PCT) {
318                 /* unset a lock */
319                 for (server=0;server<NSERVERS;server++) {
320                         ret[server] = try_unlock(cli[server][conn], fstype,
321                                                  fnum[server][fstype][conn][f],
322                                                  start, len);
323                 }
324                 if (showall || (!hide_unlock_fails && (ret[0] != ret[1]))) {
325                         printf("unlock conn=%u fstype=%u f=%u range=%u:%u(%u)       -> %u:%u\n",
326                                conn, fstype, f, 
327                                start, start+len-1, len,
328                                ret[0], ret[1]);
329                 }
330                 if (showall) brl_forall(print_brl);
331                 if (!hide_unlock_fails && ret[0] != ret[1]) return False;
332         } else {
333                 /* reopen the file */
334                 for (server=0;server<NSERVERS;server++) {
335                         try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
336                         fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
337                                                                  O_RDWR|O_CREAT);
338                         if (fnum[server][fstype][conn][f] == -1) {
339                                 printf("failed to reopen on share1\n");
340                                 return False;
341                         }
342                 }
343                 if (showall) {
344                         printf("reopen conn=%u fstype=%u f=%u\n",
345                                conn, fstype, f);
346                         brl_forall(print_brl);
347                 }
348         }
349         return True;
350 }
351
352 static void close_files(struct cli_state *cli[NSERVERS][NCONNECTIONS], 
353                         char *nfs[NSERVERS],
354                         int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
355 {
356         int server, conn, f, fstype; 
357
358         for (server=0;server<NSERVERS;server++)
359         for (fstype=0;fstype<NUMFSTYPES;fstype++)
360         for (conn=0;conn<NCONNECTIONS;conn++)
361         for (f=0;f<NFILES;f++) {
362                 if (fnum[server][fstype][conn][f] != -1) {
363                         try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
364                         fnum[server][fstype][conn][f] = -1;
365                 }
366         }
367         for (server=0;server<NSERVERS;server++) {
368                 cli_unlink(cli[server][0], FILENAME);
369         }
370 }
371
372 static void open_files(struct cli_state *cli[NSERVERS][NCONNECTIONS], 
373                        char *nfs[NSERVERS],
374                        int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
375 {
376         int server, fstype, conn, f; 
377
378         for (server=0;server<NSERVERS;server++)
379         for (fstype=0;fstype<NUMFSTYPES;fstype++)
380         for (conn=0;conn<NCONNECTIONS;conn++)
381         for (f=0;f<NFILES;f++) {
382                 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
383                                                          O_RDWR|O_CREAT);
384                 if (fnum[server][fstype][conn][f] == -1) {
385                         fprintf(stderr,"Failed to open fnum[%u][%u][%u][%u]\n",
386                                 server, fstype, conn, f);
387                         exit(1);
388                 }
389         }
390 }
391
392
393 static int retest(struct cli_state *cli[NSERVERS][NCONNECTIONS], 
394                   char *nfs[NSERVERS],
395                   int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
396                   int n)
397 {
398         int i;
399         printf("testing %u ...\n", n);
400         for (i=0; i<n; i++) {
401                 if (i && i % 100 == 0) {
402                         printf("%u\n", i);
403                 }
404
405                 if (recorded[i].needed &&
406                     !test_one(cli, nfs, fnum, &recorded[i])) return i;
407         }
408         return n;
409 }
410
411
412 /* each server has two connections open to it. Each connection has two file
413    descriptors open on the file - 8 file descriptors in total 
414
415    we then do random locking ops in tamdem on the 4 fnums from each
416    server and ensure that the results match
417  */
418 static void test_locks(char *share1, char *share2, char *nfspath1, char *nfspath2)
419 {
420         struct cli_state *cli[NSERVERS][NCONNECTIONS];
421         char *nfs[NSERVERS];
422         int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES];
423         int n, i, n1; 
424
425         nfs[0] = nfspath1;
426         nfs[1] = nfspath2;
427
428         ZERO_STRUCT(fnum);
429         ZERO_STRUCT(cli);
430
431         recorded = (struct record *)malloc(sizeof(*recorded) * numops);
432
433         for (n=0; n<numops; n++) {
434                 recorded[n].conn = random() % NCONNECTIONS;
435                 recorded[n].fstype = random() % NUMFSTYPES;
436                 recorded[n].f = random() % NFILES;
437                 recorded[n].start = LOCKBASE + ((unsigned)random() % (LOCKRANGE-1));
438                 recorded[n].len = 1 + 
439                         random() % (LOCKRANGE-(recorded[n].start-LOCKBASE));
440                 recorded[n].start *= RANGE_MULTIPLE;
441                 recorded[n].len *= RANGE_MULTIPLE;
442                 recorded[n].r1 = random() % 100;
443                 recorded[n].r2 = random() % 100;
444                 recorded[n].needed = True;
445         }
446
447         reconnect(cli, nfs, fnum, share1, share2);
448         open_files(cli, nfs, fnum);
449         n = retest(cli, nfs, fnum, numops);
450
451         if (n == numops || !analyze) return;
452         n++;
453
454         while (1) {
455                 n1 = n;
456
457                 close_files(cli, nfs, fnum);
458                 reconnect(cli, nfs, fnum, share1, share2);
459                 open_files(cli, nfs, fnum);
460
461                 for (i=0;i<n-1;i++) {
462                         int m;
463                         recorded[i].needed = False;
464
465                         close_files(cli, nfs, fnum);
466                         open_files(cli, nfs, fnum);
467
468                         m = retest(cli, nfs, fnum, n);
469                         if (m == n) {
470                                 recorded[i].needed = True;
471                         } else {
472                                 if (i < m) {
473                                         memmove(&recorded[i], &recorded[i+1],
474                                                 (m-i)*sizeof(recorded[0]));
475                                 }
476                                 n = m;
477                                 i--;
478                         }
479                 }
480
481                 if (n1 == n) break;
482         }
483
484         close_files(cli, nfs, fnum);
485         reconnect(cli, nfs, fnum, share1, share2);
486         open_files(cli, nfs, fnum);
487         showall = True;
488         n1 = retest(cli, nfs, fnum, n);
489         if (n1 != n-1) {
490                 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
491         }
492         close_files(cli, nfs, fnum);
493
494         for (i=0;i<n;i++) {
495                 printf("{%u, %u, %u, %u, %u, %u, %u, %u},\n",
496                        recorded[i].r1,
497                        recorded[i].r2,
498                        recorded[i].conn,
499                        recorded[i].fstype,
500                        recorded[i].f,
501                        recorded[i].start,
502                        recorded[i].len,
503                        recorded[i].needed);
504         }       
505 }
506
507
508
509 static void usage(void)
510 {
511         printf(
512 "Usage:\n\
513   locktest //server1/share1 //server2/share2 /path1 /path2 [options..]\n\
514   options:\n\
515         -U user%%pass\n\
516         -s seed\n\
517         -o numops\n\
518         -u          hide unlock fails\n\
519         -a          (show all ops)\n\
520         -O          use oplocks\n\
521 ");
522 }
523
524 /****************************************************************************
525   main program
526 ****************************************************************************/
527  int main(int argc,char *argv[])
528 {
529         char *share1, *share2, *nfspath1, *nfspath2;
530         extern char *optarg;
531         extern int optind;
532         int opt;
533         char *p;
534         int seed;
535
536         setlinebuf(stdout);
537
538         dbf = x_stderr;
539
540         if (argc < 5 || argv[1][0] == '-') {
541                 usage();
542                 exit(1);
543         }
544
545         share1 = argv[1];
546         share2 = argv[2];
547         nfspath1 = argv[3];
548         nfspath2 = argv[4];
549
550         all_string_sub(share1,"/","\\",0);
551         all_string_sub(share2,"/","\\",0);
552
553         setup_logging(argv[0],True);
554
555         argc -= 4;
556         argv += 4;
557
558         lp_load(dyn_CONFIGFILE,True,False,False);
559         load_interfaces();
560
561         if (getenv("USER")) {
562                 pstrcpy(username,getenv("USER"));
563         }
564
565         seed = time(NULL);
566
567         while ((opt = getopt(argc, argv, "U:s:ho:aAW:O")) != EOF) {
568                 switch (opt) {
569                 case 'U':
570                         pstrcpy(username,optarg);
571                         p = strchr_m(username,'%');
572                         if (p) {
573                                 *p = 0;
574                                 pstrcpy(password, p+1);
575                                 got_pass = 1;
576                         }
577                         break;
578                 case 's':
579                         seed = atoi(optarg);
580                         break;
581                 case 'u':
582                         hide_unlock_fails = True;
583                         break;
584                 case 'o':
585                         numops = atoi(optarg);
586                         break;
587                 case 'O':
588                         use_oplocks = True;
589                         break;
590                 case 'a':
591                         showall = True;
592                         break;
593                 case 'A':
594                         analyze = True;
595                         break;
596                 case 'h':
597                         usage();
598                         exit(1);
599                 default:
600                         printf("Unknown option %c (%d)\n", (char)opt, opt);
601                         exit(1);
602                 }
603         }
604
605         argc -= optind;
606         argv += optind;
607
608         DEBUG(0,("seed=%u\n", seed));
609         srandom(seed);
610
611         locking_init(1);
612         test_locks(share1, share2, nfspath1, nfspath2);
613
614         return(0);
615 }