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 "system/filesys.h"
22 #include "system/time.h"
24 #include "auth/credentials/credentials.h"
25 #include "auth/gensec/gensec.h"
26 #include "libcli/libcli.h"
27 #include "param/param.h"
28 #include "dynconfig.h"
29 #include "libcli/resolve/resolve.h"
31 static int numops = 1000;
34 static bool hide_unlock_fails;
35 static bool use_oplocks;
36 static uint_t lock_range = 100;
37 static uint_t lock_base = 0;
38 static uint_t min_length = 0;
39 static bool exact_error_codes;
40 static bool zero_zero;
42 #define FILENAME "\\locktest.dat"
47 #define RANGE_MULTIPLE 1
49 #define NCONNECTIONS 2
51 #define LOCK_TIMEOUT 0
53 static struct cli_credentials *servers[NSERVERS];
55 enum lock_op {OP_LOCK, OP_UNLOCK, OP_REOPEN};
59 enum brl_type lock_type;
69 static struct record preset[] = {
70 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
71 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
72 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 0, 1},
73 {OP_UNLOCK, 0 , 0, 0, 2, 0, 1},
74 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
76 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
77 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
78 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
79 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
81 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
82 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 1, 1},
83 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
84 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
86 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
87 {OP_LOCK, WRITE_LOCK, 0, 0, 1, 1, 1},
88 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
89 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
91 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
92 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
93 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
94 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
96 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
97 {OP_LOCK, READ_LOCK, 0, 0, 3, 1, 1},
98 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
99 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
104 static struct record *recorded;
106 /*****************************************************
107 return a connection to a server
108 *******************************************************/
109 static struct smbcli_state *connect_one(struct loadparm_context *lp_ctx,
110 char *share, int snum, int conn)
112 struct smbcli_state *c;
113 fstring server, myname;
116 struct smbcli_options options;
118 lp_smbcli_options(lp_ctx, &options);
120 printf("connect_one(%s, %d, %d)\n", share, snum, conn);
122 fstrcpy(server,share+2);
123 share = strchr_m(server,'\\');
124 if (!share) return NULL;
129 char **unc_list = NULL;
132 p = lp_parm_string(lp_ctx, NULL, "torture", "unclist");
135 unc_list = file_lines_load(p, &num_unc_names, NULL);
136 if (!unc_list || num_unc_names <= 0) {
137 printf("Failed to load unc names list from '%s'\n", p);
141 if (!smbcli_parse_unc(unc_list[conn % num_unc_names],
143 printf("Failed to parse UNC name %s\n",
144 unc_list[conn % num_unc_names]);
153 slprintf(myname,sizeof(myname), "lock-%u-%u", getpid(), snum);
154 cli_credentials_set_workstation(servers[snum], myname, CRED_SPECIFIED);
157 printf("\\\\%s\\%s\n", server, share);
158 status = smbcli_full_connection(NULL, &c,
160 lp_smb_ports(lp_ctx),
163 lp_resolve_context(lp_ctx),
165 if (!NT_STATUS_IS_OK(status)) {
168 } while (!NT_STATUS_IS_OK(status) && retries--);
170 if (!NT_STATUS_IS_OK(status)) {
178 static void reconnect(struct loadparm_context *lp_ctx,
179 struct smbcli_state *cli[NSERVERS][NCONNECTIONS], int fnum[NSERVERS][NCONNECTIONS][NFILES],
180 char *share[NSERVERS])
184 for (server=0;server<NSERVERS;server++)
185 for (conn=0;conn<NCONNECTIONS;conn++) {
186 if (cli[server][conn]) {
187 for (f=0;f<NFILES;f++) {
188 if (fnum[server][conn][f] != -1) {
189 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
190 fnum[server][conn][f] = -1;
193 talloc_free(cli[server][conn]);
195 cli[server][conn] = connect_one(lp_ctx, share[server],
197 if (!cli[server][conn]) {
198 DEBUG(0,("Failed to connect to %s\n", share[server]));
206 static bool test_one(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
207 int fnum[NSERVERS][NCONNECTIONS][NFILES],
210 uint_t conn = rec->conn;
212 uint64_t start = rec->start;
213 uint64_t len = rec->len;
214 enum brl_type op = rec->lock_type;
217 NTSTATUS status[NSERVERS];
219 switch (rec->lock_op) {
222 for (server=0;server<NSERVERS;server++) {
224 struct smbcli_tree *tree=cli[server][conn]->tree;
225 int fn=fnum[server][conn][f];
227 if (!(tree->session->transport->negotiate.capabilities & CAP_LARGE_FILES)) {
228 res=smbcli_lock(tree, fn, start, len, LOCK_TIMEOUT, rec->lock_op);
230 union smb_lock parms;
232 struct smb_lock_entry lock[1];
234 parms.lockx.level = RAW_LOCK_LOCKX;
235 parms.lockx.in.file.fnum = fn;
237 ltype = (rec->lock_op == READ_LOCK? 1 : 0);
238 ltype |= LOCKING_ANDX_LARGE_FILES;
239 parms.lockx.in.mode = ltype;
240 parms.lockx.in.timeout = LOCK_TIMEOUT;
241 parms.lockx.in.ulock_cnt = 0;
242 parms.lockx.in.lock_cnt = 1;
243 lock[0].pid = rec->pid;
244 lock[0].offset = start;
246 parms.lockx.in.locks = &lock[0];
248 res = smb_raw_lock(tree, &parms);
251 ret[server] = NT_STATUS_IS_OK(res);
252 status[server] = smbcli_nt_error(cli[server][conn]->tree);
253 if (!exact_error_codes &&
254 NT_STATUS_EQUAL(status[server],
255 NT_STATUS_FILE_LOCK_CONFLICT)) {
256 status[server] = NT_STATUS_LOCK_NOT_GRANTED;
259 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) {
260 printf("lock conn=%u f=%u range=%.0f(%.0f) op=%s -> %s:%s\n",
262 (double)start, (double)len,
263 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
264 nt_errstr(status[0]), nt_errstr(status[1]));
266 if (!NT_STATUS_EQUAL(status[0],status[1])) return false;
271 for (server=0;server<NSERVERS;server++) {
273 struct smbcli_tree *tree=cli[server][conn]->tree;
274 int fn=fnum[server][conn][f];
277 if (!(tree->session->transport->negotiate.capabilities & CAP_LARGE_FILES)) {
278 res=smbcli_unlock(tree, fn, start, len);
280 union smb_lock parms;
281 struct smb_lock_entry lock[1];
283 parms.lockx.level = RAW_LOCK_LOCKX;
284 parms.lockx.in.file.fnum = fn;
285 parms.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
286 parms.lockx.in.timeout = 0;
287 parms.lockx.in.ulock_cnt = 1;
288 parms.lockx.in.lock_cnt = 0;
289 lock[0].pid = rec->pid;
291 lock[0].offset = start;
292 parms.lockx.in.locks = &lock[0];
294 res = smb_raw_lock(tree, &parms);
297 ret[server] = NT_STATUS_IS_OK(res);
298 status[server] = smbcli_nt_error(cli[server][conn]->tree);
301 (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))) {
302 printf("unlock conn=%u f=%u range=%.0f(%.0f) -> %s:%s\n",
304 (double)start, (double)len,
305 nt_errstr(status[0]), nt_errstr(status[1]));
307 if (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))
312 /* reopen the file */
313 for (server=0;server<NSERVERS;server++) {
314 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
315 fnum[server][conn][f] = -1;
317 for (server=0;server<NSERVERS;server++) {
318 fnum[server][conn][f] = smbcli_open(cli[server][conn]->tree, FILENAME,
321 if (fnum[server][conn][f] == -1) {
322 printf("failed to reopen on share%d\n", server);
327 printf("reopen conn=%u f=%u\n",
336 static void close_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
337 int fnum[NSERVERS][NCONNECTIONS][NFILES])
341 for (server=0;server<NSERVERS;server++)
342 for (conn=0;conn<NCONNECTIONS;conn++)
343 for (f=0;f<NFILES;f++) {
344 if (fnum[server][conn][f] != -1) {
345 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
346 fnum[server][conn][f] = -1;
349 for (server=0;server<NSERVERS;server++) {
350 smbcli_unlink(cli[server][0]->tree, FILENAME);
354 static void open_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
355 int fnum[NSERVERS][NCONNECTIONS][NFILES])
359 for (server=0;server<NSERVERS;server++)
360 for (conn=0;conn<NCONNECTIONS;conn++)
361 for (f=0;f<NFILES;f++) {
362 fnum[server][conn][f] = smbcli_open(cli[server][conn]->tree, FILENAME,
365 if (fnum[server][conn][f] == -1) {
366 fprintf(stderr,"Failed to open fnum[%u][%u][%u]\n",
374 static int retest(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
375 int fnum[NSERVERS][NCONNECTIONS][NFILES],
379 printf("testing %u ...\n", n);
380 for (i=0; i<n; i++) {
381 if (i && i % 100 == 0) {
385 if (recorded[i].needed &&
386 !test_one(cli, fnum, &recorded[i])) return i;
392 /* each server has two connections open to it. Each connection has two file
393 descriptors open on the file - 8 file descriptors in total
395 we then do random locking ops in tamdem on the 4 fnums from each
396 server and ensure that the results match
398 static void test_locks(struct loadparm_context *lp_ctx, char *share[NSERVERS])
400 struct smbcli_state *cli[NSERVERS][NCONNECTIONS];
401 int fnum[NSERVERS][NCONNECTIONS][NFILES];
402 int n, i, n1, skip, r1, r2;
407 recorded = malloc_array_p(struct record, numops);
409 for (n=0; n<numops; n++) {
411 if (n < sizeof(preset) / sizeof(preset[0])) {
412 recorded[n] = preset[n];
415 recorded[n].conn = random() % NCONNECTIONS;
416 recorded[n].f = random() % NFILES;
417 recorded[n].start = lock_base + ((uint_t)random() % (lock_range-1));
418 recorded[n].len = min_length +
419 random() % (lock_range-(recorded[n].start-lock_base));
420 recorded[n].start *= RANGE_MULTIPLE;
421 recorded[n].len *= RANGE_MULTIPLE;
422 recorded[n].pid = random()%3;
423 if (recorded[n].pid == 2) {
424 recorded[n].pid = 0xFFFF; /* see if its magic */
429 recorded[n].lock_type = READ_LOCK;
431 recorded[n].lock_type = WRITE_LOCK;
434 recorded[n].lock_op = OP_LOCK;
435 } else if (r2 < UNLOCK_PCT) {
436 recorded[n].lock_op = OP_UNLOCK;
438 recorded[n].lock_op = OP_REOPEN;
440 recorded[n].needed = true;
441 if (!zero_zero && recorded[n].start==0 && recorded[n].len==0) {
449 reconnect(lp_ctx, cli, fnum, share);
450 open_files(cli, fnum);
451 n = retest(cli, fnum, numops);
453 if (n == numops || !analyze) return;
461 close_files(cli, fnum);
462 reconnect(lp_ctx, cli, fnum, share);
463 open_files(cli, fnum);
465 for (i=0;i<n-skip;i+=skip) {
467 printf("excluding %d-%d\n", i, i+skip-1);
468 for (j=i;j<i+skip;j++) {
469 recorded[j].needed = false;
472 close_files(cli, fnum);
473 open_files(cli, fnum);
475 m = retest(cli, fnum, n);
477 for (j=i;j<i+skip;j++) {
478 recorded[j].needed = true;
481 if (i+(skip-1) < m) {
482 memmove(&recorded[i], &recorded[i+skip],
483 (m-(i+skip-1))*sizeof(recorded[0]));
492 printf("skip=%d\n", skip);
499 close_files(cli, fnum);
500 reconnect(lp_ctx, cli, fnum, share);
501 open_files(cli, fnum);
503 n1 = retest(cli, fnum, n);
505 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
507 close_files(cli, fnum);
510 printf("{%d, %d, %u, %u, %.0f, %.0f, %u},\n",
512 recorded[i].lock_type,
515 (double)recorded[i].start,
516 (double)recorded[i].len,
523 static void usage(void)
527 locktest //server1/share1 //server2/share2 [options..]\n\
529 -U user%%pass (may be specified twice)\n\
532 -u hide unlock fails\n\
534 -A analyse for minimal ops\n\
536 -E enable exact error code checking\n\
537 -Z enable the zero/zero lock\n\
538 -R range set lock range\n\
539 -B base set lock base\n\
540 -M min set min lock length\n\
541 -l filename unclist file\n\
545 /****************************************************************************
547 ****************************************************************************/
548 int main(int argc,char *argv[])
550 char *share[NSERVERS];
553 int username_count=0;
554 struct loadparm_context *lp_ctx;
558 setup_logging("locktest", DEBUG_STDOUT);
560 if (argc < 3 || argv[1][0] == '-') {
565 setup_logging(argv[0], DEBUG_STDOUT);
567 for (server=0;server<NSERVERS;server++) {
568 share[server] = argv[1+server];
569 all_string_sub(share[server],"/","\\",0);
575 lp_ctx = loadparm_init(talloc_autofree_context());
576 lp_load(lp_ctx, dyn_CONFIGFILE);
578 servers[0] = cli_credentials_init(talloc_autofree_context());
579 servers[1] = cli_credentials_init(talloc_autofree_context());
580 cli_credentials_guess(servers[0], lp_ctx);
581 cli_credentials_guess(servers[1], lp_ctx);
585 while ((opt = getopt(argc, argv, "U:s:ho:aAW:OR:B:M:EZW:l:")) != EOF) {
588 if (username_count == 2) {
592 cli_credentials_parse_string(servers[username_count],
593 optarg, CRED_SPECIFIED);
597 lock_range = strtol(optarg, NULL, 0);
600 lock_base = strtol(optarg, NULL, 0);
603 min_length = strtol(optarg, NULL, 0);
609 hide_unlock_fails = true;
612 numops = atoi(optarg);
627 exact_error_codes = true;
630 lp_set_cmdline(lp_ctx, "torture:unclist", optarg);
633 lp_set_cmdline(lp_ctx, "workgroup", optarg);
639 printf("Unknown option %c (%d)\n", (char)opt, opt);
644 if (username_count == 0) {
648 if (username_count == 1) {
649 servers[1] = servers[0];
657 DEBUG(0,("seed=%u base=%d range=%d min_length=%d\n",
658 seed, lock_base, lock_range, min_length));
661 test_locks(lp_ctx, share);