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 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/>.
21 #include "lib/cmdline/popt_common.h"
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"
28 #include "param/param.h"
29 #include "dynconfig.h"
30 #include "libcli/resolve/resolve.h"
32 static int numops = 1000;
35 static int hide_unlock_fails;
36 static int use_oplocks;
37 static uint_t lock_range = 100;
38 static uint_t lock_base = 0;
39 static uint_t min_length = 0;
40 static int exact_error_codes;
43 #define FILENAME "\\locktest.dat"
48 #define RANGE_MULTIPLE 1
50 #define NCONNECTIONS 2
52 #define LOCK_TIMEOUT 0
54 static struct cli_credentials *servers[NSERVERS];
56 enum lock_op {OP_LOCK, OP_UNLOCK, OP_REOPEN};
60 enum brl_type lock_type;
70 static struct record preset[] = {
71 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
72 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
73 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 0, 1},
74 {OP_UNLOCK, 0 , 0, 0, 2, 0, 1},
75 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
77 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
78 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
79 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
80 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
82 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
83 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 1, 1},
84 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
85 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
87 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
88 {OP_LOCK, WRITE_LOCK, 0, 0, 1, 1, 1},
89 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
90 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
92 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
93 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
94 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
95 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
97 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
98 {OP_LOCK, READ_LOCK, 0, 0, 3, 1, 1},
99 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
100 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
105 static struct record *recorded;
107 /*****************************************************
108 return a connection to a server
109 *******************************************************/
110 static struct smbcli_state *connect_one(struct loadparm_context *lp_ctx,
111 char *share, int snum, int conn)
113 struct smbcli_state *c;
114 fstring server, myname;
117 struct smbcli_options options;
119 lp_smbcli_options(lp_ctx, &options);
121 printf("connect_one(%s, %d, %d)\n", share, snum, conn);
123 fstrcpy(server,share+2);
124 share = strchr_m(server,'\\');
125 if (!share) return NULL;
130 char **unc_list = NULL;
133 p = lp_parm_string(lp_ctx, NULL, "torture", "unclist");
136 unc_list = file_lines_load(p, &num_unc_names, NULL);
137 if (!unc_list || num_unc_names <= 0) {
138 printf("Failed to load unc names list from '%s'\n", p);
142 if (!smbcli_parse_unc(unc_list[conn % num_unc_names],
144 printf("Failed to parse UNC name %s\n",
145 unc_list[conn % num_unc_names]);
154 slprintf(myname,sizeof(myname), "lock-%u-%u", getpid(), snum);
155 cli_credentials_set_workstation(servers[snum], myname, CRED_SPECIFIED);
158 printf("\\\\%s\\%s\n", server, share);
159 status = smbcli_full_connection(NULL, &c,
161 lp_smb_ports(lp_ctx),
164 lp_resolve_context(lp_ctx),
166 if (!NT_STATUS_IS_OK(status)) {
169 } while (!NT_STATUS_IS_OK(status) && retries--);
171 if (!NT_STATUS_IS_OK(status)) {
179 static void reconnect(struct loadparm_context *lp_ctx,
180 struct smbcli_state *cli[NSERVERS][NCONNECTIONS], int fnum[NSERVERS][NCONNECTIONS][NFILES],
181 char *share[NSERVERS])
185 for (server=0;server<NSERVERS;server++)
186 for (conn=0;conn<NCONNECTIONS;conn++) {
187 if (cli[server][conn]) {
188 for (f=0;f<NFILES;f++) {
189 if (fnum[server][conn][f] != -1) {
190 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
191 fnum[server][conn][f] = -1;
194 talloc_free(cli[server][conn]);
196 cli[server][conn] = connect_one(lp_ctx, share[server],
198 if (!cli[server][conn]) {
199 DEBUG(0,("Failed to connect to %s\n", share[server]));
207 static bool test_one(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
208 int fnum[NSERVERS][NCONNECTIONS][NFILES],
211 uint_t conn = rec->conn;
213 uint64_t start = rec->start;
214 uint64_t len = rec->len;
215 enum brl_type op = rec->lock_type;
218 NTSTATUS status[NSERVERS];
220 switch (rec->lock_op) {
223 for (server=0;server<NSERVERS;server++) {
225 struct smbcli_tree *tree=cli[server][conn]->tree;
226 int fn=fnum[server][conn][f];
228 if (!(tree->session->transport->negotiate.capabilities & CAP_LARGE_FILES)) {
229 res=smbcli_lock(tree, fn, start, len, LOCK_TIMEOUT, rec->lock_op);
231 union smb_lock parms;
233 struct smb_lock_entry lock[1];
235 parms.lockx.level = RAW_LOCK_LOCKX;
236 parms.lockx.in.file.fnum = fn;
238 ltype = (rec->lock_op == READ_LOCK? 1 : 0);
239 ltype |= LOCKING_ANDX_LARGE_FILES;
240 parms.lockx.in.mode = ltype;
241 parms.lockx.in.timeout = LOCK_TIMEOUT;
242 parms.lockx.in.ulock_cnt = 0;
243 parms.lockx.in.lock_cnt = 1;
244 lock[0].pid = rec->pid;
245 lock[0].offset = start;
247 parms.lockx.in.locks = &lock[0];
249 res = smb_raw_lock(tree, &parms);
252 ret[server] = NT_STATUS_IS_OK(res);
253 status[server] = smbcli_nt_error(cli[server][conn]->tree);
254 if (!exact_error_codes &&
255 NT_STATUS_EQUAL(status[server],
256 NT_STATUS_FILE_LOCK_CONFLICT)) {
257 status[server] = NT_STATUS_LOCK_NOT_GRANTED;
260 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) {
261 printf("lock conn=%u f=%u range=%.0f(%.0f) op=%s -> %s:%s\n",
263 (double)start, (double)len,
264 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
265 nt_errstr(status[0]), nt_errstr(status[1]));
267 if (!NT_STATUS_EQUAL(status[0],status[1])) return false;
272 for (server=0;server<NSERVERS;server++) {
274 struct smbcli_tree *tree=cli[server][conn]->tree;
275 int fn=fnum[server][conn][f];
278 if (!(tree->session->transport->negotiate.capabilities & CAP_LARGE_FILES)) {
279 res=smbcli_unlock(tree, fn, start, len);
281 union smb_lock parms;
282 struct smb_lock_entry lock[1];
284 parms.lockx.level = RAW_LOCK_LOCKX;
285 parms.lockx.in.file.fnum = fn;
286 parms.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
287 parms.lockx.in.timeout = 0;
288 parms.lockx.in.ulock_cnt = 1;
289 parms.lockx.in.lock_cnt = 0;
290 lock[0].pid = rec->pid;
292 lock[0].offset = start;
293 parms.lockx.in.locks = &lock[0];
295 res = smb_raw_lock(tree, &parms);
298 ret[server] = NT_STATUS_IS_OK(res);
299 status[server] = smbcli_nt_error(cli[server][conn]->tree);
302 (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))) {
303 printf("unlock conn=%u f=%u range=%.0f(%.0f) -> %s:%s\n",
305 (double)start, (double)len,
306 nt_errstr(status[0]), nt_errstr(status[1]));
308 if (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))
313 /* reopen the file */
314 for (server=0;server<NSERVERS;server++) {
315 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
316 fnum[server][conn][f] = -1;
318 for (server=0;server<NSERVERS;server++) {
319 fnum[server][conn][f] = smbcli_open(cli[server][conn]->tree, FILENAME,
322 if (fnum[server][conn][f] == -1) {
323 printf("failed to reopen on share%d\n", server);
328 printf("reopen conn=%u f=%u\n",
337 static void close_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
338 int fnum[NSERVERS][NCONNECTIONS][NFILES])
342 for (server=0;server<NSERVERS;server++)
343 for (conn=0;conn<NCONNECTIONS;conn++)
344 for (f=0;f<NFILES;f++) {
345 if (fnum[server][conn][f] != -1) {
346 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
347 fnum[server][conn][f] = -1;
350 for (server=0;server<NSERVERS;server++) {
351 smbcli_unlink(cli[server][0]->tree, FILENAME);
355 static void open_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
356 int fnum[NSERVERS][NCONNECTIONS][NFILES])
360 for (server=0;server<NSERVERS;server++)
361 for (conn=0;conn<NCONNECTIONS;conn++)
362 for (f=0;f<NFILES;f++) {
363 fnum[server][conn][f] = smbcli_open(cli[server][conn]->tree, FILENAME,
366 if (fnum[server][conn][f] == -1) {
367 fprintf(stderr,"Failed to open fnum[%u][%u][%u]\n",
375 static int retest(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
376 int fnum[NSERVERS][NCONNECTIONS][NFILES],
380 printf("testing %u ...\n", n);
381 for (i=0; i<n; i++) {
382 if (i && i % 100 == 0) {
386 if (recorded[i].needed &&
387 !test_one(cli, fnum, &recorded[i])) return i;
393 /* each server has two connections open to it. Each connection has two file
394 descriptors open on the file - 8 file descriptors in total
396 we then do random locking ops in tamdem on the 4 fnums from each
397 server and ensure that the results match
399 static int test_locks(struct loadparm_context *lp_ctx, char *share[NSERVERS])
401 struct smbcli_state *cli[NSERVERS][NCONNECTIONS];
402 int fnum[NSERVERS][NCONNECTIONS][NFILES];
403 int n, i, n1, skip, r1, r2;
408 recorded = malloc_array_p(struct record, numops);
410 for (n=0; n<numops; n++) {
412 if (n < sizeof(preset) / sizeof(preset[0])) {
413 recorded[n] = preset[n];
416 recorded[n].conn = random() % NCONNECTIONS;
417 recorded[n].f = random() % NFILES;
418 recorded[n].start = lock_base + ((uint_t)random() % (lock_range-1));
419 recorded[n].len = min_length +
420 random() % (lock_range-(recorded[n].start-lock_base));
421 recorded[n].start *= RANGE_MULTIPLE;
422 recorded[n].len *= RANGE_MULTIPLE;
423 recorded[n].pid = random()%3;
424 if (recorded[n].pid == 2) {
425 recorded[n].pid = 0xFFFF; /* see if its magic */
430 recorded[n].lock_type = READ_LOCK;
432 recorded[n].lock_type = WRITE_LOCK;
435 recorded[n].lock_op = OP_LOCK;
436 } else if (r2 < UNLOCK_PCT) {
437 recorded[n].lock_op = OP_UNLOCK;
439 recorded[n].lock_op = OP_REOPEN;
441 recorded[n].needed = true;
442 if (!zero_zero && recorded[n].start==0 && recorded[n].len==0) {
450 reconnect(lp_ctx, cli, fnum, share);
451 open_files(cli, fnum);
452 n = retest(cli, fnum, numops);
454 if (n == numops || !analyze) {
467 close_files(cli, fnum);
468 reconnect(lp_ctx, cli, fnum, share);
469 open_files(cli, fnum);
471 for (i=0;i<n-skip;i+=skip) {
473 printf("excluding %d-%d\n", i, i+skip-1);
474 for (j=i;j<i+skip;j++) {
475 recorded[j].needed = false;
478 close_files(cli, fnum);
479 open_files(cli, fnum);
481 m = retest(cli, fnum, n);
483 for (j=i;j<i+skip;j++) {
484 recorded[j].needed = true;
487 if (i+(skip-1) < m) {
488 memmove(&recorded[i], &recorded[i+skip],
489 (m-(i+skip-1))*sizeof(recorded[0]));
498 printf("skip=%d\n", skip);
505 close_files(cli, fnum);
506 reconnect(lp_ctx, cli, fnum, share);
507 open_files(cli, fnum);
509 n1 = retest(cli, fnum, n);
511 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
513 close_files(cli, fnum);
516 printf("{%d, %d, %u, %u, %.0f, %.0f, %u},\n",
518 recorded[i].lock_type,
521 (double)recorded[i].start,
522 (double)recorded[i].len,
531 static void usage(poptContext pc)
533 printf("Usage:\n\tlocktest //server1/share1 //server2/share2 [options..]\n");
534 poptPrintUsage(pc, stdout, 0);
537 /****************************************************************************
539 ****************************************************************************/
540 int main(int argc,char *argv[])
542 char *share[NSERVERS];
545 int username_count=0;
546 struct loadparm_context *lp_ctx;
550 enum {OPT_UNCLIST=1000};
551 struct poptOption long_options[] = {
553 {"seed", 0, POPT_ARG_INT, &seed, 0, "Seed to use for randomizer", NULL},
554 {"num-ops", 0, POPT_ARG_INT, &numops, 0, "num ops", NULL},
555 {"lockrange", 0, POPT_ARG_INT, &lock_range,0, "locking range", NULL},
556 {"lockbase", 0, POPT_ARG_INT, &lock_base, 0, "locking base", NULL},
557 {"minlength", 0, POPT_ARG_INT, &min_length,0, "min lock length", NULL},
558 {"hidefails", 0, POPT_ARG_NONE, &hide_unlock_fails,0,"hide unlock fails", NULL},
559 {"oplocks", 0, POPT_ARG_NONE, &use_oplocks,0, "use oplocks", NULL},
560 {"showall", 0, POPT_ARG_NONE, &showall, 0, "display all operations", NULL},
561 {"analyse", 0, POPT_ARG_NONE, &analyze, 0, "do backtrack analysis", NULL},
562 {"zerozero", 0, POPT_ARG_NONE, &zero_zero, 0, "do zero/zero lock", NULL},
563 {"exacterrors", 0, POPT_ARG_NONE, &exact_error_codes,0,"use exact error codes", NULL},
564 {"unclist", 0, POPT_ARG_STRING, NULL, OPT_UNCLIST, "unclist", NULL},
565 { "user", 'U', POPT_ARG_STRING, NULL, 'U', "Set the network username", "[DOMAIN/]USERNAME[%PASSWORD]" },
567 POPT_COMMON_CONNECTION
568 POPT_COMMON_CREDENTIALS
576 pc = poptGetContext("locktest", argc, (const char **) argv, long_options,
577 POPT_CONTEXT_KEEP_FIRST);
579 poptSetOtherOptionHelp(pc, "<unc1> <unc2>");
581 lp_ctx = cmdline_lp_ctx;
582 servers[0] = cli_credentials_init(talloc_autofree_context());
583 servers[1] = cli_credentials_init(talloc_autofree_context());
584 cli_credentials_guess(servers[0], lp_ctx);
585 cli_credentials_guess(servers[1], lp_ctx);
587 while((opt = poptGetNextOpt(pc)) != -1) {
590 lp_set_cmdline(cmdline_lp_ctx, "torture:unclist", poptGetOptArg(pc));
593 if (username_count == 2) {
597 cli_credentials_parse_string(servers[username_count], poptGetOptArg(pc), CRED_SPECIFIED);
603 argv_new = discard_const_p(char *, poptGetArgs(pc));
605 for (i=0; i<argc; i++) {
606 if (argv_new[i] == NULL) {
612 if (!(argc_new >= 3)) {
617 setup_logging("locktest", DEBUG_STDOUT);
619 for (server=0;server<NSERVERS;server++) {
620 share[server] = argv_new[1+server];
621 all_string_sub(share[server],"/","\\",0);
624 lp_ctx = cmdline_lp_ctx;
626 if (username_count == 0) {
630 if (username_count == 1) {
631 servers[1] = servers[0];
636 DEBUG(0,("seed=%u base=%d range=%d min_length=%d\n",
637 seed, lock_base, lock_range, min_length));
640 return test_locks(lp_ctx, share);