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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "system/filesys.h"
23 #include "system/time.h"
25 #include "credentials.h"
27 static int numops = 1000;
30 static BOOL hide_unlock_fails;
31 static BOOL use_oplocks;
32 static uint_t lock_range = 100;
33 static uint_t lock_base = 0;
34 static uint_t min_length = 0;
35 static BOOL exact_error_codes;
36 static BOOL zero_zero;
38 #define FILENAME "\\locktest.dat"
43 #define RANGE_MULTIPLE 1
45 #define NCONNECTIONS 2
47 #define LOCK_TIMEOUT 0
49 #define NASTY_POSIX_LOCK_HACK 0
51 static struct cli_credentials *servers[NSERVERS];
53 enum lock_op {OP_LOCK, OP_UNLOCK, OP_REOPEN};
57 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)
108 struct smbcli_state *c;
109 fstring server, myname;
113 fstrcpy(server,share+2);
114 share = strchr_m(server,'\\');
115 if (!share) return NULL;
119 slprintf(myname,sizeof(myname), "lock-%u-%u", getpid(), snum);
120 cli_credentials_set_workstation(servers[snum], myname, CRED_SPECIFIED);
123 status = smbcli_full_connection(NULL, &c,
126 servers[snum], NULL);
127 if (!NT_STATUS_IS_OK(status)) {
130 } while (!NT_STATUS_IS_OK(status) && retries--);
132 if (!NT_STATUS_IS_OK(status)) {
140 static void reconnect(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], int fnum[NSERVERS][NCONNECTIONS][NFILES],
141 char *share[NSERVERS])
145 for (server=0;server<NSERVERS;server++)
146 for (conn=0;conn<NCONNECTIONS;conn++) {
147 if (cli[server][conn]) {
148 for (f=0;f<NFILES;f++) {
149 if (fnum[server][conn][f] != -1) {
150 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
151 fnum[server][conn][f] = -1;
154 talloc_free(cli[server][conn]);
156 cli[server][conn] = connect_one(share[server], server);
157 if (!cli[server][conn]) {
158 DEBUG(0,("Failed to connect to %s\n", share[server]));
166 static BOOL test_one(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
167 int fnum[NSERVERS][NCONNECTIONS][NFILES],
170 uint_t conn = rec->conn;
172 uint64_t start = rec->start;
173 uint64_t len = rec->len;
174 enum brl_type op = rec->lock_type;
177 NTSTATUS status[NSERVERS];
179 switch (rec->lock_op) {
182 for (server=0;server<NSERVERS;server++) {
183 ret[server] = NT_STATUS_IS_OK(smbcli_lock64(cli[server][conn]->tree,
184 fnum[server][conn][f],
185 start, len, LOCK_TIMEOUT, op));
186 status[server] = smbcli_nt_error(cli[server][conn]->tree);
187 if (!exact_error_codes &&
188 NT_STATUS_EQUAL(status[server],
189 NT_STATUS_FILE_LOCK_CONFLICT)) {
190 status[server] = NT_STATUS_LOCK_NOT_GRANTED;
193 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) {
194 printf("lock conn=%u f=%u range=%.0f(%.0f) op=%s -> %s:%s\n",
196 (double)start, (double)len,
197 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
198 nt_errstr(status[0]), nt_errstr(status[1]));
200 if (!NT_STATUS_EQUAL(status[0],status[1])) return False;
205 for (server=0;server<NSERVERS;server++) {
206 ret[server] = NT_STATUS_IS_OK(smbcli_unlock64(cli[server][conn]->tree,
207 fnum[server][conn][f],
209 status[server] = smbcli_nt_error(cli[server][conn]->tree);
212 (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))) {
213 printf("unlock conn=%u f=%u range=%.0f(%.0f) -> %s:%s\n",
215 (double)start, (double)len,
216 nt_errstr(status[0]), nt_errstr(status[1]));
218 if (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))
223 /* reopen the file */
224 for (server=0;server<NSERVERS;server++) {
225 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
226 fnum[server][conn][f] = -1;
228 for (server=0;server<NSERVERS;server++) {
229 fnum[server][conn][f] = smbcli_open(cli[server][conn]->tree, FILENAME,
232 if (fnum[server][conn][f] == -1) {
233 printf("failed to reopen on share%d\n", server);
238 printf("reopen conn=%u f=%u\n",
247 static void close_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
248 int fnum[NSERVERS][NCONNECTIONS][NFILES])
252 for (server=0;server<NSERVERS;server++)
253 for (conn=0;conn<NCONNECTIONS;conn++)
254 for (f=0;f<NFILES;f++) {
255 if (fnum[server][conn][f] != -1) {
256 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
257 fnum[server][conn][f] = -1;
260 for (server=0;server<NSERVERS;server++) {
261 smbcli_unlink(cli[server][0]->tree, FILENAME);
265 static void open_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
266 int fnum[NSERVERS][NCONNECTIONS][NFILES])
270 for (server=0;server<NSERVERS;server++)
271 for (conn=0;conn<NCONNECTIONS;conn++)
272 for (f=0;f<NFILES;f++) {
273 fnum[server][conn][f] = smbcli_open(cli[server][conn]->tree, FILENAME,
276 if (fnum[server][conn][f] == -1) {
277 fprintf(stderr,"Failed to open fnum[%u][%u][%u]\n",
285 static int retest(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
286 int fnum[NSERVERS][NCONNECTIONS][NFILES],
290 printf("testing %u ...\n", n);
291 for (i=0; i<n; i++) {
292 if (i && i % 100 == 0) {
296 if (recorded[i].needed &&
297 !test_one(cli, fnum, &recorded[i])) return i;
303 /* each server has two connections open to it. Each connection has two file
304 descriptors open on the file - 8 file descriptors in total
306 we then do random locking ops in tamdem on the 4 fnums from each
307 server and ensure that the results match
309 static void test_locks(char *share[NSERVERS])
311 struct smbcli_state *cli[NSERVERS][NCONNECTIONS];
312 int fnum[NSERVERS][NCONNECTIONS][NFILES];
313 int n, i, n1, skip, r1, r2;
318 recorded = malloc_array_p(struct record, numops);
320 for (n=0; n<numops; n++) {
322 if (n < sizeof(preset) / sizeof(preset[0])) {
323 recorded[n] = preset[n];
326 recorded[n].conn = random() % NCONNECTIONS;
327 recorded[n].f = random() % NFILES;
328 recorded[n].start = lock_base + ((uint_t)random() % (lock_range-1));
329 recorded[n].len = min_length +
330 random() % (lock_range-(recorded[n].start-lock_base));
331 recorded[n].start *= RANGE_MULTIPLE;
332 recorded[n].len *= RANGE_MULTIPLE;
336 recorded[n].lock_type = READ_LOCK;
338 recorded[n].lock_type = WRITE_LOCK;
341 recorded[n].lock_op = OP_LOCK;
342 } else if (r2 < UNLOCK_PCT) {
343 recorded[n].lock_op = OP_UNLOCK;
345 recorded[n].lock_op = OP_REOPEN;
347 recorded[n].needed = True;
348 if (!zero_zero && recorded[n].start==0 && recorded[n].len==0) {
356 reconnect(cli, fnum, share);
357 open_files(cli, fnum);
358 n = retest(cli, fnum, numops);
360 if (n == numops || !analyze) return;
368 close_files(cli, fnum);
369 reconnect(cli, fnum, share);
370 open_files(cli, fnum);
372 for (i=0;i<n-skip;i+=skip) {
374 printf("excluding %d-%d\n", i, i+skip-1);
375 for (j=i;j<i+skip;j++) {
376 recorded[j].needed = False;
379 close_files(cli, fnum);
380 open_files(cli, fnum);
382 m = retest(cli, fnum, n);
384 for (j=i;j<i+skip;j++) {
385 recorded[j].needed = True;
388 if (i+(skip-1) < m) {
389 memmove(&recorded[i], &recorded[i+skip],
390 (m-(i+skip-1))*sizeof(recorded[0]));
399 printf("skip=%d\n", skip);
406 close_files(cli, fnum);
407 reconnect(cli, fnum, share);
408 open_files(cli, fnum);
410 n1 = retest(cli, fnum, n);
412 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
414 close_files(cli, fnum);
417 printf("{%d, %d, %u, %u, %.0f, %.0f, %u},\n",
419 recorded[i].lock_type,
422 (double)recorded[i].start,
423 (double)recorded[i].len,
430 static void usage(void)
434 locktest //server1/share1 //server2/share2 [options..]\n\
436 -U user%%pass (may be specified twice)\n\
439 -u hide unlock fails\n\
441 -A analyse for minimal ops\n\
443 -E enable exact error code checking\n\
444 -Z enable the zero/zero lock\n\
445 -R range set lock range\n\
446 -B base set lock base\n\
447 -M min set min lock length\n\
451 /****************************************************************************
453 ****************************************************************************/
454 int main(int argc,char *argv[])
456 char *share[NSERVERS];
459 int username_count=0;
463 setup_logging("locktest", DEBUG_STDOUT);
465 if (argc < 3 || argv[1][0] == '-') {
470 setup_logging(argv[0], DEBUG_STDOUT);
472 for (server=0;server<NSERVERS;server++) {
473 share[server] = argv[1+server];
474 all_string_sub(share[server],"/","\\",0);
483 servers[0] = cli_credentials_init(talloc_autofree_context());
484 servers[1] = cli_credentials_init(talloc_autofree_context());
485 cli_credentials_guess(servers[0]);
486 cli_credentials_guess(servers[1]);
490 while ((opt = getopt(argc, argv, "U:s:ho:aAW:OR:B:M:EZW:")) != EOF) {
493 if (username_count == 2) {
497 cli_credentials_parse_string(servers[username_count],
498 optarg, CRED_SPECIFIED);
502 lock_range = strtol(optarg, NULL, 0);
505 lock_base = strtol(optarg, NULL, 0);
508 min_length = strtol(optarg, NULL, 0);
514 hide_unlock_fails = True;
517 numops = atoi(optarg);
532 exact_error_codes = True;
535 lp_set_cmdline("workgroup", optarg);
541 printf("Unknown option %c (%d)\n", (char)opt, opt);
546 if (username_count == 0) {
550 if (username_count == 1) {
551 servers[1] = servers[0];
554 locktest_init_subsystems;
559 DEBUG(0,("seed=%u base=%d range=%d min_length=%d\n",
560 seed, lock_base, lock_range, min_length));