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"
30 static int numops = 1000;
33 static bool hide_unlock_fails;
34 static bool use_oplocks;
35 static uint_t lock_range = 100;
36 static uint_t lock_base = 0;
37 static uint_t min_length = 0;
38 static bool exact_error_codes;
39 static bool zero_zero;
41 #define FILENAME "\\locktest.dat"
46 #define RANGE_MULTIPLE 1
48 #define NCONNECTIONS 2
50 #define LOCK_TIMEOUT 0
52 static struct cli_credentials *servers[NSERVERS];
54 enum lock_op {OP_LOCK, OP_UNLOCK, OP_REOPEN};
58 enum brl_type lock_type;
68 static struct record preset[] = {
69 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
70 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
71 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 0, 1},
72 {OP_UNLOCK, 0 , 0, 0, 2, 0, 1},
73 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
75 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
76 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
77 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
78 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
80 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
81 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 1, 1},
82 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
83 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
85 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
86 {OP_LOCK, WRITE_LOCK, 0, 0, 1, 1, 1},
87 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
88 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
90 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
91 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
92 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
93 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
95 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
96 {OP_LOCK, READ_LOCK, 0, 0, 3, 1, 1},
97 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
98 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
103 static struct record *recorded;
105 /*****************************************************
106 return a connection to a server
107 *******************************************************/
108 static struct smbcli_state *connect_one(struct loadparm_context *lp_ctx,
109 char *share, int snum, int conn)
111 struct smbcli_state *c;
112 fstring server, myname;
116 printf("connect_one(%s, %d, %d)\n", share, snum, conn);
118 fstrcpy(server,share+2);
119 share = strchr_m(server,'\\');
120 if (!share) return NULL;
125 char **unc_list = NULL;
128 p = lp_parm_string(lp_ctx, NULL, "torture", "unclist");
131 unc_list = file_lines_load(p, &num_unc_names, NULL);
132 if (!unc_list || num_unc_names <= 0) {
133 printf("Failed to load unc names list from '%s'\n", p);
137 if (!smbcli_parse_unc(unc_list[conn % num_unc_names],
139 printf("Failed to parse UNC name %s\n",
140 unc_list[conn % num_unc_names]);
149 slprintf(myname,sizeof(myname), "lock-%u-%u", getpid(), snum);
150 cli_credentials_set_workstation(servers[snum], myname, CRED_SPECIFIED);
153 printf("\\\\%s\\%s\n", server, share);
154 status = smbcli_full_connection(NULL, &c,
157 servers[snum], NULL);
158 if (!NT_STATUS_IS_OK(status)) {
161 } while (!NT_STATUS_IS_OK(status) && retries--);
163 if (!NT_STATUS_IS_OK(status)) {
171 static void reconnect(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], int fnum[NSERVERS][NCONNECTIONS][NFILES],
172 char *share[NSERVERS])
176 for (server=0;server<NSERVERS;server++)
177 for (conn=0;conn<NCONNECTIONS;conn++) {
178 if (cli[server][conn]) {
179 for (f=0;f<NFILES;f++) {
180 if (fnum[server][conn][f] != -1) {
181 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
182 fnum[server][conn][f] = -1;
185 talloc_free(cli[server][conn]);
187 cli[server][conn] = connect_one(global_loadparm, share[server],
189 if (!cli[server][conn]) {
190 DEBUG(0,("Failed to connect to %s\n", share[server]));
198 static bool test_one(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
199 int fnum[NSERVERS][NCONNECTIONS][NFILES],
202 uint_t conn = rec->conn;
204 uint64_t start = rec->start;
205 uint64_t len = rec->len;
206 enum brl_type op = rec->lock_type;
209 NTSTATUS status[NSERVERS];
211 switch (rec->lock_op) {
214 for (server=0;server<NSERVERS;server++) {
216 struct smbcli_tree *tree=cli[server][conn]->tree;
217 int fn=fnum[server][conn][f];
219 if (!(tree->session->transport->negotiate.capabilities & CAP_LARGE_FILES)) {
220 res=smbcli_lock(tree, fn, start, len, LOCK_TIMEOUT, rec->lock_op);
222 union smb_lock parms;
224 struct smb_lock_entry lock[1];
226 parms.lockx.level = RAW_LOCK_LOCKX;
227 parms.lockx.in.file.fnum = fn;
229 ltype = (rec->lock_op == READ_LOCK? 1 : 0);
230 ltype |= LOCKING_ANDX_LARGE_FILES;
231 parms.lockx.in.mode = ltype;
232 parms.lockx.in.timeout = LOCK_TIMEOUT;
233 parms.lockx.in.ulock_cnt = 0;
234 parms.lockx.in.lock_cnt = 1;
235 lock[0].pid = rec->pid;
236 lock[0].offset = start;
238 parms.lockx.in.locks = &lock[0];
240 res = smb_raw_lock(tree, &parms);
243 ret[server] = NT_STATUS_IS_OK(res);
244 status[server] = smbcli_nt_error(cli[server][conn]->tree);
245 if (!exact_error_codes &&
246 NT_STATUS_EQUAL(status[server],
247 NT_STATUS_FILE_LOCK_CONFLICT)) {
248 status[server] = NT_STATUS_LOCK_NOT_GRANTED;
251 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) {
252 printf("lock conn=%u f=%u range=%.0f(%.0f) op=%s -> %s:%s\n",
254 (double)start, (double)len,
255 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
256 nt_errstr(status[0]), nt_errstr(status[1]));
258 if (!NT_STATUS_EQUAL(status[0],status[1])) return false;
263 for (server=0;server<NSERVERS;server++) {
265 struct smbcli_tree *tree=cli[server][conn]->tree;
266 int fn=fnum[server][conn][f];
269 if (!(tree->session->transport->negotiate.capabilities & CAP_LARGE_FILES)) {
270 res=smbcli_unlock(tree, fn, start, len);
272 union smb_lock parms;
273 struct smb_lock_entry lock[1];
275 parms.lockx.level = RAW_LOCK_LOCKX;
276 parms.lockx.in.file.fnum = fn;
277 parms.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
278 parms.lockx.in.timeout = 0;
279 parms.lockx.in.ulock_cnt = 1;
280 parms.lockx.in.lock_cnt = 0;
281 lock[0].pid = rec->pid;
283 lock[0].offset = start;
284 parms.lockx.in.locks = &lock[0];
286 res = smb_raw_lock(tree, &parms);
289 ret[server] = NT_STATUS_IS_OK(res);
290 status[server] = smbcli_nt_error(cli[server][conn]->tree);
293 (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))) {
294 printf("unlock conn=%u f=%u range=%.0f(%.0f) -> %s:%s\n",
296 (double)start, (double)len,
297 nt_errstr(status[0]), nt_errstr(status[1]));
299 if (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))
304 /* reopen the file */
305 for (server=0;server<NSERVERS;server++) {
306 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
307 fnum[server][conn][f] = -1;
309 for (server=0;server<NSERVERS;server++) {
310 fnum[server][conn][f] = smbcli_open(cli[server][conn]->tree, FILENAME,
313 if (fnum[server][conn][f] == -1) {
314 printf("failed to reopen on share%d\n", server);
319 printf("reopen conn=%u f=%u\n",
328 static void close_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
329 int fnum[NSERVERS][NCONNECTIONS][NFILES])
333 for (server=0;server<NSERVERS;server++)
334 for (conn=0;conn<NCONNECTIONS;conn++)
335 for (f=0;f<NFILES;f++) {
336 if (fnum[server][conn][f] != -1) {
337 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
338 fnum[server][conn][f] = -1;
341 for (server=0;server<NSERVERS;server++) {
342 smbcli_unlink(cli[server][0]->tree, FILENAME);
346 static void open_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
347 int fnum[NSERVERS][NCONNECTIONS][NFILES])
351 for (server=0;server<NSERVERS;server++)
352 for (conn=0;conn<NCONNECTIONS;conn++)
353 for (f=0;f<NFILES;f++) {
354 fnum[server][conn][f] = smbcli_open(cli[server][conn]->tree, FILENAME,
357 if (fnum[server][conn][f] == -1) {
358 fprintf(stderr,"Failed to open fnum[%u][%u][%u]\n",
366 static int retest(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
367 int fnum[NSERVERS][NCONNECTIONS][NFILES],
371 printf("testing %u ...\n", n);
372 for (i=0; i<n; i++) {
373 if (i && i % 100 == 0) {
377 if (recorded[i].needed &&
378 !test_one(cli, fnum, &recorded[i])) return i;
384 /* each server has two connections open to it. Each connection has two file
385 descriptors open on the file - 8 file descriptors in total
387 we then do random locking ops in tamdem on the 4 fnums from each
388 server and ensure that the results match
390 static void test_locks(char *share[NSERVERS])
392 struct smbcli_state *cli[NSERVERS][NCONNECTIONS];
393 int fnum[NSERVERS][NCONNECTIONS][NFILES];
394 int n, i, n1, skip, r1, r2;
399 recorded = malloc_array_p(struct record, numops);
401 for (n=0; n<numops; n++) {
403 if (n < sizeof(preset) / sizeof(preset[0])) {
404 recorded[n] = preset[n];
407 recorded[n].conn = random() % NCONNECTIONS;
408 recorded[n].f = random() % NFILES;
409 recorded[n].start = lock_base + ((uint_t)random() % (lock_range-1));
410 recorded[n].len = min_length +
411 random() % (lock_range-(recorded[n].start-lock_base));
412 recorded[n].start *= RANGE_MULTIPLE;
413 recorded[n].len *= RANGE_MULTIPLE;
414 recorded[n].pid = random()%3;
415 if (recorded[n].pid == 2) {
416 recorded[n].pid = 0xFFFF; /* see if its magic */
421 recorded[n].lock_type = READ_LOCK;
423 recorded[n].lock_type = WRITE_LOCK;
426 recorded[n].lock_op = OP_LOCK;
427 } else if (r2 < UNLOCK_PCT) {
428 recorded[n].lock_op = OP_UNLOCK;
430 recorded[n].lock_op = OP_REOPEN;
432 recorded[n].needed = true;
433 if (!zero_zero && recorded[n].start==0 && recorded[n].len==0) {
441 reconnect(cli, fnum, share);
442 open_files(cli, fnum);
443 n = retest(cli, fnum, numops);
445 if (n == numops || !analyze) return;
453 close_files(cli, fnum);
454 reconnect(cli, fnum, share);
455 open_files(cli, fnum);
457 for (i=0;i<n-skip;i+=skip) {
459 printf("excluding %d-%d\n", i, i+skip-1);
460 for (j=i;j<i+skip;j++) {
461 recorded[j].needed = false;
464 close_files(cli, fnum);
465 open_files(cli, fnum);
467 m = retest(cli, fnum, n);
469 for (j=i;j<i+skip;j++) {
470 recorded[j].needed = true;
473 if (i+(skip-1) < m) {
474 memmove(&recorded[i], &recorded[i+skip],
475 (m-(i+skip-1))*sizeof(recorded[0]));
484 printf("skip=%d\n", skip);
491 close_files(cli, fnum);
492 reconnect(cli, fnum, share);
493 open_files(cli, fnum);
495 n1 = retest(cli, fnum, n);
497 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
499 close_files(cli, fnum);
502 printf("{%d, %d, %u, %u, %.0f, %.0f, %u},\n",
504 recorded[i].lock_type,
507 (double)recorded[i].start,
508 (double)recorded[i].len,
515 static void usage(void)
519 locktest //server1/share1 //server2/share2 [options..]\n\
521 -U user%%pass (may be specified twice)\n\
524 -u hide unlock fails\n\
526 -A analyse for minimal ops\n\
528 -E enable exact error code checking\n\
529 -Z enable the zero/zero lock\n\
530 -R range set lock range\n\
531 -B base set lock base\n\
532 -M min set min lock length\n\
533 -l filename unclist file\n\
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 setup_logging("locktest", DEBUG_STDOUT);
552 if (argc < 3 || argv[1][0] == '-') {
557 setup_logging(argv[0], DEBUG_STDOUT);
559 for (server=0;server<NSERVERS;server++) {
560 share[server] = argv[1+server];
561 all_string_sub(share[server],"/","\\",0);
567 lp_load(dyn_CONFIGFILE, &lp_ctx);
569 servers[0] = cli_credentials_init(talloc_autofree_context());
570 servers[1] = cli_credentials_init(talloc_autofree_context());
571 cli_credentials_guess(servers[0], lp_ctx);
572 cli_credentials_guess(servers[1], lp_ctx);
576 while ((opt = getopt(argc, argv, "U:s:ho:aAW:OR:B:M:EZW:l:")) != EOF) {
579 if (username_count == 2) {
583 cli_credentials_parse_string(servers[username_count],
584 optarg, CRED_SPECIFIED);
588 lock_range = strtol(optarg, NULL, 0);
591 lock_base = strtol(optarg, NULL, 0);
594 min_length = strtol(optarg, NULL, 0);
600 hide_unlock_fails = true;
603 numops = atoi(optarg);
618 exact_error_codes = true;
621 lp_set_cmdline(lp_ctx, "torture:unclist", optarg);
624 lp_set_cmdline(lp_ctx, "workgroup", optarg);
630 printf("Unknown option %c (%d)\n", (char)opt, opt);
635 if (username_count == 0) {
639 if (username_count == 1) {
640 servers[1] = servers[0];
648 DEBUG(0,("seed=%u base=%d range=%d min_length=%d\n",
649 seed, lock_base, lock_range, min_length));