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 "system/filesys.h"
23 #include "system/time.h"
25 #include "auth/credentials/credentials.h"
26 #include "auth/gensec/gensec.h"
27 #include "libcli/libcli.h"
29 static int numops
= 1000;
32 static BOOL hide_unlock_fails
;
33 static BOOL use_oplocks
;
34 static uint_t lock_range
= 100;
35 static uint_t lock_base
= 0;
36 static uint_t min_length
= 0;
37 static BOOL exact_error_codes
;
38 static BOOL zero_zero
;
40 #define FILENAME "\\locktest.dat"
45 #define RANGE_MULTIPLE 1
47 #define NCONNECTIONS 2
49 #define LOCK_TIMEOUT 0
51 static struct cli_credentials
*servers
[NSERVERS
];
53 enum lock_op
{OP_LOCK
, OP_UNLOCK
, OP_REOPEN
};
57 enum brl_type lock_type
;
66 static struct record preset
[] = {
67 {OP_LOCK
, WRITE_LOCK
, 0, 0, 2, 0, 1},
68 {OP_LOCK
, WRITE_LOCK
, 0, 0, 0, 0, 1},
69 {OP_LOCK
, WRITE_LOCK
, 0, 0, 3, 0, 1},
70 {OP_UNLOCK
, 0 , 0, 0, 2, 0, 1},
71 {OP_REOPEN
, 0, 0, 0, 0, 0, 1},
73 {OP_LOCK
, READ_LOCK
, 0, 0, 2, 0, 1},
74 {OP_LOCK
, READ_LOCK
, 0, 0, 1, 1, 1},
75 {OP_LOCK
, WRITE_LOCK
, 0, 0, 0, 0, 1},
76 {OP_REOPEN
, 0, 0, 0, 0, 0, 1},
78 {OP_LOCK
, READ_LOCK
, 0, 0, 2, 0, 1},
79 {OP_LOCK
, WRITE_LOCK
, 0, 0, 3, 1, 1},
80 {OP_LOCK
, WRITE_LOCK
, 0, 0, 0, 0, 1},
81 {OP_REOPEN
, 0, 0, 0, 0, 0, 1},
83 {OP_LOCK
, READ_LOCK
, 0, 0, 2, 0, 1},
84 {OP_LOCK
, WRITE_LOCK
, 0, 0, 1, 1, 1},
85 {OP_LOCK
, WRITE_LOCK
, 0, 0, 0, 0, 1},
86 {OP_REOPEN
, 0, 0, 0, 0, 0, 1},
88 {OP_LOCK
, WRITE_LOCK
, 0, 0, 2, 0, 1},
89 {OP_LOCK
, READ_LOCK
, 0, 0, 1, 1, 1},
90 {OP_LOCK
, WRITE_LOCK
, 0, 0, 0, 0, 1},
91 {OP_REOPEN
, 0, 0, 0, 0, 0, 1},
93 {OP_LOCK
, WRITE_LOCK
, 0, 0, 2, 0, 1},
94 {OP_LOCK
, READ_LOCK
, 0, 0, 3, 1, 1},
95 {OP_LOCK
, WRITE_LOCK
, 0, 0, 0, 0, 1},
96 {OP_REOPEN
, 0, 0, 0, 0, 0, 1},
101 static struct record
*recorded
;
103 /*****************************************************
104 return a connection to a server
105 *******************************************************/
106 static struct smbcli_state
*connect_one(char *share
, int snum
, int conn
)
108 struct smbcli_state
*c
;
109 fstring server
, myname
;
113 printf("connect_one(%s, %d, %d)\n", share
, snum
, conn
);
115 fstrcpy(server
,share
+2);
116 share
= strchr_m(server
,'\\');
117 if (!share
) return NULL
;
122 char **unc_list
= NULL
;
125 p
= lp_parm_string(-1, "torture", "unclist");
128 unc_list
= file_lines_load(p
, &num_unc_names
, NULL
);
129 if (!unc_list
|| num_unc_names
<= 0) {
130 printf("Failed to load unc names list from '%s'\n", p
);
134 if (!smbcli_parse_unc(unc_list
[conn
% num_unc_names
],
136 printf("Failed to parse UNC name %s\n",
137 unc_list
[conn
% num_unc_names
]);
146 slprintf(myname
,sizeof(myname
), "lock-%u-%u", getpid(), snum
);
147 cli_credentials_set_workstation(servers
[snum
], myname
, CRED_SPECIFIED
);
150 printf("\\\\%s\\%s\n", server
, share
);
151 status
= smbcli_full_connection(NULL
, &c
,
154 servers
[snum
], NULL
);
155 if (!NT_STATUS_IS_OK(status
)) {
158 } while (!NT_STATUS_IS_OK(status
) && retries
--);
160 if (!NT_STATUS_IS_OK(status
)) {
168 static void reconnect(struct smbcli_state
*cli
[NSERVERS
][NCONNECTIONS
], int fnum
[NSERVERS
][NCONNECTIONS
][NFILES
],
169 char *share
[NSERVERS
])
173 for (server
=0;server
<NSERVERS
;server
++)
174 for (conn
=0;conn
<NCONNECTIONS
;conn
++) {
175 if (cli
[server
][conn
]) {
176 for (f
=0;f
<NFILES
;f
++) {
177 if (fnum
[server
][conn
][f
] != -1) {
178 smbcli_close(cli
[server
][conn
]->tree
, fnum
[server
][conn
][f
]);
179 fnum
[server
][conn
][f
] = -1;
182 talloc_free(cli
[server
][conn
]);
184 cli
[server
][conn
] = connect_one(share
[server
], server
, conn
);
185 if (!cli
[server
][conn
]) {
186 DEBUG(0,("Failed to connect to %s\n", share
[server
]));
194 static BOOL
test_one(struct smbcli_state
*cli
[NSERVERS
][NCONNECTIONS
],
195 int fnum
[NSERVERS
][NCONNECTIONS
][NFILES
],
198 uint_t conn
= rec
->conn
;
200 uint64_t start
= rec
->start
;
201 uint64_t len
= rec
->len
;
202 enum brl_type op
= rec
->lock_type
;
205 NTSTATUS status
[NSERVERS
];
207 switch (rec
->lock_op
) {
210 for (server
=0;server
<NSERVERS
;server
++) {
211 ret
[server
] = NT_STATUS_IS_OK(smbcli_lock64(cli
[server
][conn
]->tree
,
212 fnum
[server
][conn
][f
],
213 start
, len
, LOCK_TIMEOUT
, op
));
214 status
[server
] = smbcli_nt_error(cli
[server
][conn
]->tree
);
215 if (!exact_error_codes
&&
216 NT_STATUS_EQUAL(status
[server
],
217 NT_STATUS_FILE_LOCK_CONFLICT
)) {
218 status
[server
] = NT_STATUS_LOCK_NOT_GRANTED
;
221 if (showall
|| !NT_STATUS_EQUAL(status
[0],status
[1])) {
222 printf("lock conn=%u f=%u range=%.0f(%.0f) op=%s -> %s:%s\n",
224 (double)start
, (double)len
,
225 op
==READ_LOCK
?"READ_LOCK":"WRITE_LOCK",
226 nt_errstr(status
[0]), nt_errstr(status
[1]));
228 if (!NT_STATUS_EQUAL(status
[0],status
[1])) return False
;
233 for (server
=0;server
<NSERVERS
;server
++) {
234 ret
[server
] = NT_STATUS_IS_OK(smbcli_unlock64(cli
[server
][conn
]->tree
,
235 fnum
[server
][conn
][f
],
237 status
[server
] = smbcli_nt_error(cli
[server
][conn
]->tree
);
240 (!hide_unlock_fails
&& !NT_STATUS_EQUAL(status
[0],status
[1]))) {
241 printf("unlock conn=%u f=%u range=%.0f(%.0f) -> %s:%s\n",
243 (double)start
, (double)len
,
244 nt_errstr(status
[0]), nt_errstr(status
[1]));
246 if (!hide_unlock_fails
&& !NT_STATUS_EQUAL(status
[0],status
[1]))
251 /* reopen the file */
252 for (server
=0;server
<NSERVERS
;server
++) {
253 smbcli_close(cli
[server
][conn
]->tree
, fnum
[server
][conn
][f
]);
254 fnum
[server
][conn
][f
] = -1;
256 for (server
=0;server
<NSERVERS
;server
++) {
257 fnum
[server
][conn
][f
] = smbcli_open(cli
[server
][conn
]->tree
, FILENAME
,
260 if (fnum
[server
][conn
][f
] == -1) {
261 printf("failed to reopen on share%d\n", server
);
266 printf("reopen conn=%u f=%u\n",
275 static void close_files(struct smbcli_state
*cli
[NSERVERS
][NCONNECTIONS
],
276 int fnum
[NSERVERS
][NCONNECTIONS
][NFILES
])
280 for (server
=0;server
<NSERVERS
;server
++)
281 for (conn
=0;conn
<NCONNECTIONS
;conn
++)
282 for (f
=0;f
<NFILES
;f
++) {
283 if (fnum
[server
][conn
][f
] != -1) {
284 smbcli_close(cli
[server
][conn
]->tree
, fnum
[server
][conn
][f
]);
285 fnum
[server
][conn
][f
] = -1;
288 for (server
=0;server
<NSERVERS
;server
++) {
289 smbcli_unlink(cli
[server
][0]->tree
, FILENAME
);
293 static void open_files(struct smbcli_state
*cli
[NSERVERS
][NCONNECTIONS
],
294 int fnum
[NSERVERS
][NCONNECTIONS
][NFILES
])
298 for (server
=0;server
<NSERVERS
;server
++)
299 for (conn
=0;conn
<NCONNECTIONS
;conn
++)
300 for (f
=0;f
<NFILES
;f
++) {
301 fnum
[server
][conn
][f
] = smbcli_open(cli
[server
][conn
]->tree
, FILENAME
,
304 if (fnum
[server
][conn
][f
] == -1) {
305 fprintf(stderr
,"Failed to open fnum[%u][%u][%u]\n",
313 static int retest(struct smbcli_state
*cli
[NSERVERS
][NCONNECTIONS
],
314 int fnum
[NSERVERS
][NCONNECTIONS
][NFILES
],
318 printf("testing %u ...\n", n
);
319 for (i
=0; i
<n
; i
++) {
320 if (i
&& i
% 100 == 0) {
324 if (recorded
[i
].needed
&&
325 !test_one(cli
, fnum
, &recorded
[i
])) return i
;
331 /* each server has two connections open to it. Each connection has two file
332 descriptors open on the file - 8 file descriptors in total
334 we then do random locking ops in tamdem on the 4 fnums from each
335 server and ensure that the results match
337 static void test_locks(char *share
[NSERVERS
])
339 struct smbcli_state
*cli
[NSERVERS
][NCONNECTIONS
];
340 int fnum
[NSERVERS
][NCONNECTIONS
][NFILES
];
341 int n
, i
, n1
, skip
, r1
, r2
;
346 recorded
= malloc_array_p(struct record
, numops
);
348 for (n
=0; n
<numops
; n
++) {
350 if (n
< sizeof(preset
) / sizeof(preset
[0])) {
351 recorded
[n
] = preset
[n
];
354 recorded
[n
].conn
= random() % NCONNECTIONS
;
355 recorded
[n
].f
= random() % NFILES
;
356 recorded
[n
].start
= lock_base
+ ((uint_t
)random() % (lock_range
-1));
357 recorded
[n
].len
= min_length
+
358 random() % (lock_range
-(recorded
[n
].start
-lock_base
));
359 recorded
[n
].start
*= RANGE_MULTIPLE
;
360 recorded
[n
].len
*= RANGE_MULTIPLE
;
364 recorded
[n
].lock_type
= READ_LOCK
;
366 recorded
[n
].lock_type
= WRITE_LOCK
;
369 recorded
[n
].lock_op
= OP_LOCK
;
370 } else if (r2
< UNLOCK_PCT
) {
371 recorded
[n
].lock_op
= OP_UNLOCK
;
373 recorded
[n
].lock_op
= OP_REOPEN
;
375 recorded
[n
].needed
= True
;
376 if (!zero_zero
&& recorded
[n
].start
==0 && recorded
[n
].len
==0) {
384 reconnect(cli
, fnum
, share
);
385 open_files(cli
, fnum
);
386 n
= retest(cli
, fnum
, numops
);
388 if (n
== numops
|| !analyze
) return;
396 close_files(cli
, fnum
);
397 reconnect(cli
, fnum
, share
);
398 open_files(cli
, fnum
);
400 for (i
=0;i
<n
-skip
;i
+=skip
) {
402 printf("excluding %d-%d\n", i
, i
+skip
-1);
403 for (j
=i
;j
<i
+skip
;j
++) {
404 recorded
[j
].needed
= False
;
407 close_files(cli
, fnum
);
408 open_files(cli
, fnum
);
410 m
= retest(cli
, fnum
, n
);
412 for (j
=i
;j
<i
+skip
;j
++) {
413 recorded
[j
].needed
= True
;
416 if (i
+(skip
-1) < m
) {
417 memmove(&recorded
[i
], &recorded
[i
+skip
],
418 (m
-(i
+skip
-1))*sizeof(recorded
[0]));
427 printf("skip=%d\n", skip
);
434 close_files(cli
, fnum
);
435 reconnect(cli
, fnum
, share
);
436 open_files(cli
, fnum
);
438 n1
= retest(cli
, fnum
, n
);
440 printf("ERROR - inconsistent result (%u %u)\n", n1
, n
);
442 close_files(cli
, fnum
);
445 printf("{%d, %d, %u, %u, %.0f, %.0f, %u},\n",
447 recorded
[i
].lock_type
,
450 (double)recorded
[i
].start
,
451 (double)recorded
[i
].len
,
458 static void usage(void)
462 locktest //server1/share1 //server2/share2 [options..]\n\
464 -U user%%pass (may be specified twice)\n\
467 -u hide unlock fails\n\
469 -A analyse for minimal ops\n\
471 -E enable exact error code checking\n\
472 -Z enable the zero/zero lock\n\
473 -R range set lock range\n\
474 -B base set lock base\n\
475 -M min set min lock length\n\
476 -l filename unclist file\n\
480 /****************************************************************************
482 ****************************************************************************/
483 int main(int argc
,char *argv
[])
485 char *share
[NSERVERS
];
488 int username_count
=0;
492 setup_logging("locktest", DEBUG_STDOUT
);
494 if (argc
< 3 || argv
[1][0] == '-') {
499 setup_logging(argv
[0], DEBUG_STDOUT
);
501 for (server
=0;server
<NSERVERS
;server
++) {
502 share
[server
] = argv
[1+server
];
503 all_string_sub(share
[server
],"/","\\",0);
511 servers
[0] = cli_credentials_init(talloc_autofree_context());
512 servers
[1] = cli_credentials_init(talloc_autofree_context());
513 cli_credentials_guess(servers
[0]);
514 cli_credentials_guess(servers
[1]);
518 while ((opt
= getopt(argc
, argv
, "U:s:ho:aAW:OR:B:M:EZW:l:")) != EOF
) {
521 if (username_count
== 2) {
525 cli_credentials_parse_string(servers
[username_count
],
526 optarg
, CRED_SPECIFIED
);
530 lock_range
= strtol(optarg
, NULL
, 0);
533 lock_base
= strtol(optarg
, NULL
, 0);
536 min_length
= strtol(optarg
, NULL
, 0);
542 hide_unlock_fails
= True
;
545 numops
= atoi(optarg
);
560 exact_error_codes
= True
;
563 lp_set_cmdline("torture:unclist", optarg
);
566 lp_set_cmdline("workgroup", optarg
);
572 printf("Unknown option %c (%d)\n", (char)opt
, opt
);
577 if (username_count
== 0) {
581 if (username_count
== 1) {
582 servers
[1] = servers
[0];
590 DEBUG(0,("seed=%u base=%d range=%d min_length=%d\n",
591 seed
, lock_base
, lock_range
, min_length
));