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