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"
28 static int numops = 1000;
31 static BOOL hide_unlock_fails;
32 static BOOL use_oplocks;
33 static uint_t lock_range = 100;
34 static uint_t lock_base = 0;
35 static uint_t min_length = 0;
36 static BOOL exact_error_codes;
37 static BOOL zero_zero;
39 #define FILENAME "\\locktest.dat"
44 #define RANGE_MULTIPLE 1
46 #define NCONNECTIONS 2
48 #define LOCK_TIMEOUT 0
50 static struct cli_credentials *servers[NSERVERS];
52 enum lock_op {OP_LOCK, OP_UNLOCK, OP_REOPEN};
56 enum brl_type lock_type;
66 static struct record preset[] = {
67 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
68 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
69 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 0, 1},
70 {OP_UNLOCK, 0 , 0, 0, 2, 0, 1},
71 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
73 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
74 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
75 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
76 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
78 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
79 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 1, 1},
80 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
81 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
83 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
84 {OP_LOCK, WRITE_LOCK, 0, 0, 1, 1, 1},
85 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
86 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
88 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
89 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
90 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
91 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
93 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
94 {OP_LOCK, READ_LOCK, 0, 0, 3, 1, 1},
95 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
96 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
101 static struct record *recorded;
103 /*****************************************************
104 return a connection to a server
105 *******************************************************/
106 static struct smbcli_state *connect_one(char *share, int snum, int conn)
108 struct smbcli_state *c;
109 fstring server, myname;
113 printf("connect_one(%s, %d, %d)\n", share, snum, conn);
115 fstrcpy(server,share+2);
116 share = strchr_m(server,'\\');
117 if (!share) return NULL;
122 char **unc_list = NULL;
125 p = lp_parm_string(-1, "torture", "unclist");
128 unc_list = file_lines_load(p, &num_unc_names, NULL);
129 if (!unc_list || num_unc_names <= 0) {
130 printf("Failed to load unc names list from '%s'\n", p);
134 if (!smbcli_parse_unc(unc_list[conn % num_unc_names],
136 printf("Failed to parse UNC name %s\n",
137 unc_list[conn % num_unc_names]);
146 slprintf(myname,sizeof(myname), "lock-%u-%u", getpid(), snum);
147 cli_credentials_set_workstation(servers[snum], myname, CRED_SPECIFIED);
150 printf("\\\\%s\\%s\n", server, share);
151 status = smbcli_full_connection(NULL, &c,
154 servers[snum], NULL);
155 if (!NT_STATUS_IS_OK(status)) {
158 } while (!NT_STATUS_IS_OK(status) && retries--);
160 if (!NT_STATUS_IS_OK(status)) {
168 static void reconnect(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], int fnum[NSERVERS][NCONNECTIONS][NFILES],
169 char *share[NSERVERS])
173 for (server=0;server<NSERVERS;server++)
174 for (conn=0;conn<NCONNECTIONS;conn++) {
175 if (cli[server][conn]) {
176 for (f=0;f<NFILES;f++) {
177 if (fnum[server][conn][f] != -1) {
178 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
179 fnum[server][conn][f] = -1;
182 talloc_free(cli[server][conn]);
184 cli[server][conn] = connect_one(share[server], server, conn);
185 if (!cli[server][conn]) {
186 DEBUG(0,("Failed to connect to %s\n", share[server]));
194 static BOOL test_one(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
195 int fnum[NSERVERS][NCONNECTIONS][NFILES],
198 uint_t conn = rec->conn;
200 uint64_t start = rec->start;
201 uint64_t len = rec->len;
202 enum brl_type op = rec->lock_type;
205 NTSTATUS status[NSERVERS];
207 switch (rec->lock_op) {
210 for (server=0;server<NSERVERS;server++) {
212 struct smbcli_tree *tree=cli[server][conn]->tree;
213 int fn=fnum[server][conn][f];
215 if (!(tree->session->transport->negotiate.capabilities & CAP_LARGE_FILES)) {
216 res=smbcli_lock(tree, fn, start, len, LOCK_TIMEOUT, rec->lock_op);
218 union smb_lock parms;
220 struct smb_lock_entry lock[1];
222 parms.lockx.level = RAW_LOCK_LOCKX;
223 parms.lockx.in.file.fnum = fn;
225 ltype = (rec->lock_op == READ_LOCK? 1 : 0);
226 ltype |= LOCKING_ANDX_LARGE_FILES;
227 parms.lockx.in.mode = ltype;
228 parms.lockx.in.timeout = LOCK_TIMEOUT;
229 parms.lockx.in.ulock_cnt = 0;
230 parms.lockx.in.lock_cnt = 1;
231 lock[0].pid = rec->pid;
232 lock[0].offset = start;
234 parms.lockx.in.locks = &lock[0];
236 res = smb_raw_lock(tree, &parms);
239 ret[server] = NT_STATUS_IS_OK(res);
240 status[server] = smbcli_nt_error(cli[server][conn]->tree);
241 if (!exact_error_codes &&
242 NT_STATUS_EQUAL(status[server],
243 NT_STATUS_FILE_LOCK_CONFLICT)) {
244 status[server] = NT_STATUS_LOCK_NOT_GRANTED;
247 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) {
248 printf("lock conn=%u f=%u range=%.0f(%.0f) op=%s -> %s:%s\n",
250 (double)start, (double)len,
251 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
252 nt_errstr(status[0]), nt_errstr(status[1]));
254 if (!NT_STATUS_EQUAL(status[0],status[1])) return False;
259 for (server=0;server<NSERVERS;server++) {
261 struct smbcli_tree *tree=cli[server][conn]->tree;
262 int fn=fnum[server][conn][f];
265 if (!(tree->session->transport->negotiate.capabilities & CAP_LARGE_FILES)) {
266 res=smbcli_unlock(tree, fn, start, len);
268 union smb_lock parms;
269 struct smb_lock_entry lock[1];
271 parms.lockx.level = RAW_LOCK_LOCKX;
272 parms.lockx.in.file.fnum = fn;
273 parms.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
274 parms.lockx.in.timeout = 0;
275 parms.lockx.in.ulock_cnt = 1;
276 parms.lockx.in.lock_cnt = 0;
277 lock[0].pid = rec->pid;
279 lock[0].offset = start;
280 parms.lockx.in.locks = &lock[0];
282 res = smb_raw_lock(tree, &parms);
285 ret[server] = NT_STATUS_IS_OK(res);
286 status[server] = smbcli_nt_error(cli[server][conn]->tree);
289 (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))) {
290 printf("unlock conn=%u f=%u range=%.0f(%.0f) -> %s:%s\n",
292 (double)start, (double)len,
293 nt_errstr(status[0]), nt_errstr(status[1]));
295 if (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))
300 /* reopen the file */
301 for (server=0;server<NSERVERS;server++) {
302 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
303 fnum[server][conn][f] = -1;
305 for (server=0;server<NSERVERS;server++) {
306 fnum[server][conn][f] = smbcli_open(cli[server][conn]->tree, FILENAME,
309 if (fnum[server][conn][f] == -1) {
310 printf("failed to reopen on share%d\n", server);
315 printf("reopen conn=%u f=%u\n",
324 static void close_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
325 int fnum[NSERVERS][NCONNECTIONS][NFILES])
329 for (server=0;server<NSERVERS;server++)
330 for (conn=0;conn<NCONNECTIONS;conn++)
331 for (f=0;f<NFILES;f++) {
332 if (fnum[server][conn][f] != -1) {
333 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
334 fnum[server][conn][f] = -1;
337 for (server=0;server<NSERVERS;server++) {
338 smbcli_unlink(cli[server][0]->tree, FILENAME);
342 static void open_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
343 int fnum[NSERVERS][NCONNECTIONS][NFILES])
347 for (server=0;server<NSERVERS;server++)
348 for (conn=0;conn<NCONNECTIONS;conn++)
349 for (f=0;f<NFILES;f++) {
350 fnum[server][conn][f] = smbcli_open(cli[server][conn]->tree, FILENAME,
353 if (fnum[server][conn][f] == -1) {
354 fprintf(stderr,"Failed to open fnum[%u][%u][%u]\n",
362 static int retest(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
363 int fnum[NSERVERS][NCONNECTIONS][NFILES],
367 printf("testing %u ...\n", n);
368 for (i=0; i<n; i++) {
369 if (i && i % 100 == 0) {
373 if (recorded[i].needed &&
374 !test_one(cli, fnum, &recorded[i])) return i;
380 /* each server has two connections open to it. Each connection has two file
381 descriptors open on the file - 8 file descriptors in total
383 we then do random locking ops in tamdem on the 4 fnums from each
384 server and ensure that the results match
386 static void test_locks(char *share[NSERVERS])
388 struct smbcli_state *cli[NSERVERS][NCONNECTIONS];
389 int fnum[NSERVERS][NCONNECTIONS][NFILES];
390 int n, i, n1, skip, r1, r2;
395 recorded = malloc_array_p(struct record, numops);
397 for (n=0; n<numops; n++) {
399 if (n < sizeof(preset) / sizeof(preset[0])) {
400 recorded[n] = preset[n];
403 recorded[n].conn = random() % NCONNECTIONS;
404 recorded[n].f = random() % NFILES;
405 recorded[n].start = lock_base + ((uint_t)random() % (lock_range-1));
406 recorded[n].len = min_length +
407 random() % (lock_range-(recorded[n].start-lock_base));
408 recorded[n].start *= RANGE_MULTIPLE;
409 recorded[n].len *= RANGE_MULTIPLE;
410 recorded[n].pid = random()%3;
411 if (recorded[n].pid == 2) {
412 recorded[n].pid = 0xFFFF; /* see if its magic */
417 recorded[n].lock_type = READ_LOCK;
419 recorded[n].lock_type = WRITE_LOCK;
422 recorded[n].lock_op = OP_LOCK;
423 } else if (r2 < UNLOCK_PCT) {
424 recorded[n].lock_op = OP_UNLOCK;
426 recorded[n].lock_op = OP_REOPEN;
428 recorded[n].needed = True;
429 if (!zero_zero && recorded[n].start==0 && recorded[n].len==0) {
437 reconnect(cli, fnum, share);
438 open_files(cli, fnum);
439 n = retest(cli, fnum, numops);
441 if (n == numops || !analyze) return;
449 close_files(cli, fnum);
450 reconnect(cli, fnum, share);
451 open_files(cli, fnum);
453 for (i=0;i<n-skip;i+=skip) {
455 printf("excluding %d-%d\n", i, i+skip-1);
456 for (j=i;j<i+skip;j++) {
457 recorded[j].needed = False;
460 close_files(cli, fnum);
461 open_files(cli, fnum);
463 m = retest(cli, fnum, n);
465 for (j=i;j<i+skip;j++) {
466 recorded[j].needed = True;
469 if (i+(skip-1) < m) {
470 memmove(&recorded[i], &recorded[i+skip],
471 (m-(i+skip-1))*sizeof(recorded[0]));
480 printf("skip=%d\n", skip);
487 close_files(cli, fnum);
488 reconnect(cli, fnum, share);
489 open_files(cli, fnum);
491 n1 = retest(cli, fnum, n);
493 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
495 close_files(cli, fnum);
498 printf("{%d, %d, %u, %u, %.0f, %.0f, %u},\n",
500 recorded[i].lock_type,
503 (double)recorded[i].start,
504 (double)recorded[i].len,
511 static void usage(void)
515 locktest //server1/share1 //server2/share2 [options..]\n\
517 -U user%%pass (may be specified twice)\n\
520 -u hide unlock fails\n\
522 -A analyse for minimal ops\n\
524 -E enable exact error code checking\n\
525 -Z enable the zero/zero lock\n\
526 -R range set lock range\n\
527 -B base set lock base\n\
528 -M min set min lock length\n\
529 -l filename unclist file\n\
533 /****************************************************************************
535 ****************************************************************************/
536 int main(int argc,char *argv[])
538 char *share[NSERVERS];
541 int username_count=0;
545 setup_logging("locktest", DEBUG_STDOUT);
547 if (argc < 3 || argv[1][0] == '-') {
552 setup_logging(argv[0], DEBUG_STDOUT);
554 for (server=0;server<NSERVERS;server++) {
555 share[server] = argv[1+server];
556 all_string_sub(share[server],"/","\\",0);
564 servers[0] = cli_credentials_init(talloc_autofree_context());
565 servers[1] = cli_credentials_init(talloc_autofree_context());
566 cli_credentials_guess(servers[0]);
567 cli_credentials_guess(servers[1]);
571 while ((opt = getopt(argc, argv, "U:s:ho:aAW:OR:B:M:EZW:l:")) != EOF) {
574 if (username_count == 2) {
578 cli_credentials_parse_string(servers[username_count],
579 optarg, CRED_SPECIFIED);
583 lock_range = strtol(optarg, NULL, 0);
586 lock_base = strtol(optarg, NULL, 0);
589 min_length = strtol(optarg, NULL, 0);
595 hide_unlock_fails = True;
598 numops = atoi(optarg);
613 exact_error_codes = True;
616 lp_set_cmdline("torture:unclist", optarg);
619 lp_set_cmdline("workgroup", optarg);
625 printf("Unknown option %c (%d)\n", (char)opt, opt);
630 if (username_count == 0) {
634 if (username_count == 1) {
635 servers[1] = servers[0];
643 DEBUG(0,("seed=%u base=%d range=%d min_length=%d\n",
644 seed, lock_base, lock_range, min_length));