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