r25398: Parse loadparm context to all lp_*() functions.
[tprouty/samba.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 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 "system/passwd.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, 0, share, NULL,
167                                            username, lp_workgroup(), password, NULL);
168         if (!NT_STATUS_IS_OK(nt_status)) {
169                 DEBUG(0, ("smbcli_full_connection failed with error %s\n", nt_errstr(nt_status)));
170                 return NULL;
171         }
172
173         c->use_oplocks = use_oplocks;
174
175         return c;
176 }
177
178
179 static void reconnect(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], 
180                       char *nfs[NSERVERS], 
181                       int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
182                       char *share1, char *share2)
183 {
184         int server, conn, f, fstype;
185         char *share[2];
186         share[0] = share1;
187         share[1] = share2;
188
189         fstype = FSTYPE_SMB;
190
191         for (server=0;server<NSERVERS;server++)
192         for (conn=0;conn<NCONNECTIONS;conn++) {
193                 if (cli[server][conn]) {
194                         for (f=0;f<NFILES;f++) {
195                                 smbcli_close(cli[server][conn], fnum[server][fstype][conn][f]);
196                         }
197                         smbcli_ulogoff(cli[server][conn]);
198                         talloc_free(cli[server][conn]);
199                 }
200                 cli[server][conn] = connect_one(share[server]);
201                 if (!cli[server][conn]) {
202                         DEBUG(0,("Failed to connect to %s\n", share[server]));
203                         exit(1);
204                 }
205         }
206 }
207
208
209
210 static BOOL test_one(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], 
211                      char *nfs[NSERVERS],
212                      int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
213                      struct record *rec)
214 {
215         uint_t conn = rec->conn;
216         uint_t f = rec->f;
217         uint_t fstype = rec->fstype;
218         uint_t start = rec->start;
219         uint_t len = rec->len;
220         uint_t r1 = rec->r1;
221         uint_t r2 = rec->r2;
222         enum brl_type op;
223         int server;
224         BOOL ret[NSERVERS];
225
226         if (r1 < READ_PCT) {
227                 op = READ_LOCK; 
228         } else {
229                 op = WRITE_LOCK; 
230         }
231
232         if (r2 < LOCK_PCT) {
233                 /* set a lock */
234                 for (server=0;server<NSERVERS;server++) {
235                         ret[server] = try_lock(cli[server][conn], fstype,
236                                                fnum[server][fstype][conn][f],
237                                                start, len, op);
238                 }
239                 if (showall || ret[0] != ret[1]) {
240                         printf("lock   conn=%u fstype=%u f=%u range=%u:%u(%u) op=%s -> %u:%u\n",
241                                conn, fstype, f, 
242                                start, start+len-1, len,
243                                op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
244                                ret[0], ret[1]);
245                 }
246                 if (ret[0] != ret[1]) return False;
247         } else if (r2 < LOCK_PCT+UNLOCK_PCT) {
248                 /* unset a lock */
249                 for (server=0;server<NSERVERS;server++) {
250                         ret[server] = try_unlock(cli[server][conn], fstype,
251                                                  fnum[server][fstype][conn][f],
252                                                  start, len);
253                 }
254                 if (showall || (!hide_unlock_fails && (ret[0] != ret[1]))) {
255                         printf("unlock conn=%u fstype=%u f=%u range=%u:%u(%u)       -> %u:%u\n",
256                                conn, fstype, f, 
257                                start, start+len-1, len,
258                                ret[0], ret[1]);
259                 }
260                 if (!hide_unlock_fails && ret[0] != ret[1]) return False;
261         } else {
262                 /* reopen the file */
263                 for (server=0;server<NSERVERS;server++) {
264                         try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
265                         fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
266                                                                  O_RDWR|O_CREAT);
267                         if (fnum[server][fstype][conn][f] == -1) {
268                                 printf("failed to reopen on share1\n");
269                                 return False;
270                         }
271                 }
272                 if (showall) {
273                         printf("reopen conn=%u fstype=%u f=%u\n",
274                                conn, fstype, f);
275                 }
276         }
277         return True;
278 }
279
280 static void close_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], 
281                         char *nfs[NSERVERS],
282                         int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
283 {
284         int server, conn, f, fstype; 
285
286         for (server=0;server<NSERVERS;server++)
287         for (fstype=0;fstype<NUMFSTYPES;fstype++)
288         for (conn=0;conn<NCONNECTIONS;conn++)
289         for (f=0;f<NFILES;f++) {
290                 if (fnum[server][fstype][conn][f] != -1) {
291                         try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
292                         fnum[server][fstype][conn][f] = -1;
293                 }
294         }
295         for (server=0;server<NSERVERS;server++) {
296                 smbcli_unlink(cli[server][0], FILENAME);
297         }
298 }
299
300 static void open_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], 
301                        char *nfs[NSERVERS],
302                        int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
303 {
304         int server, fstype, conn, f; 
305
306         for (server=0;server<NSERVERS;server++)
307         for (fstype=0;fstype<NUMFSTYPES;fstype++)
308         for (conn=0;conn<NCONNECTIONS;conn++)
309         for (f=0;f<NFILES;f++) {
310                 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
311                                                          O_RDWR|O_CREAT);
312                 if (fnum[server][fstype][conn][f] == -1) {
313                         fprintf(stderr,"Failed to open fnum[%u][%u][%u][%u]\n",
314                                 server, fstype, conn, f);
315                         exit(1);
316                 }
317         }
318 }
319
320
321 static int retest(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], 
322                   char *nfs[NSERVERS],
323                   int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
324                   int n)
325 {
326         int i;
327         printf("testing %u ...\n", n);
328         for (i=0; i<n; i++) {
329                 if (i && i % 100 == 0) {
330                         printf("%u\n", i);
331                 }
332
333                 if (recorded[i].needed &&
334                     !test_one(cli, nfs, fnum, &recorded[i])) return i;
335         }
336         return n;
337 }
338
339
340 /* each server has two connections open to it. Each connection has two file
341    descriptors open on the file - 8 file descriptors in total 
342
343    we then do random locking ops in tamdem on the 4 fnums from each
344    server and ensure that the results match
345  */
346 static void test_locks(char *share1, char *share2, char *nfspath1, char *nfspath2)
347 {
348         struct smbcli_state *cli[NSERVERS][NCONNECTIONS];
349         char *nfs[NSERVERS];
350         int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES];
351         int n, i, n1; 
352
353         nfs[0] = nfspath1;
354         nfs[1] = nfspath2;
355
356         ZERO_STRUCT(fnum);
357         ZERO_STRUCT(cli);
358
359         recorded = malloc_array_p(struct record, numops);
360
361         for (n=0; n<numops; n++) {
362                 recorded[n].conn = random() % NCONNECTIONS;
363                 recorded[n].fstype = random() % NUMFSTYPES;
364                 recorded[n].f = random() % NFILES;
365                 recorded[n].start = LOCKBASE + ((uint_t)random() % (LOCKRANGE-1));
366                 recorded[n].len = 1 + 
367                         random() % (LOCKRANGE-(recorded[n].start-LOCKBASE));
368                 recorded[n].start *= RANGE_MULTIPLE;
369                 recorded[n].len *= RANGE_MULTIPLE;
370                 recorded[n].r1 = random() % 100;
371                 recorded[n].r2 = random() % 100;
372                 recorded[n].needed = True;
373         }
374
375         reconnect(cli, nfs, fnum, share1, share2);
376         open_files(cli, nfs, fnum);
377         n = retest(cli, nfs, fnum, numops);
378
379         if (n == numops || !analyze) return;
380         n++;
381
382         while (1) {
383                 n1 = n;
384
385                 close_files(cli, nfs, fnum);
386                 reconnect(cli, nfs, fnum, share1, share2);
387                 open_files(cli, nfs, fnum);
388
389                 for (i=0;i<n-1;i++) {
390                         int m;
391                         recorded[i].needed = False;
392
393                         close_files(cli, nfs, fnum);
394                         open_files(cli, nfs, fnum);
395
396                         m = retest(cli, nfs, fnum, n);
397                         if (m == n) {
398                                 recorded[i].needed = True;
399                         } else {
400                                 if (i < m) {
401                                         memmove(&recorded[i], &recorded[i+1],
402                                                 (m-i)*sizeof(recorded[0]));
403                                 }
404                                 n = m;
405                                 i--;
406                         }
407                 }
408
409                 if (n1 == n) break;
410         }
411
412         close_files(cli, nfs, fnum);
413         reconnect(cli, nfs, fnum, share1, share2);
414         open_files(cli, nfs, fnum);
415         showall = True;
416         n1 = retest(cli, nfs, fnum, n);
417         if (n1 != n-1) {
418                 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
419         }
420         close_files(cli, nfs, fnum);
421
422         for (i=0;i<n;i++) {
423                 printf("{%u, %u, %u, %u, %u, %u, %u, %u},\n",
424                        recorded[i].r1,
425                        recorded[i].r2,
426                        recorded[i].conn,
427                        recorded[i].fstype,
428                        recorded[i].f,
429                        recorded[i].start,
430                        recorded[i].len,
431                        recorded[i].needed);
432         }       
433 }
434
435
436
437 static void usage(void)
438 {
439         printf(
440 "Usage:\n\
441   locktest //server1/share1 //server2/share2 /path1 /path2 [options..]\n\
442   options:\n\
443         -U user%%pass\n\
444         -s seed\n\
445         -o numops\n\
446         -u          hide unlock fails\n\
447         -a          (show all ops)\n\
448         -O          use oplocks\n\
449 ");
450 }
451
452 /****************************************************************************
453   main program
454 ****************************************************************************/
455  int main(int argc,char *argv[])
456 {
457         char *share1, *share2, *nfspath1, *nfspath2;
458         extern char *optarg;
459         extern int optind;
460         int opt;
461         char *p;
462         int seed;
463
464         setlinebuf(stdout);
465
466         dbf = x_stderr;
467
468         if (argc < 5 || argv[1][0] == '-') {
469                 usage();
470                 exit(1);
471         }
472
473         share1 = argv[1];
474         share2 = argv[2];
475         nfspath1 = argv[3];
476         nfspath2 = argv[4];
477
478         all_string_sub(share1,"/","\\",0);
479         all_string_sub(share2,"/","\\",0);
480
481         setup_logging(argv[0], DEBUG_STDOUT);
482
483         argc -= 4;
484         argv += 4;
485
486         lp_load(dyn_CONFIGFILE);
487
488         if (getenv("USER")) {
489                 fstrcpy(username,getenv("USER"));
490         }
491
492         seed = time(NULL);
493
494         while ((opt = getopt(argc, argv, "U:s:ho:aAW:O")) != EOF) {
495                 switch (opt) {
496                 case 'U':
497                         fstrcpy(username,optarg);
498                         p = strchr_m(username,'%');
499                         if (p) {
500                                 *p = 0;
501                                 fstrcpy(password, p+1);
502                                 got_pass = 1;
503                         }
504                         break;
505                 case 's':
506                         seed = atoi(optarg);
507                         break;
508                 case 'u':
509                         hide_unlock_fails = True;
510                         break;
511                 case 'o':
512                         numops = atoi(optarg);
513                         break;
514                 case 'O':
515                         use_oplocks = True;
516                         break;
517                 case 'a':
518                         showall = True;
519                         break;
520                 case 'A':
521                         analyze = True;
522                         break;
523                 case 'h':
524                         usage();
525                         exit(1);
526                 default:
527                         printf("Unknown option %c (%d)\n", (char)opt, opt);
528                         exit(1);
529                 }
530         }
531
532         argc -= optind;
533         argv += optind;
534
535         DEBUG(0,("seed=%u\n", seed));
536         srandom(seed);
537
538         locking_init(1);
539         test_locks(share1, share2, nfspath1, nfspath2);
540
541         return(0);
542 }