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;
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, int conn)
109 struct smbcli_state *c;
110 fstring server, myname;
114 printf("connect_one(%s, %d, %d)\n", share, snum, conn);
116 fstrcpy(server,share+2);
117 share = strchr_m(server,'\\');
118 if (!share) return NULL;
123 char **unc_list = NULL;
126 p = lp_parm_string(-1, "torture", "unclist");
129 unc_list = file_lines_load(p, &num_unc_names, NULL);
130 if (!unc_list || num_unc_names <= 0) {
131 printf("Failed to load unc names list from '%s'\n", p);
135 if (!smbcli_parse_unc(unc_list[conn % num_unc_names],
137 printf("Failed to parse UNC name %s\n",
138 unc_list[conn % num_unc_names]);
147 slprintf(myname,sizeof(myname), "lock-%u-%u", getpid(), snum);
148 cli_credentials_set_workstation(servers[snum], myname, CRED_SPECIFIED);
151 printf("\\\\%s\\%s\n", server, share);
152 status = smbcli_full_connection(NULL, &c,
155 servers[snum], NULL);
156 if (!NT_STATUS_IS_OK(status)) {
159 } while (!NT_STATUS_IS_OK(status) && retries--);
161 if (!NT_STATUS_IS_OK(status)) {
169 static void reconnect(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], int fnum[NSERVERS][NCONNECTIONS][NFILES],
170 char *share[NSERVERS])
174 for (server=0;server<NSERVERS;server++)
175 for (conn=0;conn<NCONNECTIONS;conn++) {
176 if (cli[server][conn]) {
177 for (f=0;f<NFILES;f++) {
178 if (fnum[server][conn][f] != -1) {
179 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
180 fnum[server][conn][f] = -1;
183 talloc_free(cli[server][conn]);
185 cli[server][conn] = connect_one(share[server], server, conn);
186 if (!cli[server][conn]) {
187 DEBUG(0,("Failed to connect to %s\n", share[server]));
195 static BOOL test_one(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
196 int fnum[NSERVERS][NCONNECTIONS][NFILES],
199 uint_t conn = rec->conn;
201 uint64_t start = rec->start;
202 uint64_t len = rec->len;
203 enum brl_type op = rec->lock_type;
206 NTSTATUS status[NSERVERS];
208 switch (rec->lock_op) {
211 for (server=0;server<NSERVERS;server++) {
213 struct smbcli_tree *tree=cli[server][conn]->tree;
214 int fn=fnum[server][conn][f];
216 if (!(tree->session->transport->negotiate.capabilities & CAP_LARGE_FILES)) {
217 res=smbcli_lock(tree, fn, start, len, LOCK_TIMEOUT, rec->lock_op);
219 union smb_lock parms;
221 struct smb_lock_entry lock[1];
223 parms.lockx.level = RAW_LOCK_LOCKX;
224 parms.lockx.in.file.fnum = fn;
226 ltype = (rec->lock_op == READ_LOCK? 1 : 0);
227 ltype |= LOCKING_ANDX_LARGE_FILES;
228 parms.lockx.in.mode = ltype;
229 parms.lockx.in.timeout = LOCK_TIMEOUT;
230 parms.lockx.in.ulock_cnt = 0;
231 parms.lockx.in.lock_cnt = 1;
232 lock[0].pid = rec->pid;
233 lock[0].offset = start;
235 parms.lockx.in.locks = &lock[0];
237 res = smb_raw_lock(tree, &parms);
240 ret[server] = NT_STATUS_IS_OK(res);
241 status[server] = smbcli_nt_error(cli[server][conn]->tree);
242 if (!exact_error_codes &&
243 NT_STATUS_EQUAL(status[server],
244 NT_STATUS_FILE_LOCK_CONFLICT)) {
245 status[server] = NT_STATUS_LOCK_NOT_GRANTED;
248 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) {
249 printf("lock conn=%u f=%u range=%.0f(%.0f) op=%s -> %s:%s\n",
251 (double)start, (double)len,
252 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
253 nt_errstr(status[0]), nt_errstr(status[1]));
255 if (!NT_STATUS_EQUAL(status[0],status[1])) return False;
260 for (server=0;server<NSERVERS;server++) {
262 struct smbcli_tree *tree=cli[server][conn]->tree;
263 int fn=fnum[server][conn][f];
266 if (!(tree->session->transport->negotiate.capabilities & CAP_LARGE_FILES)) {
267 res=smbcli_unlock(tree, fn, start, len);
269 union smb_lock parms;
270 struct smb_lock_entry lock[1];
272 parms.lockx.level = RAW_LOCK_LOCKX;
273 parms.lockx.in.file.fnum = fn;
274 parms.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
275 parms.lockx.in.timeout = 0;
276 parms.lockx.in.ulock_cnt = 1;
277 parms.lockx.in.lock_cnt = 0;
278 lock[0].pid = rec->pid;
280 lock[0].offset = start;
281 parms.lockx.in.locks = &lock[0];
283 res = smb_raw_lock(tree, &parms);
286 ret[server] = NT_STATUS_IS_OK(res);
287 status[server] = smbcli_nt_error(cli[server][conn]->tree);
290 (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))) {
291 printf("unlock conn=%u f=%u range=%.0f(%.0f) -> %s:%s\n",
293 (double)start, (double)len,
294 nt_errstr(status[0]), nt_errstr(status[1]));
296 if (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))
301 /* reopen the file */
302 for (server=0;server<NSERVERS;server++) {
303 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
304 fnum[server][conn][f] = -1;
306 for (server=0;server<NSERVERS;server++) {
307 fnum[server][conn][f] = smbcli_open(cli[server][conn]->tree, FILENAME,
310 if (fnum[server][conn][f] == -1) {
311 printf("failed to reopen on share%d\n", server);
316 printf("reopen conn=%u f=%u\n",
325 static void close_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
326 int fnum[NSERVERS][NCONNECTIONS][NFILES])
330 for (server=0;server<NSERVERS;server++)
331 for (conn=0;conn<NCONNECTIONS;conn++)
332 for (f=0;f<NFILES;f++) {
333 if (fnum[server][conn][f] != -1) {
334 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
335 fnum[server][conn][f] = -1;
338 for (server=0;server<NSERVERS;server++) {
339 smbcli_unlink(cli[server][0]->tree, FILENAME);
343 static void open_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
344 int fnum[NSERVERS][NCONNECTIONS][NFILES])
348 for (server=0;server<NSERVERS;server++)
349 for (conn=0;conn<NCONNECTIONS;conn++)
350 for (f=0;f<NFILES;f++) {
351 fnum[server][conn][f] = smbcli_open(cli[server][conn]->tree, FILENAME,
354 if (fnum[server][conn][f] == -1) {
355 fprintf(stderr,"Failed to open fnum[%u][%u][%u]\n",
363 static int retest(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
364 int fnum[NSERVERS][NCONNECTIONS][NFILES],
368 printf("testing %u ...\n", n);
369 for (i=0; i<n; i++) {
370 if (i && i % 100 == 0) {
374 if (recorded[i].needed &&
375 !test_one(cli, fnum, &recorded[i])) return i;
381 /* each server has two connections open to it. Each connection has two file
382 descriptors open on the file - 8 file descriptors in total
384 we then do random locking ops in tamdem on the 4 fnums from each
385 server and ensure that the results match
387 static void test_locks(char *share[NSERVERS])
389 struct smbcli_state *cli[NSERVERS][NCONNECTIONS];
390 int fnum[NSERVERS][NCONNECTIONS][NFILES];
391 int n, i, n1, skip, r1, r2;
396 recorded = malloc_array_p(struct record, numops);
398 for (n=0; n<numops; n++) {
400 if (n < sizeof(preset) / sizeof(preset[0])) {
401 recorded[n] = preset[n];
404 recorded[n].conn = random() % NCONNECTIONS;
405 recorded[n].f = random() % NFILES;
406 recorded[n].start = lock_base + ((uint_t)random() % (lock_range-1));
407 recorded[n].len = min_length +
408 random() % (lock_range-(recorded[n].start-lock_base));
409 recorded[n].start *= RANGE_MULTIPLE;
410 recorded[n].len *= RANGE_MULTIPLE;
411 recorded[n].pid = random()%3;
412 if (recorded[n].pid == 2) {
413 recorded[n].pid = 0xFFFF; /* see if its magic */
418 recorded[n].lock_type = READ_LOCK;
420 recorded[n].lock_type = WRITE_LOCK;
423 recorded[n].lock_op = OP_LOCK;
424 } else if (r2 < UNLOCK_PCT) {
425 recorded[n].lock_op = OP_UNLOCK;
427 recorded[n].lock_op = OP_REOPEN;
429 recorded[n].needed = True;
430 if (!zero_zero && recorded[n].start==0 && recorded[n].len==0) {
438 reconnect(cli, fnum, share);
439 open_files(cli, fnum);
440 n = retest(cli, fnum, numops);
442 if (n == numops || !analyze) return;
450 close_files(cli, fnum);
451 reconnect(cli, fnum, share);
452 open_files(cli, fnum);
454 for (i=0;i<n-skip;i+=skip) {
456 printf("excluding %d-%d\n", i, i+skip-1);
457 for (j=i;j<i+skip;j++) {
458 recorded[j].needed = False;
461 close_files(cli, fnum);
462 open_files(cli, fnum);
464 m = retest(cli, fnum, n);
466 for (j=i;j<i+skip;j++) {
467 recorded[j].needed = True;
470 if (i+(skip-1) < m) {
471 memmove(&recorded[i], &recorded[i+skip],
472 (m-(i+skip-1))*sizeof(recorded[0]));
481 printf("skip=%d\n", skip);
488 close_files(cli, fnum);
489 reconnect(cli, fnum, share);
490 open_files(cli, fnum);
492 n1 = retest(cli, fnum, n);
494 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
496 close_files(cli, fnum);
499 printf("{%d, %d, %u, %u, %.0f, %.0f, %u},\n",
501 recorded[i].lock_type,
504 (double)recorded[i].start,
505 (double)recorded[i].len,
512 static void usage(void)
516 locktest //server1/share1 //server2/share2 [options..]\n\
518 -U user%%pass (may be specified twice)\n\
521 -u hide unlock fails\n\
523 -A analyse for minimal ops\n\
525 -E enable exact error code checking\n\
526 -Z enable the zero/zero lock\n\
527 -R range set lock range\n\
528 -B base set lock base\n\
529 -M min set min lock length\n\
530 -l filename unclist file\n\
534 /****************************************************************************
536 ****************************************************************************/
537 int main(int argc,char *argv[])
539 char *share[NSERVERS];
542 int username_count=0;
546 setup_logging("locktest", DEBUG_STDOUT);
548 if (argc < 3 || argv[1][0] == '-') {
553 setup_logging(argv[0], DEBUG_STDOUT);
555 for (server=0;server<NSERVERS;server++) {
556 share[server] = argv[1+server];
557 all_string_sub(share[server],"/","\\",0);
565 servers[0] = cli_credentials_init(talloc_autofree_context());
566 servers[1] = cli_credentials_init(talloc_autofree_context());
567 cli_credentials_guess(servers[0]);
568 cli_credentials_guess(servers[1]);
572 while ((opt = getopt(argc, argv, "U:s:ho:aAW:OR:B:M:EZW:l:")) != EOF) {
575 if (username_count == 2) {
579 cli_credentials_parse_string(servers[username_count],
580 optarg, CRED_SPECIFIED);
584 lock_range = strtol(optarg, NULL, 0);
587 lock_base = strtol(optarg, NULL, 0);
590 min_length = strtol(optarg, NULL, 0);
596 hide_unlock_fails = True;
599 numops = atoi(optarg);
614 exact_error_codes = True;
617 lp_set_cmdline("torture:unclist", optarg);
620 lp_set_cmdline("workgroup", optarg);
626 printf("Unknown option %c (%d)\n", (char)opt, opt);
631 if (username_count == 0) {
635 if (username_count == 1) {
636 servers[1] = servers[0];
644 DEBUG(0,("seed=%u base=%d range=%d min_length=%d\n",
645 seed, lock_base, lock_range, min_length));