2 Unix SMB/CIFS implementation.
3 randomised byte range lock tester
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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "dynconfig.h"
23 #include "system/time.h"
25 static int numops = 1000;
28 static BOOL hide_unlock_fails;
29 static BOOL use_oplocks;
30 static uint_t lock_range = 100;
31 static uint_t lock_base = 0;
32 static uint_t min_length = 0;
33 static BOOL exact_error_codes;
34 static BOOL zero_zero;
36 #define FILENAME "\\locktest.dat"
41 #define RANGE_MULTIPLE 1
43 #define NCONNECTIONS 2
45 #define LOCK_TIMEOUT 0
47 #define NASTY_POSIX_LOCK_HACK 0
54 enum lock_op {OP_LOCK, OP_UNLOCK, OP_REOPEN};
58 enum brl_type lock_type;
67 static struct record preset[] = {
68 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
69 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
70 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 0, 1},
71 {OP_UNLOCK, 0 , 0, 0, 2, 0, 1},
72 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
74 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
75 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
76 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
77 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
79 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
80 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 1, 1},
81 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
82 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
84 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
85 {OP_LOCK, WRITE_LOCK, 0, 0, 1, 1, 1},
86 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
87 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
89 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
90 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
91 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
92 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
94 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
95 {OP_LOCK, READ_LOCK, 0, 0, 3, 1, 1},
96 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
97 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
102 static struct record *recorded;
104 /*****************************************************
105 return a connection to a server
106 *******************************************************/
107 static struct smbcli_state *connect_one(char *share, int snum)
109 struct smbcli_state *c;
110 fstring server, myname;
114 fstrcpy(server,share+2);
115 share = strchr_m(server,'\\');
116 if (!share) return NULL;
120 slprintf(myname,sizeof(myname), "lock-%u-%u", getpid(), snum);
123 status = smbcli_full_connection(NULL, &c, myname,
126 servers[snum].username, lp_workgroup(),
127 servers[snum].password);
128 if (!NT_STATUS_IS_OK(status)) {
131 } while (!NT_STATUS_IS_OK(status) && retries--);
133 if (!NT_STATUS_IS_OK(status)) {
141 static void reconnect(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], int fnum[NSERVERS][NCONNECTIONS][NFILES],
142 char *share[NSERVERS])
146 for (server=0;server<NSERVERS;server++)
147 for (conn=0;conn<NCONNECTIONS;conn++) {
148 if (cli[server][conn]) {
149 for (f=0;f<NFILES;f++) {
150 if (fnum[server][conn][f] != -1) {
151 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
152 fnum[server][conn][f] = -1;
155 smbcli_shutdown(cli[server][conn]);
157 cli[server][conn] = connect_one(share[server], server);
158 if (!cli[server][conn]) {
159 DEBUG(0,("Failed to connect to %s\n", share[server]));
167 static BOOL test_one(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
168 int fnum[NSERVERS][NCONNECTIONS][NFILES],
171 uint_t conn = rec->conn;
173 uint64_t start = rec->start;
174 uint64_t len = rec->len;
175 enum brl_type op = rec->lock_type;
178 NTSTATUS status[NSERVERS];
180 switch (rec->lock_op) {
183 for (server=0;server<NSERVERS;server++) {
184 ret[server] = NT_STATUS_IS_OK(smbcli_lock64(cli[server][conn]->tree,
185 fnum[server][conn][f],
186 start, len, LOCK_TIMEOUT, op));
187 status[server] = smbcli_nt_error(cli[server][conn]->tree);
188 if (!exact_error_codes &&
189 NT_STATUS_EQUAL(status[server],
190 NT_STATUS_FILE_LOCK_CONFLICT)) {
191 status[server] = NT_STATUS_LOCK_NOT_GRANTED;
194 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) {
195 printf("lock conn=%u f=%u range=%.0f(%.0f) op=%s -> %s:%s\n",
197 (double)start, (double)len,
198 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
199 nt_errstr(status[0]), nt_errstr(status[1]));
201 if (!NT_STATUS_EQUAL(status[0],status[1])) return False;
206 for (server=0;server<NSERVERS;server++) {
207 ret[server] = NT_STATUS_IS_OK(smbcli_unlock64(cli[server][conn]->tree,
208 fnum[server][conn][f],
210 status[server] = smbcli_nt_error(cli[server][conn]->tree);
213 (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))) {
214 printf("unlock conn=%u f=%u range=%.0f(%.0f) -> %s:%s\n",
216 (double)start, (double)len,
217 nt_errstr(status[0]), nt_errstr(status[1]));
219 if (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))
224 /* reopen the file */
225 for (server=0;server<NSERVERS;server++) {
226 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
227 fnum[server][conn][f] = -1;
229 for (server=0;server<NSERVERS;server++) {
230 fnum[server][conn][f] = smbcli_open(cli[server][conn]->tree, FILENAME,
233 if (fnum[server][conn][f] == -1) {
234 printf("failed to reopen on share%d\n", server);
239 printf("reopen conn=%u f=%u\n",
248 static void close_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
249 int fnum[NSERVERS][NCONNECTIONS][NFILES])
253 for (server=0;server<NSERVERS;server++)
254 for (conn=0;conn<NCONNECTIONS;conn++)
255 for (f=0;f<NFILES;f++) {
256 if (fnum[server][conn][f] != -1) {
257 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
258 fnum[server][conn][f] = -1;
261 for (server=0;server<NSERVERS;server++) {
262 smbcli_unlink(cli[server][0]->tree, FILENAME);
266 static void open_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
267 int fnum[NSERVERS][NCONNECTIONS][NFILES])
271 for (server=0;server<NSERVERS;server++)
272 for (conn=0;conn<NCONNECTIONS;conn++)
273 for (f=0;f<NFILES;f++) {
274 fnum[server][conn][f] = smbcli_open(cli[server][conn]->tree, FILENAME,
277 if (fnum[server][conn][f] == -1) {
278 fprintf(stderr,"Failed to open fnum[%u][%u][%u]\n",
286 static int retest(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
287 int fnum[NSERVERS][NCONNECTIONS][NFILES],
291 printf("testing %u ...\n", n);
292 for (i=0; i<n; i++) {
293 if (i && i % 100 == 0) {
297 if (recorded[i].needed &&
298 !test_one(cli, fnum, &recorded[i])) return i;
304 /* each server has two connections open to it. Each connection has two file
305 descriptors open on the file - 8 file descriptors in total
307 we then do random locking ops in tamdem on the 4 fnums from each
308 server and ensure that the results match
310 static void test_locks(char *share[NSERVERS])
312 struct smbcli_state *cli[NSERVERS][NCONNECTIONS];
313 int fnum[NSERVERS][NCONNECTIONS][NFILES];
314 int n, i, n1, skip, r1, r2;
319 recorded = malloc_array_p(struct record, numops);
321 for (n=0; n<numops; n++) {
323 if (n < sizeof(preset) / sizeof(preset[0])) {
324 recorded[n] = preset[n];
327 recorded[n].conn = random() % NCONNECTIONS;
328 recorded[n].f = random() % NFILES;
329 recorded[n].start = lock_base + ((uint_t)random() % (lock_range-1));
330 recorded[n].len = min_length +
331 random() % (lock_range-(recorded[n].start-lock_base));
332 recorded[n].start *= RANGE_MULTIPLE;
333 recorded[n].len *= RANGE_MULTIPLE;
337 recorded[n].lock_type = READ_LOCK;
339 recorded[n].lock_type = WRITE_LOCK;
342 recorded[n].lock_op = OP_LOCK;
343 } else if (r2 < UNLOCK_PCT) {
344 recorded[n].lock_op = OP_UNLOCK;
346 recorded[n].lock_op = OP_REOPEN;
348 recorded[n].needed = True;
349 if (!zero_zero && recorded[n].start==0 && recorded[n].len==0) {
357 reconnect(cli, fnum, share);
358 open_files(cli, fnum);
359 n = retest(cli, fnum, numops);
361 if (n == numops || !analyze) return;
369 close_files(cli, fnum);
370 reconnect(cli, fnum, share);
371 open_files(cli, fnum);
373 for (i=0;i<n-skip;i+=skip) {
375 printf("excluding %d-%d\n", i, i+skip-1);
376 for (j=i;j<i+skip;j++) {
377 recorded[j].needed = False;
380 close_files(cli, fnum);
381 open_files(cli, fnum);
383 m = retest(cli, fnum, n);
385 for (j=i;j<i+skip;j++) {
386 recorded[j].needed = True;
389 if (i+(skip-1) < m) {
390 memmove(&recorded[i], &recorded[i+skip],
391 (m-(i+skip-1))*sizeof(recorded[0]));
400 printf("skip=%d\n", skip);
407 close_files(cli, fnum);
408 reconnect(cli, fnum, share);
409 open_files(cli, fnum);
411 n1 = retest(cli, fnum, n);
413 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
415 close_files(cli, fnum);
418 printf("{%d, %d, %u, %u, %.0f, %.0f, %u},\n",
420 recorded[i].lock_type,
423 (double)recorded[i].start,
424 (double)recorded[i].len,
431 static void usage(void)
435 locktest //server1/share1 //server2/share2 [options..]\n\
437 -U user%%pass (may be specified twice)\n\
440 -u hide unlock fails\n\
442 -A analyse for minimal ops\n\
444 -E enable exact error code checking\n\
445 -Z enable the zero/zero lock\n\
446 -R range set lock range\n\
447 -B base set lock base\n\
448 -M min set min lock length\n\
452 /****************************************************************************
454 ****************************************************************************/
455 int main(int argc,char *argv[])
457 char *share[NSERVERS];
463 setup_logging("locktest", DEBUG_STDOUT);
465 if (argc < 3 || argv[1][0] == '-') {
470 setup_logging(argv[0], DEBUG_STDOUT);
472 for (server=0;server<NSERVERS;server++) {
473 share[server] = argv[1+server];
474 all_string_sub(share[server],"/","\\",0);
480 lp_load(dyn_CONFIGFILE,True,False,False);
485 while ((opt = getopt(argc, argv, "U:s:ho:aAW:OR:B:M:EZW:")) != EOF) {
488 i = servers[0].username?1:0;
489 if (!split_username(optarg,
490 &servers[i].username,
491 &servers[i].password)) {
492 printf("Must supply USER%%PASS\n");
497 lock_range = strtol(optarg, NULL, 0);
500 lock_base = strtol(optarg, NULL, 0);
503 min_length = strtol(optarg, NULL, 0);
509 hide_unlock_fails = True;
512 numops = atoi(optarg);
527 exact_error_codes = True;
530 lp_set_cmdline("workgroup", optarg);
536 printf("Unknown option %c (%d)\n", (char)opt, opt);
541 if (!servers[0].username) {
545 if (!servers[1].username) {
546 servers[1].username = servers[0].username;
547 servers[1].password = servers[0].password;
550 locktest_init_subsystems;
555 DEBUG(0,("seed=%u base=%d range=%d min_length=%d\n",
556 seed, lock_base, lock_range, min_length));