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(char *share, int snum, int conn)
110 struct smbcli_state *c;
111 fstring server, myname;
115 printf("connect_one(%s, %d, %d)\n", share, snum, conn);
117 fstrcpy(server,share+2);
118 share = strchr_m(server,'\\');
119 if (!share) return NULL;
124 char **unc_list = NULL;
127 p = lp_parm_string(global_loadparm, NULL, "torture", "unclist");
130 unc_list = file_lines_load(p, &num_unc_names, NULL);
131 if (!unc_list || num_unc_names <= 0) {
132 printf("Failed to load unc names list from '%s'\n", p);
136 if (!smbcli_parse_unc(unc_list[conn % num_unc_names],
138 printf("Failed to parse UNC name %s\n",
139 unc_list[conn % num_unc_names]);
148 slprintf(myname,sizeof(myname), "lock-%u-%u", getpid(), snum);
149 cli_credentials_set_workstation(servers[snum], myname, CRED_SPECIFIED);
152 printf("\\\\%s\\%s\n", server, share);
153 status = smbcli_full_connection(NULL, &c,
156 servers[snum], NULL);
157 if (!NT_STATUS_IS_OK(status)) {
160 } while (!NT_STATUS_IS_OK(status) && retries--);
162 if (!NT_STATUS_IS_OK(status)) {
170 static void reconnect(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], int fnum[NSERVERS][NCONNECTIONS][NFILES],
171 char *share[NSERVERS])
175 for (server=0;server<NSERVERS;server++)
176 for (conn=0;conn<NCONNECTIONS;conn++) {
177 if (cli[server][conn]) {
178 for (f=0;f<NFILES;f++) {
179 if (fnum[server][conn][f] != -1) {
180 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
181 fnum[server][conn][f] = -1;
184 talloc_free(cli[server][conn]);
186 cli[server][conn] = connect_one(share[server], server, conn);
187 if (!cli[server][conn]) {
188 DEBUG(0,("Failed to connect to %s\n", share[server]));
196 static bool test_one(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
197 int fnum[NSERVERS][NCONNECTIONS][NFILES],
200 uint_t conn = rec->conn;
202 uint64_t start = rec->start;
203 uint64_t len = rec->len;
204 enum brl_type op = rec->lock_type;
207 NTSTATUS status[NSERVERS];
209 switch (rec->lock_op) {
212 for (server=0;server<NSERVERS;server++) {
214 struct smbcli_tree *tree=cli[server][conn]->tree;
215 int fn=fnum[server][conn][f];
217 if (!(tree->session->transport->negotiate.capabilities & CAP_LARGE_FILES)) {
218 res=smbcli_lock(tree, fn, start, len, LOCK_TIMEOUT, rec->lock_op);
220 union smb_lock parms;
222 struct smb_lock_entry lock[1];
224 parms.lockx.level = RAW_LOCK_LOCKX;
225 parms.lockx.in.file.fnum = fn;
227 ltype = (rec->lock_op == READ_LOCK? 1 : 0);
228 ltype |= LOCKING_ANDX_LARGE_FILES;
229 parms.lockx.in.mode = ltype;
230 parms.lockx.in.timeout = LOCK_TIMEOUT;
231 parms.lockx.in.ulock_cnt = 0;
232 parms.lockx.in.lock_cnt = 1;
233 lock[0].pid = rec->pid;
234 lock[0].offset = start;
236 parms.lockx.in.locks = &lock[0];
238 res = smb_raw_lock(tree, &parms);
241 ret[server] = NT_STATUS_IS_OK(res);
242 status[server] = smbcli_nt_error(cli[server][conn]->tree);
243 if (!exact_error_codes &&
244 NT_STATUS_EQUAL(status[server],
245 NT_STATUS_FILE_LOCK_CONFLICT)) {
246 status[server] = NT_STATUS_LOCK_NOT_GRANTED;
249 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) {
250 printf("lock conn=%u f=%u range=%.0f(%.0f) op=%s -> %s:%s\n",
252 (double)start, (double)len,
253 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
254 nt_errstr(status[0]), nt_errstr(status[1]));
256 if (!NT_STATUS_EQUAL(status[0],status[1])) return False;
261 for (server=0;server<NSERVERS;server++) {
263 struct smbcli_tree *tree=cli[server][conn]->tree;
264 int fn=fnum[server][conn][f];
267 if (!(tree->session->transport->negotiate.capabilities & CAP_LARGE_FILES)) {
268 res=smbcli_unlock(tree, fn, start, len);
270 union smb_lock parms;
271 struct smb_lock_entry lock[1];
273 parms.lockx.level = RAW_LOCK_LOCKX;
274 parms.lockx.in.file.fnum = fn;
275 parms.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
276 parms.lockx.in.timeout = 0;
277 parms.lockx.in.ulock_cnt = 1;
278 parms.lockx.in.lock_cnt = 0;
279 lock[0].pid = rec->pid;
281 lock[0].offset = start;
282 parms.lockx.in.locks = &lock[0];
284 res = smb_raw_lock(tree, &parms);
287 ret[server] = NT_STATUS_IS_OK(res);
288 status[server] = smbcli_nt_error(cli[server][conn]->tree);
291 (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))) {
292 printf("unlock conn=%u f=%u range=%.0f(%.0f) -> %s:%s\n",
294 (double)start, (double)len,
295 nt_errstr(status[0]), nt_errstr(status[1]));
297 if (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))
302 /* reopen the file */
303 for (server=0;server<NSERVERS;server++) {
304 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
305 fnum[server][conn][f] = -1;
307 for (server=0;server<NSERVERS;server++) {
308 fnum[server][conn][f] = smbcli_open(cli[server][conn]->tree, FILENAME,
311 if (fnum[server][conn][f] == -1) {
312 printf("failed to reopen on share%d\n", server);
317 printf("reopen conn=%u f=%u\n",
326 static void close_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
327 int fnum[NSERVERS][NCONNECTIONS][NFILES])
331 for (server=0;server<NSERVERS;server++)
332 for (conn=0;conn<NCONNECTIONS;conn++)
333 for (f=0;f<NFILES;f++) {
334 if (fnum[server][conn][f] != -1) {
335 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
336 fnum[server][conn][f] = -1;
339 for (server=0;server<NSERVERS;server++) {
340 smbcli_unlink(cli[server][0]->tree, FILENAME);
344 static void open_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
345 int fnum[NSERVERS][NCONNECTIONS][NFILES])
349 for (server=0;server<NSERVERS;server++)
350 for (conn=0;conn<NCONNECTIONS;conn++)
351 for (f=0;f<NFILES;f++) {
352 fnum[server][conn][f] = smbcli_open(cli[server][conn]->tree, FILENAME,
355 if (fnum[server][conn][f] == -1) {
356 fprintf(stderr,"Failed to open fnum[%u][%u][%u]\n",
364 static int retest(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
365 int fnum[NSERVERS][NCONNECTIONS][NFILES],
369 printf("testing %u ...\n", n);
370 for (i=0; i<n; i++) {
371 if (i && i % 100 == 0) {
375 if (recorded[i].needed &&
376 !test_one(cli, fnum, &recorded[i])) return i;
382 /* each server has two connections open to it. Each connection has two file
383 descriptors open on the file - 8 file descriptors in total
385 we then do random locking ops in tamdem on the 4 fnums from each
386 server and ensure that the results match
388 static void test_locks(char *share[NSERVERS])
390 struct smbcli_state *cli[NSERVERS][NCONNECTIONS];
391 int fnum[NSERVERS][NCONNECTIONS][NFILES];
392 int n, i, n1, skip, r1, r2;
397 recorded = malloc_array_p(struct record, numops);
399 for (n=0; n<numops; n++) {
401 if (n < sizeof(preset) / sizeof(preset[0])) {
402 recorded[n] = preset[n];
405 recorded[n].conn = random() % NCONNECTIONS;
406 recorded[n].f = random() % NFILES;
407 recorded[n].start = lock_base + ((uint_t)random() % (lock_range-1));
408 recorded[n].len = min_length +
409 random() % (lock_range-(recorded[n].start-lock_base));
410 recorded[n].start *= RANGE_MULTIPLE;
411 recorded[n].len *= RANGE_MULTIPLE;
412 recorded[n].pid = random()%3;
413 if (recorded[n].pid == 2) {
414 recorded[n].pid = 0xFFFF; /* see if its magic */
419 recorded[n].lock_type = READ_LOCK;
421 recorded[n].lock_type = WRITE_LOCK;
424 recorded[n].lock_op = OP_LOCK;
425 } else if (r2 < UNLOCK_PCT) {
426 recorded[n].lock_op = OP_UNLOCK;
428 recorded[n].lock_op = OP_REOPEN;
430 recorded[n].needed = True;
431 if (!zero_zero && recorded[n].start==0 && recorded[n].len==0) {
439 reconnect(cli, fnum, share);
440 open_files(cli, fnum);
441 n = retest(cli, fnum, numops);
443 if (n == numops || !analyze) return;
451 close_files(cli, fnum);
452 reconnect(cli, fnum, share);
453 open_files(cli, fnum);
455 for (i=0;i<n-skip;i+=skip) {
457 printf("excluding %d-%d\n", i, i+skip-1);
458 for (j=i;j<i+skip;j++) {
459 recorded[j].needed = False;
462 close_files(cli, fnum);
463 open_files(cli, fnum);
465 m = retest(cli, fnum, n);
467 for (j=i;j<i+skip;j++) {
468 recorded[j].needed = True;
471 if (i+(skip-1) < m) {
472 memmove(&recorded[i], &recorded[i+skip],
473 (m-(i+skip-1))*sizeof(recorded[0]));
482 printf("skip=%d\n", skip);
489 close_files(cli, fnum);
490 reconnect(cli, fnum, share);
491 open_files(cli, fnum);
493 n1 = retest(cli, fnum, n);
495 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
497 close_files(cli, fnum);
500 printf("{%d, %d, %u, %u, %.0f, %.0f, %u},\n",
502 recorded[i].lock_type,
505 (double)recorded[i].start,
506 (double)recorded[i].len,
513 static void usage(void)
517 locktest //server1/share1 //server2/share2 [options..]\n\
519 -U user%%pass (may be specified twice)\n\
522 -u hide unlock fails\n\
524 -A analyse for minimal ops\n\
526 -E enable exact error code checking\n\
527 -Z enable the zero/zero lock\n\
528 -R range set lock range\n\
529 -B base set lock base\n\
530 -M min set min lock length\n\
531 -l filename unclist file\n\
535 /****************************************************************************
537 ****************************************************************************/
538 int main(int argc,char *argv[])
540 char *share[NSERVERS];
543 int username_count=0;
547 setup_logging("locktest", DEBUG_STDOUT);
549 if (argc < 3 || argv[1][0] == '-') {
554 setup_logging(argv[0], DEBUG_STDOUT);
556 for (server=0;server<NSERVERS;server++) {
557 share[server] = argv[1+server];
558 all_string_sub(share[server],"/","\\",0);
564 lp_load(dyn_CONFIGFILE);
566 servers[0] = cli_credentials_init(talloc_autofree_context());
567 servers[1] = cli_credentials_init(talloc_autofree_context());
568 cli_credentials_guess(servers[0]);
569 cli_credentials_guess(servers[1]);
573 while ((opt = getopt(argc, argv, "U:s:ho:aAW:OR:B:M:EZW:l:")) != EOF) {
576 if (username_count == 2) {
580 cli_credentials_parse_string(servers[username_count],
581 optarg, CRED_SPECIFIED);
585 lock_range = strtol(optarg, NULL, 0);
588 lock_base = strtol(optarg, NULL, 0);
591 min_length = strtol(optarg, NULL, 0);
597 hide_unlock_fails = True;
600 numops = atoi(optarg);
615 exact_error_codes = True;
618 lp_set_cmdline(global_loadparm, "torture:unclist", optarg);
621 lp_set_cmdline(global_loadparm, "workgroup", optarg);
627 printf("Unknown option %c (%d)\n", (char)opt, opt);
632 if (username_count == 0) {
636 if (username_count == 1) {
637 servers[1] = servers[0];
645 DEBUG(0,("seed=%u base=%d range=%d min_length=%d\n",
646 seed, lock_base, lock_range, min_length));