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.
23 static fstring password[2];
24 static fstring username[2];
27 static BOOL use_kerberos;
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 #define NASTY_POSIX_LOCK_HACK 0
52 enum lock_op {OP_LOCK, OP_UNLOCK, OP_REOPEN};
56 enum brl_type lock_type;
65 static struct record preset[] = {
66 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
67 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
68 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 0, 1},
69 {OP_UNLOCK, 0 , 0, 0, 2, 0, 1},
70 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
72 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
73 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
74 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
75 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
77 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
78 {OP_LOCK, WRITE_LOCK, 0, 0, 3, 1, 1},
79 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
80 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
82 {OP_LOCK, READ_LOCK, 0, 0, 2, 0, 1},
83 {OP_LOCK, WRITE_LOCK, 0, 0, 1, 1, 1},
84 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
85 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
87 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
88 {OP_LOCK, READ_LOCK, 0, 0, 1, 1, 1},
89 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
90 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
92 {OP_LOCK, WRITE_LOCK, 0, 0, 2, 0, 1},
93 {OP_LOCK, READ_LOCK, 0, 0, 3, 1, 1},
94 {OP_LOCK, WRITE_LOCK, 0, 0, 0, 0, 1},
95 {OP_REOPEN, 0, 0, 0, 0, 0, 1},
100 static struct record *recorded;
102 /*****************************************************
103 return a connection to a server
104 *******************************************************/
105 static struct smbcli_state *connect_one(char *share, int snum)
107 struct smbcli_state *c;
108 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);
122 flags |= SMBCLI_FULL_CONNECTION_USE_KERBEROS;
125 status = smbcli_full_connection(&c, myname,
128 username[snum], lp_workgroup(),
129 password[snum], flags, NULL);
130 if (!NT_STATUS_IS_OK(status)) {
133 } while (!NT_STATUS_IS_OK(status) && retries--);
135 if (!NT_STATUS_IS_OK(status)) {
143 static void reconnect(struct smbcli_state *cli[NSERVERS][NCONNECTIONS], int fnum[NSERVERS][NCONNECTIONS][NFILES],
144 char *share[NSERVERS])
148 for (server=0;server<NSERVERS;server++)
149 for (conn=0;conn<NCONNECTIONS;conn++) {
150 if (cli[server][conn]) {
151 for (f=0;f<NFILES;f++) {
152 if (fnum[server][conn][f] != -1) {
153 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
154 fnum[server][conn][f] = -1;
157 smbcli_shutdown(cli[server][conn]);
159 cli[server][conn] = connect_one(share[server], server);
160 if (!cli[server][conn]) {
161 DEBUG(0,("Failed to connect to %s\n", share[server]));
169 static BOOL test_one(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
170 int fnum[NSERVERS][NCONNECTIONS][NFILES],
173 uint_t conn = rec->conn;
175 uint64_t start = rec->start;
176 uint64_t len = rec->len;
177 enum brl_type op = rec->lock_type;
180 NTSTATUS status[NSERVERS];
182 switch (rec->lock_op) {
185 for (server=0;server<NSERVERS;server++) {
186 ret[server] = NT_STATUS_IS_OK(smbcli_lock64(cli[server][conn]->tree,
187 fnum[server][conn][f],
188 start, len, LOCK_TIMEOUT, op));
189 status[server] = smbcli_nt_error(cli[server][conn]->tree);
190 if (!exact_error_codes &&
191 NT_STATUS_EQUAL(status[server],
192 NT_STATUS_FILE_LOCK_CONFLICT)) {
193 status[server] = NT_STATUS_LOCK_NOT_GRANTED;
196 if (showall || !NT_STATUS_EQUAL(status[0],status[1])) {
197 printf("lock conn=%u f=%u range=%.0f(%.0f) op=%s -> %s:%s\n",
199 (double)start, (double)len,
200 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
201 nt_errstr(status[0]), nt_errstr(status[1]));
203 if (!NT_STATUS_EQUAL(status[0],status[1])) return False;
208 for (server=0;server<NSERVERS;server++) {
209 ret[server] = NT_STATUS_IS_OK(smbcli_unlock64(cli[server][conn]->tree,
210 fnum[server][conn][f],
212 status[server] = smbcli_nt_error(cli[server][conn]->tree);
215 (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))) {
216 printf("unlock conn=%u f=%u range=%.0f(%.0f) -> %s:%s\n",
218 (double)start, (double)len,
219 nt_errstr(status[0]), nt_errstr(status[1]));
221 if (!hide_unlock_fails && !NT_STATUS_EQUAL(status[0],status[1]))
226 /* reopen the file */
227 for (server=0;server<NSERVERS;server++) {
228 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
229 fnum[server][conn][f] = -1;
231 for (server=0;server<NSERVERS;server++) {
232 fnum[server][conn][f] = smbcli_open(cli[server][conn]->tree, FILENAME,
235 if (fnum[server][conn][f] == -1) {
236 printf("failed to reopen on share%d\n", server);
241 printf("reopen conn=%u f=%u\n",
250 static void close_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
251 int fnum[NSERVERS][NCONNECTIONS][NFILES])
255 for (server=0;server<NSERVERS;server++)
256 for (conn=0;conn<NCONNECTIONS;conn++)
257 for (f=0;f<NFILES;f++) {
258 if (fnum[server][conn][f] != -1) {
259 smbcli_close(cli[server][conn]->tree, fnum[server][conn][f]);
260 fnum[server][conn][f] = -1;
263 for (server=0;server<NSERVERS;server++) {
264 smbcli_unlink(cli[server][0]->tree, FILENAME);
268 static void open_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
269 int fnum[NSERVERS][NCONNECTIONS][NFILES])
273 for (server=0;server<NSERVERS;server++)
274 for (conn=0;conn<NCONNECTIONS;conn++)
275 for (f=0;f<NFILES;f++) {
276 fnum[server][conn][f] = smbcli_open(cli[server][conn]->tree, FILENAME,
279 if (fnum[server][conn][f] == -1) {
280 fprintf(stderr,"Failed to open fnum[%u][%u][%u]\n",
288 static int retest(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
289 int fnum[NSERVERS][NCONNECTIONS][NFILES],
293 printf("testing %u ...\n", n);
294 for (i=0; i<n; i++) {
295 if (i && i % 100 == 0) {
299 if (recorded[i].needed &&
300 !test_one(cli, fnum, &recorded[i])) return i;
306 /* each server has two connections open to it. Each connection has two file
307 descriptors open on the file - 8 file descriptors in total
309 we then do random locking ops in tamdem on the 4 fnums from each
310 server and ensure that the results match
312 static void test_locks(char *share[NSERVERS])
314 struct smbcli_state *cli[NSERVERS][NCONNECTIONS];
315 int fnum[NSERVERS][NCONNECTIONS][NFILES];
316 int n, i, n1, skip, r1, r2;
321 recorded = (struct record *)malloc(sizeof(*recorded) * numops);
323 for (n=0; n<numops; n++) {
325 if (n < sizeof(preset) / sizeof(preset[0])) {
326 recorded[n] = preset[n];
329 recorded[n].conn = random() % NCONNECTIONS;
330 recorded[n].f = random() % NFILES;
331 recorded[n].start = lock_base + ((uint_t)random() % (lock_range-1));
332 recorded[n].len = min_length +
333 random() % (lock_range-(recorded[n].start-lock_base));
334 recorded[n].start *= RANGE_MULTIPLE;
335 recorded[n].len *= RANGE_MULTIPLE;
339 recorded[n].lock_type = READ_LOCK;
341 recorded[n].lock_type = WRITE_LOCK;
344 recorded[n].lock_op = OP_LOCK;
345 } else if (r2 < UNLOCK_PCT) {
346 recorded[n].lock_op = OP_UNLOCK;
348 recorded[n].lock_op = OP_REOPEN;
350 recorded[n].needed = True;
351 if (!zero_zero && recorded[n].start==0 && recorded[n].len==0) {
359 reconnect(cli, fnum, share);
360 open_files(cli, fnum);
361 n = retest(cli, fnum, numops);
363 if (n == numops || !analyze) return;
371 close_files(cli, fnum);
372 reconnect(cli, fnum, share);
373 open_files(cli, fnum);
375 for (i=0;i<n-skip;i+=skip) {
377 printf("excluding %d-%d\n", i, i+skip-1);
378 for (j=i;j<i+skip;j++) {
379 recorded[j].needed = False;
382 close_files(cli, fnum);
383 open_files(cli, fnum);
385 m = retest(cli, fnum, n);
387 for (j=i;j<i+skip;j++) {
388 recorded[j].needed = True;
391 if (i+(skip-1) < m) {
392 memmove(&recorded[i], &recorded[i+skip],
393 (m-(i+skip-1))*sizeof(recorded[0]));
402 printf("skip=%d\n", skip);
409 close_files(cli, fnum);
410 reconnect(cli, fnum, share);
411 open_files(cli, fnum);
413 n1 = retest(cli, fnum, n);
415 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
417 close_files(cli, fnum);
420 printf("{%d, %d, %u, %u, %.0f, %.0f, %u},\n",
422 recorded[i].lock_type,
425 (double)recorded[i].start,
426 (double)recorded[i].len,
433 static void usage(void)
437 locktest //server1/share1 //server2/share2 [options..]\n\
439 -U user%%pass (may be specified twice)\n\
443 -u hide unlock fails\n\
445 -A analyse for minimal ops\n\
447 -E enable exact error code checking\n\
448 -Z enable the zero/zero lock\n\
449 -R range set lock range\n\
450 -B base set lock base\n\
451 -M min set min lock length\n\
455 /****************************************************************************
457 ****************************************************************************/
458 int main(int argc,char *argv[])
460 char *share[NSERVERS];
467 setup_logging("locktest", DEBUG_STDOUT);
469 if (argc < 3 || argv[1][0] == '-') {
474 setup_logging(argv[0], DEBUG_STDOUT);
476 for (server=0;server<NSERVERS;server++) {
477 share[server] = argv[1+server];
478 all_string_sub(share[server],"/","\\",0);
484 lp_load(dyn_CONFIGFILE,True,False,False);
487 if (getenv("USER")) {
488 fstrcpy(username[0],getenv("USER"));
489 fstrcpy(username[1],getenv("USER"));
494 while ((opt = getopt(argc, argv, "U:s:ho:aAW:OkR:B:M:EZW:")) != EOF) {
500 d_printf("No kerberos support compiled in\n");
507 d_printf("Max of 2 usernames\n");
510 fstrcpy(username[got_pass],optarg);
511 p = strchr_m(username[got_pass],'%');
514 fstrcpy(password[got_pass], p+1);
519 lock_range = strtol(optarg, NULL, 0);
522 lock_base = strtol(optarg, NULL, 0);
525 min_length = strtol(optarg, NULL, 0);
531 hide_unlock_fails = True;
534 numops = atoi(optarg);
549 exact_error_codes = True;
552 lp_set_cmdline("workgroup", optarg);
558 printf("Unknown option %c (%d)\n", (char)opt, opt);
563 if(use_kerberos && !got_user) got_pass = True;
568 DEBUG(0,("seed=%u base=%d range=%d min_length=%d\n",
569 seed, lock_base, lock_range, min_length));