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