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 loadparm_context *lp_ctx,
172 struct smbcli_state *cli[NSERVERS][NCONNECTIONS], int fnum[NSERVERS][NCONNECTIONS][NFILES],
173 char *share[NSERVERS])
177 for (server=0;server<NSERVERS;server++)
178 for (conn=0;conn<NCONNECTIONS;conn++) {
179 if (cli[server][conn]) {
180 for (f=0;f<NFILES;f++) {
181 if (fnum[server][conn][f] != -1) {
182 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
183 fnum[server][conn][f] = -1;
186 talloc_free(cli[server][conn]);
188 cli[server][conn] = connect_one(lp_ctx, share[server],
190 if (!cli[server][conn]) {
191 DEBUG(0,("Failed to connect to %s\n", share[server]));
199 static bool test_one(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
200 int fnum[NSERVERS][NCONNECTIONS][NFILES],
203 uint_t conn = rec->conn;
205 uint64_t start = rec->start;
206 uint64_t len = rec->len;
207 enum brl_type op = rec->lock_type;
210 NTSTATUS status[NSERVERS];
212 switch (rec->lock_op) {
215 for (server=0;server<NSERVERS;server++) {
217 struct smbcli_tree *tree=cli[server][conn]->tree;
218 int fn=fnum[server][conn][f];
220 if (!(tree->session->transport->negotiate.capabilities & CAP_LARGE_FILES)) {
221 res=smbcli_lock(tree, fn, start, len, LOCK_TIMEOUT, rec->lock_op);
223 union smb_lock parms;
225 struct smb_lock_entry lock[1];
227 parms.lockx.level = RAW_LOCK_LOCKX;
228 parms.lockx.in.file.fnum = fn;
230 ltype = (rec->lock_op == READ_LOCK? 1 : 0);
231 ltype |= LOCKING_ANDX_LARGE_FILES;
232 parms.lockx.in.mode = ltype;
233 parms.lockx.in.timeout = LOCK_TIMEOUT;
234 parms.lockx.in.ulock_cnt = 0;
235 parms.lockx.in.lock_cnt = 1;
236 lock[0].pid = rec->pid;
237 lock[0].offset = start;
239 parms.lockx.in.locks = &lock[0];
241 res = smb_raw_lock(tree, &parms);
244 ret[server] = NT_STATUS_IS_OK(res);
245 status[server] = smbcli_nt_error(cli[server][conn]->tree);
246 if (!exact_error_codes &&
247 NT_STATUS_EQUAL(status[server],
248 NT_STATUS_FILE_LOCK_CONFLICT)) {
249 status[server] = NT_STATUS_LOCK_NOT_GRANTED;
252 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) {
253 printf("lock conn=%u f=%u range=%.0f(%.0f) op=%s -> %s:%s\n",
255 (double)start, (double)len,
256 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
257 nt_errstr(status[0]), nt_errstr(status[1]));
259 if (!NT_STATUS_EQUAL(status[0],status[1])) return false;
264 for (server=0;server<NSERVERS;server++) {
266 struct smbcli_tree *tree=cli[server][conn]->tree;
267 int fn=fnum[server][conn][f];
270 if (!(tree->session->transport->negotiate.capabilities & CAP_LARGE_FILES)) {
271 res=smbcli_unlock(tree, fn, start, len);
273 union smb_lock parms;
274 struct smb_lock_entry lock[1];
276 parms.lockx.level = RAW_LOCK_LOCKX;
277 parms.lockx.in.file.fnum = fn;
278 parms.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
279 parms.lockx.in.timeout = 0;
280 parms.lockx.in.ulock_cnt = 1;
281 parms.lockx.in.lock_cnt = 0;
282 lock[0].pid = rec->pid;
284 lock[0].offset = start;
285 parms.lockx.in.locks = &lock[0];
287 res = smb_raw_lock(tree, &parms);
290 ret[server] = NT_STATUS_IS_OK(res);
291 status[server] = smbcli_nt_error(cli[server][conn]->tree);
294 (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))) {
295 printf("unlock conn=%u f=%u range=%.0f(%.0f) -> %s:%s\n",
297 (double)start, (double)len,
298 nt_errstr(status[0]), nt_errstr(status[1]));
300 if (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))
305 /* reopen the file */
306 for (server=0;server<NSERVERS;server++) {
307 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
308 fnum[server][conn][f] = -1;
310 for (server=0;server<NSERVERS;server++) {
311 fnum[server][conn][f] = smbcli_open(cli[server][conn]->tree, FILENAME,
314 if (fnum[server][conn][f] == -1) {
315 printf("failed to reopen on share%d\n", server);
320 printf("reopen conn=%u f=%u\n",
329 static void close_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
330 int fnum[NSERVERS][NCONNECTIONS][NFILES])
334 for (server=0;server<NSERVERS;server++)
335 for (conn=0;conn<NCONNECTIONS;conn++)
336 for (f=0;f<NFILES;f++) {
337 if (fnum[server][conn][f] != -1) {
338 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
339 fnum[server][conn][f] = -1;
342 for (server=0;server<NSERVERS;server++) {
343 smbcli_unlink(cli[server][0]->tree, FILENAME);
347 static void open_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
348 int fnum[NSERVERS][NCONNECTIONS][NFILES])
352 for (server=0;server<NSERVERS;server++)
353 for (conn=0;conn<NCONNECTIONS;conn++)
354 for (f=0;f<NFILES;f++) {
355 fnum[server][conn][f] = smbcli_open(cli[server][conn]->tree, FILENAME,
358 if (fnum[server][conn][f] == -1) {
359 fprintf(stderr,"Failed to open fnum[%u][%u][%u]\n",
367 static int retest(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
368 int fnum[NSERVERS][NCONNECTIONS][NFILES],
372 printf("testing %u ...\n", n);
373 for (i=0; i<n; i++) {
374 if (i && i % 100 == 0) {
378 if (recorded[i].needed &&
379 !test_one(cli, fnum, &recorded[i])) return i;
385 /* each server has two connections open to it. Each connection has two file
386 descriptors open on the file - 8 file descriptors in total
388 we then do random locking ops in tamdem on the 4 fnums from each
389 server and ensure that the results match
391 static void test_locks(struct loadparm_context *lp_ctx, char *share[NSERVERS])
393 struct smbcli_state *cli[NSERVERS][NCONNECTIONS];
394 int fnum[NSERVERS][NCONNECTIONS][NFILES];
395 int n, i, n1, skip, r1, r2;
400 recorded = malloc_array_p(struct record, numops);
402 for (n=0; n<numops; n++) {
404 if (n < sizeof(preset) / sizeof(preset[0])) {
405 recorded[n] = preset[n];
408 recorded[n].conn = random() % NCONNECTIONS;
409 recorded[n].f = random() % NFILES;
410 recorded[n].start = lock_base + ((uint_t)random() % (lock_range-1));
411 recorded[n].len = min_length +
412 random() % (lock_range-(recorded[n].start-lock_base));
413 recorded[n].start *= RANGE_MULTIPLE;
414 recorded[n].len *= RANGE_MULTIPLE;
415 recorded[n].pid = random()%3;
416 if (recorded[n].pid == 2) {
417 recorded[n].pid = 0xFFFF; /* see if its magic */
422 recorded[n].lock_type = READ_LOCK;
424 recorded[n].lock_type = WRITE_LOCK;
427 recorded[n].lock_op = OP_LOCK;
428 } else if (r2 < UNLOCK_PCT) {
429 recorded[n].lock_op = OP_UNLOCK;
431 recorded[n].lock_op = OP_REOPEN;
433 recorded[n].needed = true;
434 if (!zero_zero && recorded[n].start==0 && recorded[n].len==0) {
442 reconnect(lp_ctx, cli, fnum, share);
443 open_files(cli, fnum);
444 n = retest(cli, fnum, numops);
446 if (n == numops || !analyze) return;
454 close_files(cli, fnum);
455 reconnect(lp_ctx, cli, fnum, share);
456 open_files(cli, fnum);
458 for (i=0;i<n-skip;i+=skip) {
460 printf("excluding %d-%d\n", i, i+skip-1);
461 for (j=i;j<i+skip;j++) {
462 recorded[j].needed = false;
465 close_files(cli, fnum);
466 open_files(cli, fnum);
468 m = retest(cli, fnum, n);
470 for (j=i;j<i+skip;j++) {
471 recorded[j].needed = true;
474 if (i+(skip-1) < m) {
475 memmove(&recorded[i], &recorded[i+skip],
476 (m-(i+skip-1))*sizeof(recorded[0]));
485 printf("skip=%d\n", skip);
492 close_files(cli, fnum);
493 reconnect(lp_ctx, cli, fnum, share);
494 open_files(cli, fnum);
496 n1 = retest(cli, fnum, n);
498 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
500 close_files(cli, fnum);
503 printf("{%d, %d, %u, %u, %.0f, %.0f, %u},\n",
505 recorded[i].lock_type,
508 (double)recorded[i].start,
509 (double)recorded[i].len,
516 static void usage(void)
520 locktest //server1/share1 //server2/share2 [options..]\n\
522 -U user%%pass (may be specified twice)\n\
525 -u hide unlock fails\n\
527 -A analyse for minimal ops\n\
529 -E enable exact error code checking\n\
530 -Z enable the zero/zero lock\n\
531 -R range set lock range\n\
532 -B base set lock base\n\
533 -M min set min lock length\n\
534 -l filename unclist file\n\
538 /****************************************************************************
540 ****************************************************************************/
541 int main(int argc,char *argv[])
543 char *share[NSERVERS];
546 int username_count=0;
547 struct loadparm_context *lp_ctx;
551 setup_logging("locktest", DEBUG_STDOUT);
553 if (argc < 3 || argv[1][0] == '-') {
558 setup_logging(argv[0], DEBUG_STDOUT);
560 for (server=0;server<NSERVERS;server++) {
561 share[server] = argv[1+server];
562 all_string_sub(share[server],"/","\\",0);
568 lp_ctx = loadparm_init(talloc_autofree_context());
569 lp_load(lp_ctx, dyn_CONFIGFILE);
571 servers[0] = cli_credentials_init(talloc_autofree_context());
572 servers[1] = cli_credentials_init(talloc_autofree_context());
573 cli_credentials_guess(servers[0], lp_ctx);
574 cli_credentials_guess(servers[1], lp_ctx);
578 while ((opt = getopt(argc, argv, "U:s:ho:aAW:OR:B:M:EZW:l:")) != EOF) {
581 if (username_count == 2) {
585 cli_credentials_parse_string(servers[username_count],
586 optarg, CRED_SPECIFIED);
590 lock_range = strtol(optarg, NULL, 0);
593 lock_base = strtol(optarg, NULL, 0);
596 min_length = strtol(optarg, NULL, 0);
602 hide_unlock_fails = true;
605 numops = atoi(optarg);
620 exact_error_codes = true;
623 lp_set_cmdline(lp_ctx, "torture:unclist", optarg);
626 lp_set_cmdline(lp_ctx, "workgroup", optarg);
632 printf("Unknown option %c (%d)\n", (char)opt, opt);
637 if (username_count == 0) {
641 if (username_count == 1) {
642 servers[1] = servers[0];
650 DEBUG(0,("seed=%u base=%d range=%d min_length=%d\n",
651 seed, lock_base, lock_range, min_length));
654 test_locks(lp_ctx, share);