s3-includes: move readahead prototype to its only user.
[Samba.git] / source3 / torture / locktest2.c
blob55dc1d52b782331434b5c9cd004af887640bcbeb
1 /*
2 Unix SMB/CIFS implementation.
3 byte range lock tester - with local filesystem support
4 Copyright (C) Andrew Tridgell 1999
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.
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.
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/>.
20 #include "includes.h"
21 #include "system/filesys.h"
22 #include "locking/proto.h"
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;
33 extern char *optarg;
34 extern int optind;
36 #define FILENAME "\\locktest.dat"
37 #define LOCKRANGE 100
38 #define LOCKBASE 0
41 #define LOCKBASE (0x40000000 - 50)
44 #define READ_PCT 50
45 #define LOCK_PCT 25
46 #define UNLOCK_PCT 65
47 #define RANGE_MULTIPLE 1
49 #define NSERVERS 2
50 #define NCONNECTIONS 2
51 #define NUMFSTYPES 2
52 #define NFILES 2
53 #define LOCK_TIMEOUT 0
55 #define FSTYPE_SMB 0
56 #define FSTYPE_NFS 1
58 struct record {
59 char r1, r2;
60 char conn, f, fstype;
61 unsigned start, len;
62 char needed;
65 static struct record *recorded;
67 static int try_open(struct cli_state *c, char *nfs, int fstype, const char *fname, int flags)
69 char *path;
71 switch (fstype) {
72 case FSTYPE_SMB:
74 uint16_t fd;
75 if (!NT_STATUS_IS_OK(cli_open(c, fname, flags, DENY_NONE, &fd))) {
76 return -1;
78 return fd;
81 case FSTYPE_NFS:
82 if (asprintf(&path, "%s%s", nfs, fname) > 0) {
83 int ret;
84 string_replace(path,'\\', '/');
85 ret = open(path, flags, 0666);
86 SAFE_FREE(path);
87 return ret;
89 break;
92 return -1;
95 static bool try_close(struct cli_state *c, int fstype, int fd)
97 switch (fstype) {
98 case FSTYPE_SMB:
99 return NT_STATUS_IS_OK(cli_close(c, fd));
101 case FSTYPE_NFS:
102 return close(fd) == 0;
105 return False;
108 static bool try_lock(struct cli_state *c, int fstype,
109 int fd, unsigned start, unsigned len,
110 enum brl_type op)
112 struct flock lock;
114 switch (fstype) {
115 case FSTYPE_SMB:
116 return cli_lock(c, fd, start, len, LOCK_TIMEOUT, op);
118 case FSTYPE_NFS:
119 lock.l_type = (op==READ_LOCK) ? F_RDLCK:F_WRLCK;
120 lock.l_whence = SEEK_SET;
121 lock.l_start = start;
122 lock.l_len = len;
123 lock.l_pid = getpid();
124 return fcntl(fd,F_SETLK,&lock) == 0;
127 return False;
130 static bool try_unlock(struct cli_state *c, int fstype,
131 int fd, unsigned start, unsigned len)
133 struct flock lock;
135 switch (fstype) {
136 case FSTYPE_SMB:
137 return NT_STATUS_IS_OK(cli_unlock(c, fd, start, len));
139 case FSTYPE_NFS:
140 lock.l_type = F_UNLCK;
141 lock.l_whence = SEEK_SET;
142 lock.l_start = start;
143 lock.l_len = len;
144 lock.l_pid = getpid();
145 return fcntl(fd,F_SETLK,&lock) == 0;
148 return False;
151 static void print_brl(struct file_id id, struct server_id pid,
152 enum brl_type lock_type,
153 enum brl_flavour lock_flav,
154 br_off start, br_off size,
155 void *private_data)
157 printf("%6d %s %s %.0f:%.0f(%.0f)\n",
158 (int)procid_to_pid(&pid), file_id_string_tos(&id),
159 lock_type==READ_LOCK?"R":"W",
160 (double)start, (double)start+size-1,(double)size);
164 /*****************************************************
165 return a connection to a server
166 *******************************************************/
167 static struct cli_state *connect_one(char *share)
169 struct cli_state *c;
170 char *server_n;
171 fstring server;
172 fstring myname;
173 static int count;
174 NTSTATUS nt_status;
176 fstrcpy(server,share+2);
177 share = strchr_m(server,'\\');
178 if (!share) return NULL;
179 *share = 0;
180 share++;
182 server_n = server;
184 if (!got_pass) {
185 char *pass = getpass("Password: ");
186 if (pass) {
187 fstrcpy(password, pass);
191 slprintf(myname,sizeof(myname), "lock-%lu-%u", (unsigned long)getpid(), count++);
193 nt_status = cli_full_connection(&c, myname, server_n, NULL, 0, share, "?????",
194 username, lp_workgroup(), password, 0,
195 Undefined);
197 if (!NT_STATUS_IS_OK(nt_status)) {
198 DEBUG(0, ("cli_full_connection failed with error %s\n", nt_errstr(nt_status)));
199 return NULL;
202 c->use_oplocks = use_oplocks;
204 return c;
208 static void reconnect(struct cli_state *cli[NSERVERS][NCONNECTIONS],
209 char *nfs[NSERVERS],
210 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
211 char *share1, char *share2)
213 int server, conn, f, fstype;
214 char *share[2];
215 share[0] = share1;
216 share[1] = share2;
218 fstype = FSTYPE_SMB;
220 for (server=0;server<NSERVERS;server++)
221 for (conn=0;conn<NCONNECTIONS;conn++) {
222 if (cli[server][conn]) {
223 for (f=0;f<NFILES;f++) {
224 cli_close(cli[server][conn], fnum[server][fstype][conn][f]);
226 cli_ulogoff(cli[server][conn]);
227 cli_shutdown(cli[server][conn]);
229 cli[server][conn] = connect_one(share[server]);
230 if (!cli[server][conn]) {
231 DEBUG(0,("Failed to connect to %s\n", share[server]));
232 exit(1);
239 static bool test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS],
240 char *nfs[NSERVERS],
241 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
242 struct record *rec)
244 unsigned conn = rec->conn;
245 unsigned f = rec->f;
246 unsigned fstype = rec->fstype;
247 unsigned start = rec->start;
248 unsigned len = rec->len;
249 unsigned r1 = rec->r1;
250 unsigned r2 = rec->r2;
251 enum brl_type op;
252 int server;
253 bool ret[NSERVERS];
255 if (r1 < READ_PCT) {
256 op = READ_LOCK;
257 } else {
258 op = WRITE_LOCK;
261 if (r2 < LOCK_PCT) {
262 /* set a lock */
263 for (server=0;server<NSERVERS;server++) {
264 ret[server] = try_lock(cli[server][conn], fstype,
265 fnum[server][fstype][conn][f],
266 start, len, op);
268 if (showall || ret[0] != ret[1]) {
269 printf("lock conn=%u fstype=%u f=%u range=%u:%u(%u) op=%s -> %u:%u\n",
270 conn, fstype, f,
271 start, start+len-1, len,
272 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
273 ret[0], ret[1]);
275 if (showall) brl_forall(print_brl, NULL);
276 if (ret[0] != ret[1]) return False;
277 } else if (r2 < LOCK_PCT+UNLOCK_PCT) {
278 /* unset a lock */
279 for (server=0;server<NSERVERS;server++) {
280 ret[server] = try_unlock(cli[server][conn], fstype,
281 fnum[server][fstype][conn][f],
282 start, len);
284 if (showall || (!hide_unlock_fails && (ret[0] != ret[1]))) {
285 printf("unlock conn=%u fstype=%u f=%u range=%u:%u(%u) -> %u:%u\n",
286 conn, fstype, f,
287 start, start+len-1, len,
288 ret[0], ret[1]);
290 if (showall) brl_forall(print_brl, NULL);
291 if (!hide_unlock_fails && ret[0] != ret[1]) return False;
292 } else {
293 /* reopen the file */
294 for (server=0;server<NSERVERS;server++) {
295 try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
296 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
297 O_RDWR|O_CREAT);
298 if (fnum[server][fstype][conn][f] == -1) {
299 printf("failed to reopen on share1\n");
300 return False;
303 if (showall) {
304 printf("reopen conn=%u fstype=%u f=%u\n",
305 conn, fstype, f);
306 brl_forall(print_brl, NULL);
309 return True;
312 static void close_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
313 char *nfs[NSERVERS],
314 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
316 int server, conn, f, fstype;
318 for (server=0;server<NSERVERS;server++)
319 for (fstype=0;fstype<NUMFSTYPES;fstype++)
320 for (conn=0;conn<NCONNECTIONS;conn++)
321 for (f=0;f<NFILES;f++) {
322 if (fnum[server][fstype][conn][f] != -1) {
323 try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
324 fnum[server][fstype][conn][f] = -1;
327 for (server=0;server<NSERVERS;server++) {
328 cli_unlink(cli[server][0], FILENAME, aSYSTEM | aHIDDEN);
332 static void open_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
333 char *nfs[NSERVERS],
334 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
336 int server, fstype, conn, f;
338 for (server=0;server<NSERVERS;server++)
339 for (fstype=0;fstype<NUMFSTYPES;fstype++)
340 for (conn=0;conn<NCONNECTIONS;conn++)
341 for (f=0;f<NFILES;f++) {
342 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
343 O_RDWR|O_CREAT);
344 if (fnum[server][fstype][conn][f] == -1) {
345 fprintf(stderr,"Failed to open fnum[%u][%u][%u][%u]\n",
346 server, fstype, conn, f);
347 exit(1);
353 static int retest(struct cli_state *cli[NSERVERS][NCONNECTIONS],
354 char *nfs[NSERVERS],
355 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
356 int n)
358 int i;
359 printf("testing %u ...\n", n);
360 for (i=0; i<n; i++) {
361 if (i && i % 100 == 0) {
362 printf("%u\n", i);
365 if (recorded[i].needed &&
366 !test_one(cli, nfs, fnum, &recorded[i])) return i;
368 return n;
372 /* each server has two connections open to it. Each connection has two file
373 descriptors open on the file - 8 file descriptors in total
375 we then do random locking ops in tamdem on the 4 fnums from each
376 server and ensure that the results match
378 static void test_locks(char *share1, char *share2, char *nfspath1, char *nfspath2)
380 struct cli_state *cli[NSERVERS][NCONNECTIONS];
381 char *nfs[NSERVERS];
382 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES];
383 int n, i, n1;
385 nfs[0] = nfspath1;
386 nfs[1] = nfspath2;
388 ZERO_STRUCT(fnum);
389 ZERO_STRUCT(cli);
391 recorded = SMB_MALLOC_ARRAY(struct record, numops);
393 for (n=0; n<numops; n++) {
394 recorded[n].conn = random() % NCONNECTIONS;
395 recorded[n].fstype = random() % NUMFSTYPES;
396 recorded[n].f = random() % NFILES;
397 recorded[n].start = LOCKBASE + ((unsigned)random() % (LOCKRANGE-1));
398 recorded[n].len = 1 +
399 random() % (LOCKRANGE-(recorded[n].start-LOCKBASE));
400 recorded[n].start *= RANGE_MULTIPLE;
401 recorded[n].len *= RANGE_MULTIPLE;
402 recorded[n].r1 = random() % 100;
403 recorded[n].r2 = random() % 100;
404 recorded[n].needed = True;
407 reconnect(cli, nfs, fnum, share1, share2);
408 open_files(cli, nfs, fnum);
409 n = retest(cli, nfs, fnum, numops);
411 if (n == numops || !analyze) return;
412 n++;
414 while (1) {
415 n1 = n;
417 close_files(cli, nfs, fnum);
418 reconnect(cli, nfs, fnum, share1, share2);
419 open_files(cli, nfs, fnum);
421 for (i=0;i<n-1;i++) {
422 int m;
423 recorded[i].needed = False;
425 close_files(cli, nfs, fnum);
426 open_files(cli, nfs, fnum);
428 m = retest(cli, nfs, fnum, n);
429 if (m == n) {
430 recorded[i].needed = True;
431 } else {
432 if (i < m) {
433 memmove(&recorded[i], &recorded[i+1],
434 (m-i)*sizeof(recorded[0]));
436 n = m;
437 i--;
441 if (n1 == n) break;
444 close_files(cli, nfs, fnum);
445 reconnect(cli, nfs, fnum, share1, share2);
446 open_files(cli, nfs, fnum);
447 showall = True;
448 n1 = retest(cli, nfs, fnum, n);
449 if (n1 != n-1) {
450 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
452 close_files(cli, nfs, fnum);
454 for (i=0;i<n;i++) {
455 printf("{%u, %u, %u, %u, %u, %u, %u, %u},\n",
456 recorded[i].r1,
457 recorded[i].r2,
458 recorded[i].conn,
459 recorded[i].fstype,
460 recorded[i].f,
461 recorded[i].start,
462 recorded[i].len,
463 recorded[i].needed);
469 static void usage(void)
471 printf(
472 "Usage:\n\
473 locktest //server1/share1 //server2/share2 /path1 /path2 [options..]\n\
474 options:\n\
475 -U user%%pass\n\
476 -s seed\n\
477 -o numops\n\
478 -u hide unlock fails\n\
479 -a (show all ops)\n\
480 -O use oplocks\n\
484 /****************************************************************************
485 main program
486 ****************************************************************************/
487 int main(int argc,char *argv[])
489 char *share1, *share2, *nfspath1, *nfspath2;
490 int opt;
491 char *p;
492 int seed;
494 setlinebuf(stdout);
496 if (argc < 5 || argv[1][0] == '-') {
497 usage();
498 exit(1);
501 setup_logging(argv[0], DEBUG_STDOUT);
503 share1 = argv[1];
504 share2 = argv[2];
505 nfspath1 = argv[3];
506 nfspath2 = argv[4];
508 all_string_sub(share1,"/","\\",0);
509 all_string_sub(share2,"/","\\",0);
511 argc -= 4;
512 argv += 4;
514 lp_load(get_dyn_CONFIGFILE(),True,False,False,True);
515 load_interfaces();
517 if (getenv("USER")) {
518 fstrcpy(username,getenv("USER"));
521 seed = time(NULL);
523 while ((opt = getopt(argc, argv, "U:s:ho:aAW:O")) != EOF) {
524 switch (opt) {
525 case 'U':
526 fstrcpy(username,optarg);
527 p = strchr_m(username,'%');
528 if (p) {
529 *p = 0;
530 fstrcpy(password, p+1);
531 got_pass = 1;
533 break;
534 case 's':
535 seed = atoi(optarg);
536 break;
537 case 'u':
538 hide_unlock_fails = True;
539 break;
540 case 'o':
541 numops = atoi(optarg);
542 break;
543 case 'O':
544 use_oplocks = True;
545 break;
546 case 'a':
547 showall = True;
548 break;
549 case 'A':
550 analyze = True;
551 break;
552 case 'h':
553 usage();
554 exit(1);
555 default:
556 printf("Unknown option %c (%d)\n", (char)opt, opt);
557 exit(1);
561 argc -= optind;
562 argv += optind;
564 DEBUG(0,("seed=%u\n", seed));
565 srandom(seed);
567 locking_init_readonly();
568 test_locks(share1, share2, nfspath1, nfspath2);
570 return(0);