2 Unix SMB/CIFS implementation.
3 byte range lock tester - with local filesystem support
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/>.
22 static fstring password;
23 static fstring username;
25 static int numops = 1000;
28 static BOOL hide_unlock_fails;
29 static BOOL use_oplocks;
34 #define FILENAME "\\locktest.dat"
39 #define LOCKBASE (0x40000000 - 50)
45 #define RANGE_MULTIPLE 1
48 #define NCONNECTIONS 2
51 #define LOCK_TIMEOUT 0
63 static struct record *recorded;
65 static int try_open(struct cli_state *c, char *nfs, int fstype, const char *fname, int flags)
71 return cli_open(c, fname, flags, DENY_NONE);
74 slprintf(path, sizeof(path), "%s%s", nfs, fname);
75 pstring_sub(path,"\\", "/");
76 return open(path, flags, 0666);
82 static BOOL try_close(struct cli_state *c, int fstype, int fd)
86 return cli_close(c, fd);
89 return close(fd) == 0;
95 static BOOL try_lock(struct cli_state *c, int fstype,
96 int fd, unsigned start, unsigned len,
103 return cli_lock(c, fd, start, len, LOCK_TIMEOUT, op);
106 lock.l_type = (op==READ_LOCK) ? F_RDLCK:F_WRLCK;
107 lock.l_whence = SEEK_SET;
108 lock.l_start = start;
110 lock.l_pid = getpid();
111 return fcntl(fd,F_SETLK,&lock) == 0;
117 static BOOL try_unlock(struct cli_state *c, int fstype,
118 int fd, unsigned start, unsigned len)
124 return cli_unlock(c, fd, start, len);
127 lock.l_type = F_UNLCK;
128 lock.l_whence = SEEK_SET;
129 lock.l_start = start;
131 lock.l_pid = getpid();
132 return fcntl(fd,F_SETLK,&lock) == 0;
138 static void print_brl(struct file_id id, struct server_id pid,
139 enum brl_type lock_type,
140 enum brl_flavour lock_flav,
141 br_off start, br_off size,
144 printf("%6d %s %s %.0f:%.0f(%.0f)\n",
145 (int)procid_to_pid(&pid), file_id_static_string(&id),
146 lock_type==READ_LOCK?"R":"W",
147 (double)start, (double)start+size-1,(double)size);
151 /*****************************************************
152 return a connection to a server
153 *******************************************************/
154 static struct cli_state *connect_one(char *share)
163 fstrcpy(server,share+2);
164 share = strchr_m(server,'\\');
165 if (!share) return NULL;
172 char *pass = getpass("Password: ");
174 fstrcpy(password, pass);
178 slprintf(myname,sizeof(myname), "lock-%lu-%u", (unsigned long)getpid(), count++);
180 nt_status = cli_full_connection(&c, myname, server_n, NULL, 0, share, "?????",
181 username, lp_workgroup(), password, 0,
184 if (!NT_STATUS_IS_OK(nt_status)) {
185 DEBUG(0, ("cli_full_connection failed with error %s\n", nt_errstr(nt_status)));
189 c->use_oplocks = use_oplocks;
195 static void reconnect(struct cli_state *cli[NSERVERS][NCONNECTIONS],
197 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
198 char *share1, char *share2)
200 int server, conn, f, fstype;
207 for (server=0;server<NSERVERS;server++)
208 for (conn=0;conn<NCONNECTIONS;conn++) {
209 if (cli[server][conn]) {
210 for (f=0;f<NFILES;f++) {
211 cli_close(cli[server][conn], fnum[server][fstype][conn][f]);
213 cli_ulogoff(cli[server][conn]);
214 cli_shutdown(cli[server][conn]);
216 cli[server][conn] = connect_one(share[server]);
217 if (!cli[server][conn]) {
218 DEBUG(0,("Failed to connect to %s\n", share[server]));
226 static BOOL test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS],
228 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
231 unsigned conn = rec->conn;
233 unsigned fstype = rec->fstype;
234 unsigned start = rec->start;
235 unsigned len = rec->len;
236 unsigned r1 = rec->r1;
237 unsigned r2 = rec->r2;
250 for (server=0;server<NSERVERS;server++) {
251 ret[server] = try_lock(cli[server][conn], fstype,
252 fnum[server][fstype][conn][f],
255 if (showall || ret[0] != ret[1]) {
256 printf("lock conn=%u fstype=%u f=%u range=%u:%u(%u) op=%s -> %u:%u\n",
258 start, start+len-1, len,
259 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
262 if (showall) brl_forall(print_brl, NULL);
263 if (ret[0] != ret[1]) return False;
264 } else if (r2 < LOCK_PCT+UNLOCK_PCT) {
266 for (server=0;server<NSERVERS;server++) {
267 ret[server] = try_unlock(cli[server][conn], fstype,
268 fnum[server][fstype][conn][f],
271 if (showall || (!hide_unlock_fails && (ret[0] != ret[1]))) {
272 printf("unlock conn=%u fstype=%u f=%u range=%u:%u(%u) -> %u:%u\n",
274 start, start+len-1, len,
277 if (showall) brl_forall(print_brl, NULL);
278 if (!hide_unlock_fails && ret[0] != ret[1]) return False;
280 /* reopen the file */
281 for (server=0;server<NSERVERS;server++) {
282 try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
283 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
285 if (fnum[server][fstype][conn][f] == -1) {
286 printf("failed to reopen on share1\n");
291 printf("reopen conn=%u fstype=%u f=%u\n",
293 brl_forall(print_brl, NULL);
299 static void close_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
301 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
303 int server, conn, f, fstype;
305 for (server=0;server<NSERVERS;server++)
306 for (fstype=0;fstype<NUMFSTYPES;fstype++)
307 for (conn=0;conn<NCONNECTIONS;conn++)
308 for (f=0;f<NFILES;f++) {
309 if (fnum[server][fstype][conn][f] != -1) {
310 try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
311 fnum[server][fstype][conn][f] = -1;
314 for (server=0;server<NSERVERS;server++) {
315 cli_unlink(cli[server][0], FILENAME);
319 static void open_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
321 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
323 int server, fstype, conn, f;
325 for (server=0;server<NSERVERS;server++)
326 for (fstype=0;fstype<NUMFSTYPES;fstype++)
327 for (conn=0;conn<NCONNECTIONS;conn++)
328 for (f=0;f<NFILES;f++) {
329 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
331 if (fnum[server][fstype][conn][f] == -1) {
332 fprintf(stderr,"Failed to open fnum[%u][%u][%u][%u]\n",
333 server, fstype, conn, f);
340 static int retest(struct cli_state *cli[NSERVERS][NCONNECTIONS],
342 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
346 printf("testing %u ...\n", n);
347 for (i=0; i<n; i++) {
348 if (i && i % 100 == 0) {
352 if (recorded[i].needed &&
353 !test_one(cli, nfs, fnum, &recorded[i])) return i;
359 /* each server has two connections open to it. Each connection has two file
360 descriptors open on the file - 8 file descriptors in total
362 we then do random locking ops in tamdem on the 4 fnums from each
363 server and ensure that the results match
365 static void test_locks(char *share1, char *share2, char *nfspath1, char *nfspath2)
367 struct cli_state *cli[NSERVERS][NCONNECTIONS];
369 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES];
378 recorded = SMB_MALLOC_ARRAY(struct record, numops);
380 for (n=0; n<numops; n++) {
381 recorded[n].conn = random() % NCONNECTIONS;
382 recorded[n].fstype = random() % NUMFSTYPES;
383 recorded[n].f = random() % NFILES;
384 recorded[n].start = LOCKBASE + ((unsigned)random() % (LOCKRANGE-1));
385 recorded[n].len = 1 +
386 random() % (LOCKRANGE-(recorded[n].start-LOCKBASE));
387 recorded[n].start *= RANGE_MULTIPLE;
388 recorded[n].len *= RANGE_MULTIPLE;
389 recorded[n].r1 = random() % 100;
390 recorded[n].r2 = random() % 100;
391 recorded[n].needed = True;
394 reconnect(cli, nfs, fnum, share1, share2);
395 open_files(cli, nfs, fnum);
396 n = retest(cli, nfs, fnum, numops);
398 if (n == numops || !analyze) return;
404 close_files(cli, nfs, fnum);
405 reconnect(cli, nfs, fnum, share1, share2);
406 open_files(cli, nfs, fnum);
408 for (i=0;i<n-1;i++) {
410 recorded[i].needed = False;
412 close_files(cli, nfs, fnum);
413 open_files(cli, nfs, fnum);
415 m = retest(cli, nfs, fnum, n);
417 recorded[i].needed = True;
420 memmove(&recorded[i], &recorded[i+1],
421 (m-i)*sizeof(recorded[0]));
431 close_files(cli, nfs, fnum);
432 reconnect(cli, nfs, fnum, share1, share2);
433 open_files(cli, nfs, fnum);
435 n1 = retest(cli, nfs, fnum, n);
437 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
439 close_files(cli, nfs, fnum);
442 printf("{%u, %u, %u, %u, %u, %u, %u, %u},\n",
456 static void usage(void)
460 locktest //server1/share1 //server2/share2 /path1 /path2 [options..]\n\
465 -u hide unlock fails\n\
471 /****************************************************************************
473 ****************************************************************************/
474 int main(int argc,char *argv[])
476 char *share1, *share2, *nfspath1, *nfspath2;
485 if (argc < 5 || argv[1][0] == '-') {
495 all_string_sub(share1,"/","\\",0);
496 all_string_sub(share2,"/","\\",0);
498 setup_logging(argv[0],True);
503 lp_load(dyn_CONFIGFILE,True,False,False,True);
506 if (getenv("USER")) {
507 fstrcpy(username,getenv("USER"));
512 while ((opt = getopt(argc, argv, "U:s:ho:aAW:O")) != EOF) {
515 fstrcpy(username,optarg);
516 p = strchr_m(username,'%');
519 fstrcpy(password, p+1);
527 hide_unlock_fails = True;
530 numops = atoi(optarg);
545 printf("Unknown option %c (%d)\n", (char)opt, opt);
553 DEBUG(0,("seed=%u\n", seed));
557 test_locks(share1, share2, nfspath1, nfspath2);