r3441: some include file cleanups and general housekeeping
[ab/samba.git/.git] / source4 / 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 #include "includes.h"
22
23 static fstring password;
24 static fstring username;
25 static int got_pass;
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
32 #define FILENAME "\\locktest.dat"
33 #define LOCKRANGE 100
34 #define LOCKBASE 0
35
36 /*
37 #define LOCKBASE (0x40000000 - 50)
38 */
39
40 #define READ_PCT 50
41 #define LOCK_PCT 25
42 #define UNLOCK_PCT 65
43 #define RANGE_MULTIPLE 1
44
45 #define NSERVERS 2
46 #define NCONNECTIONS 2
47 #define NUMFSTYPES 2
48 #define NFILES 2
49 #define LOCK_TIMEOUT 0
50
51 #define FSTYPE_SMB 0
52 #define FSTYPE_NFS 1
53
54 struct record {
55         char r1, r2;
56         char conn, f, fstype;
57         uint_t start, len;
58         char needed;
59 };
60
61 static struct record *recorded;
62
63 static int try_open(struct smbcli_state *c, char *nfs, int fstype, const char *fname, int flags)
64 {
65         pstring path;
66
67         switch (fstype) {
68         case FSTYPE_SMB:
69                 return smbcli_open(c, fname, flags, DENY_NONE);
70
71         case FSTYPE_NFS:
72                 slprintf(path, sizeof(path), "%s%s", nfs, fname);
73                 pstring_sub(path,"\\", "/");
74                 return open(path, flags, 0666);
75         }
76
77         return -1;
78 }
79
80 static BOOL try_close(struct smbcli_state *c, int fstype, int fd)
81 {
82         switch (fstype) {
83         case FSTYPE_SMB:
84                 return smbcli_close(c, fd);
85
86         case FSTYPE_NFS:
87                 return close(fd) == 0;
88         }
89
90         return False;
91 }
92
93 static BOOL try_lock(struct smbcli_state *c, int fstype, 
94                      int fd, uint_t start, uint_t len,
95                      enum brl_type op)
96 {
97         struct flock lock;
98
99         switch (fstype) {
100         case FSTYPE_SMB:
101                 return smbcli_lock(c, fd, start, len, LOCK_TIMEOUT, op);
102
103         case FSTYPE_NFS:
104                 lock.l_type = (op==READ_LOCK) ? F_RDLCK:F_WRLCK;
105                 lock.l_whence = SEEK_SET;
106                 lock.l_start = start;
107                 lock.l_len = len;
108                 lock.l_pid = getpid();
109                 return fcntl(fd,F_SETLK,&lock) == 0;
110         }
111
112         return False;
113 }
114
115 static BOOL try_unlock(struct smbcli_state *c, int fstype, 
116                        int fd, uint_t start, uint_t len)
117 {
118         struct flock lock;
119
120         switch (fstype) {
121         case FSTYPE_SMB:
122                 return smbcli_unlock(c, fd, start, len);
123
124         case FSTYPE_NFS:
125                 lock.l_type = F_UNLCK;
126                 lock.l_whence = SEEK_SET;
127                 lock.l_start = start;
128                 lock.l_len = len;
129                 lock.l_pid = getpid();
130                 return fcntl(fd,F_SETLK,&lock) == 0;
131         }
132
133         return False;
134 }       
135
136 /***************************************************** 
137 return a connection to a server
138 *******************************************************/
139 static struct smbcli_state *connect_one(char *share)
140 {
141         struct smbcli_state *c;
142         char *server_n;
143         fstring server;
144         fstring myname;
145         static int count;
146         NTSTATUS nt_status;
147
148         fstrcpy(server,share+2);
149         share = strchr_m(server,'\\');
150         if (!share) return NULL;
151         *share = 0;
152         share++;
153
154         server_n = server;
155         
156         if (!got_pass) {
157                 char *pass = getpass("Password: ");
158                 if (pass) {
159                         fstrcpy(password, pass);
160                 }
161         }
162
163         slprintf(myname,sizeof(myname), "lock-%u-%u", getpid(), count++);
164
165         nt_status = smbcli_full_connection(NULL, 
166                                            &c, myname, server_n, NULL, 0, share, "?????", 
167                                            username, lp_workgroup(), password, 0,
168                                            NULL);
169
170         if (!NT_STATUS_IS_OK(nt_status)) {
171                 DEBUG(0, ("smbcli_full_connection failed with error %s\n", nt_errstr(nt_status)));
172                 return NULL;
173         }
174
175         c->use_oplocks = use_oplocks;
176
177         return c;
178 }
179
180
181 static void reconnect(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], 
182                       char *nfs[NSERVERS], 
183                       int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
184                       char *share1, char *share2)
185 {
186         int server, conn, f, fstype;
187         char *share[2];
188         share[0] = share1;
189         share[1] = share2;
190
191         fstype = FSTYPE_SMB;
192
193         for (server=0;server<NSERVERS;server++)
194         for (conn=0;conn<NCONNECTIONS;conn++) {
195                 if (cli[server][conn]) {
196                         for (f=0;f<NFILES;f++) {
197                                 smbcli_close(cli[server][conn], fnum[server][fstype][conn][f]);
198                         }
199                         smbcli_ulogoff(cli[server][conn]);
200                         smbcli_shutdown(cli[server][conn]);
201                 }
202                 cli[server][conn] = connect_one(share[server]);
203                 if (!cli[server][conn]) {
204                         DEBUG(0,("Failed to connect to %s\n", share[server]));
205                         exit(1);
206                 }
207         }
208 }
209
210
211
212 static BOOL test_one(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], 
213                      char *nfs[NSERVERS],
214                      int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
215                      struct record *rec)
216 {
217         uint_t conn = rec->conn;
218         uint_t f = rec->f;
219         uint_t fstype = rec->fstype;
220         uint_t start = rec->start;
221         uint_t len = rec->len;
222         uint_t r1 = rec->r1;
223         uint_t r2 = rec->r2;
224         enum brl_type op;
225         int server;
226         BOOL ret[NSERVERS];
227
228         if (r1 < READ_PCT) {
229                 op = READ_LOCK; 
230         } else {
231                 op = WRITE_LOCK; 
232         }
233
234         if (r2 < LOCK_PCT) {
235                 /* set a lock */
236                 for (server=0;server<NSERVERS;server++) {
237                         ret[server] = try_lock(cli[server][conn], fstype,
238                                                fnum[server][fstype][conn][f],
239                                                start, len, op);
240                 }
241                 if (showall || ret[0] != ret[1]) {
242                         printf("lock   conn=%u fstype=%u f=%u range=%u:%u(%u) op=%s -> %u:%u\n",
243                                conn, fstype, f, 
244                                start, start+len-1, len,
245                                op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
246                                ret[0], ret[1]);
247                 }
248                 if (ret[0] != ret[1]) return False;
249         } else if (r2 < LOCK_PCT+UNLOCK_PCT) {
250                 /* unset a lock */
251                 for (server=0;server<NSERVERS;server++) {
252                         ret[server] = try_unlock(cli[server][conn], fstype,
253                                                  fnum[server][fstype][conn][f],
254                                                  start, len);
255                 }
256                 if (showall || (!hide_unlock_fails && (ret[0] != ret[1]))) {
257                         printf("unlock conn=%u fstype=%u f=%u range=%u:%u(%u)       -> %u:%u\n",
258                                conn, fstype, f, 
259                                start, start+len-1, len,
260                                ret[0], ret[1]);
261                 }
262                 if (!hide_unlock_fails && ret[0] != ret[1]) return False;
263         } else {
264                 /* reopen the file */
265                 for (server=0;server<NSERVERS;server++) {
266                         try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
267                         fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
268                                                                  O_RDWR|O_CREAT);
269                         if (fnum[server][fstype][conn][f] == -1) {
270                                 printf("failed to reopen on share1\n");
271                                 return False;
272                         }
273                 }
274                 if (showall) {
275                         printf("reopen conn=%u fstype=%u f=%u\n",
276                                conn, fstype, f);
277                 }
278         }
279         return True;
280 }
281
282 static void close_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], 
283                         char *nfs[NSERVERS],
284                         int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
285 {
286         int server, conn, f, fstype; 
287
288         for (server=0;server<NSERVERS;server++)
289         for (fstype=0;fstype<NUMFSTYPES;fstype++)
290         for (conn=0;conn<NCONNECTIONS;conn++)
291         for (f=0;f<NFILES;f++) {
292                 if (fnum[server][fstype][conn][f] != -1) {
293                         try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
294                         fnum[server][fstype][conn][f] = -1;
295                 }
296         }
297         for (server=0;server<NSERVERS;server++) {
298                 smbcli_unlink(cli[server][0], FILENAME);
299         }
300 }
301
302 static void open_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], 
303                        char *nfs[NSERVERS],
304                        int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
305 {
306         int server, fstype, conn, f; 
307
308         for (server=0;server<NSERVERS;server++)
309         for (fstype=0;fstype<NUMFSTYPES;fstype++)
310         for (conn=0;conn<NCONNECTIONS;conn++)
311         for (f=0;f<NFILES;f++) {
312                 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
313                                                          O_RDWR|O_CREAT);
314                 if (fnum[server][fstype][conn][f] == -1) {
315                         fprintf(stderr,"Failed to open fnum[%u][%u][%u][%u]\n",
316                                 server, fstype, conn, f);
317                         exit(1);
318                 }
319         }
320 }
321
322
323 static int retest(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], 
324                   char *nfs[NSERVERS],
325                   int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
326                   int n)
327 {
328         int i;
329         printf("testing %u ...\n", n);
330         for (i=0; i<n; i++) {
331                 if (i && i % 100 == 0) {
332                         printf("%u\n", i);
333                 }
334
335                 if (recorded[i].needed &&
336                     !test_one(cli, nfs, fnum, &recorded[i])) return i;
337         }
338         return n;
339 }
340
341
342 /* each server has two connections open to it. Each connection has two file
343    descriptors open on the file - 8 file descriptors in total 
344
345    we then do random locking ops in tamdem on the 4 fnums from each
346    server and ensure that the results match
347  */
348 static void test_locks(char *share1, char *share2, char *nfspath1, char *nfspath2)
349 {
350         struct smbcli_state *cli[NSERVERS][NCONNECTIONS];
351         char *nfs[NSERVERS];
352         int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES];
353         int n, i, n1; 
354
355         nfs[0] = nfspath1;
356         nfs[1] = nfspath2;
357
358         ZERO_STRUCT(fnum);
359         ZERO_STRUCT(cli);
360
361         recorded = (struct record *)malloc(sizeof(*recorded) * numops);
362
363         for (n=0; n<numops; n++) {
364                 recorded[n].conn = random() % NCONNECTIONS;
365                 recorded[n].fstype = random() % NUMFSTYPES;
366                 recorded[n].f = random() % NFILES;
367                 recorded[n].start = LOCKBASE + ((uint_t)random() % (LOCKRANGE-1));
368                 recorded[n].len = 1 + 
369                         random() % (LOCKRANGE-(recorded[n].start-LOCKBASE));
370                 recorded[n].start *= RANGE_MULTIPLE;
371                 recorded[n].len *= RANGE_MULTIPLE;
372                 recorded[n].r1 = random() % 100;
373                 recorded[n].r2 = random() % 100;
374                 recorded[n].needed = True;
375         }
376
377         reconnect(cli, nfs, fnum, share1, share2);
378         open_files(cli, nfs, fnum);
379         n = retest(cli, nfs, fnum, numops);
380
381         if (n == numops || !analyze) return;
382         n++;
383
384         while (1) {
385                 n1 = n;
386
387                 close_files(cli, nfs, fnum);
388                 reconnect(cli, nfs, fnum, share1, share2);
389                 open_files(cli, nfs, fnum);
390
391                 for (i=0;i<n-1;i++) {
392                         int m;
393                         recorded[i].needed = False;
394
395                         close_files(cli, nfs, fnum);
396                         open_files(cli, nfs, fnum);
397
398                         m = retest(cli, nfs, fnum, n);
399                         if (m == n) {
400                                 recorded[i].needed = True;
401                         } else {
402                                 if (i < m) {
403                                         memmove(&recorded[i], &recorded[i+1],
404                                                 (m-i)*sizeof(recorded[0]));
405                                 }
406                                 n = m;
407                                 i--;
408                         }
409                 }
410
411                 if (n1 == n) break;
412         }
413
414         close_files(cli, nfs, fnum);
415         reconnect(cli, nfs, fnum, share1, share2);
416         open_files(cli, nfs, fnum);
417         showall = True;
418         n1 = retest(cli, nfs, fnum, n);
419         if (n1 != n-1) {
420                 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
421         }
422         close_files(cli, nfs, fnum);
423
424         for (i=0;i<n;i++) {
425                 printf("{%u, %u, %u, %u, %u, %u, %u, %u},\n",
426                        recorded[i].r1,
427                        recorded[i].r2,
428                        recorded[i].conn,
429                        recorded[i].fstype,
430                        recorded[i].f,
431                        recorded[i].start,
432                        recorded[i].len,
433                        recorded[i].needed);
434         }       
435 }
436
437
438
439 static void usage(void)
440 {
441         printf(
442 "Usage:\n\
443   locktest //server1/share1 //server2/share2 /path1 /path2 [options..]\n\
444   options:\n\
445         -U user%%pass\n\
446         -s seed\n\
447         -o numops\n\
448         -u          hide unlock fails\n\
449         -a          (show all ops)\n\
450         -O          use oplocks\n\
451 ");
452 }
453
454 /****************************************************************************
455   main program
456 ****************************************************************************/
457  int main(int argc,char *argv[])
458 {
459         char *share1, *share2, *nfspath1, *nfspath2;
460         extern char *optarg;
461         extern int optind;
462         int opt;
463         char *p;
464         int seed;
465
466         setlinebuf(stdout);
467
468         dbf = x_stderr;
469
470         if (argc < 5 || argv[1][0] == '-') {
471                 usage();
472                 exit(1);
473         }
474
475         share1 = argv[1];
476         share2 = argv[2];
477         nfspath1 = argv[3];
478         nfspath2 = argv[4];
479
480         all_string_sub(share1,"/","\\",0);
481         all_string_sub(share2,"/","\\",0);
482
483         setup_logging(argv[0], DEBUG_STDOUT);
484
485         argc -= 4;
486         argv += 4;
487
488         lp_load(dyn_CONFIGFILE,True,False,False);
489         load_interfaces();
490
491         if (getenv("USER")) {
492                 fstrcpy(username,getenv("USER"));
493         }
494
495         seed = time(NULL);
496
497         while ((opt = getopt(argc, argv, "U:s:ho:aAW:O")) != EOF) {
498                 switch (opt) {
499                 case 'U':
500                         fstrcpy(username,optarg);
501                         p = strchr_m(username,'%');
502                         if (p) {
503                                 *p = 0;
504                                 fstrcpy(password, p+1);
505                                 got_pass = 1;
506                         }
507                         break;
508                 case 's':
509                         seed = atoi(optarg);
510                         break;
511                 case 'u':
512                         hide_unlock_fails = True;
513                         break;
514                 case 'o':
515                         numops = atoi(optarg);
516                         break;
517                 case 'O':
518                         use_oplocks = True;
519                         break;
520                 case 'a':
521                         showall = True;
522                         break;
523                 case 'A':
524                         analyze = True;
525                         break;
526                 case 'h':
527                         usage();
528                         exit(1);
529                 default:
530                         printf("Unknown option %c (%d)\n", (char)opt, opt);
531                         exit(1);
532                 }
533         }
534
535         argc -= optind;
536         argv += optind;
537
538         DEBUG(0,("seed=%u\n", seed));
539         srandom(seed);
540
541         locking_init(1);
542         test_locks(share1, share2, nfspath1, nfspath2);
543
544         return(0);
545 }