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 if (asprintf(&path, "%s%s", nfs, fname) > 0) {
76 string_replace(path,'\\', '/');
77 ret = open(path, flags, 0666);
87 static bool try_close(struct cli_state *c, int fstype, int fd)
91 return cli_close(c, fd);
94 return close(fd) == 0;
100 static bool try_lock(struct cli_state *c, int fstype,
101 int fd, unsigned start, unsigned len,
108 return cli_lock(c, fd, start, len, LOCK_TIMEOUT, op);
111 lock.l_type = (op==READ_LOCK) ? F_RDLCK:F_WRLCK;
112 lock.l_whence = SEEK_SET;
113 lock.l_start = start;
115 lock.l_pid = getpid();
116 return fcntl(fd,F_SETLK,&lock) == 0;
122 static bool try_unlock(struct cli_state *c, int fstype,
123 int fd, unsigned start, unsigned len)
129 return cli_unlock(c, fd, start, len);
132 lock.l_type = F_UNLCK;
133 lock.l_whence = SEEK_SET;
134 lock.l_start = start;
136 lock.l_pid = getpid();
137 return fcntl(fd,F_SETLK,&lock) == 0;
143 static void print_brl(struct file_id id, struct server_id pid,
144 enum brl_type lock_type,
145 enum brl_flavour lock_flav,
146 br_off start, br_off size,
149 printf("%6d %s %s %.0f:%.0f(%.0f)\n",
150 (int)procid_to_pid(&pid), file_id_string_tos(&id),
151 lock_type==READ_LOCK?"R":"W",
152 (double)start, (double)start+size-1,(double)size);
156 /*****************************************************
157 return a connection to a server
158 *******************************************************/
159 static struct cli_state *connect_one(char *share)
168 fstrcpy(server,share+2);
169 share = strchr_m(server,'\\');
170 if (!share) return NULL;
177 char *pass = getpass("Password: ");
179 fstrcpy(password, pass);
183 slprintf(myname,sizeof(myname), "lock-%lu-%u", (unsigned long)getpid(), count++);
185 nt_status = cli_full_connection(&c, myname, server_n, NULL, 0, share, "?????",
186 username, lp_workgroup(), password, 0,
189 if (!NT_STATUS_IS_OK(nt_status)) {
190 DEBUG(0, ("cli_full_connection failed with error %s\n", nt_errstr(nt_status)));
194 c->use_oplocks = use_oplocks;
200 static void reconnect(struct cli_state *cli[NSERVERS][NCONNECTIONS],
202 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
203 char *share1, char *share2)
205 int server, conn, f, fstype;
212 for (server=0;server<NSERVERS;server++)
213 for (conn=0;conn<NCONNECTIONS;conn++) {
214 if (cli[server][conn]) {
215 for (f=0;f<NFILES;f++) {
216 cli_close(cli[server][conn], fnum[server][fstype][conn][f]);
218 cli_ulogoff(cli[server][conn]);
219 cli_shutdown(cli[server][conn]);
221 cli[server][conn] = connect_one(share[server]);
222 if (!cli[server][conn]) {
223 DEBUG(0,("Failed to connect to %s\n", share[server]));
231 static bool test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS],
233 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
236 unsigned conn = rec->conn;
238 unsigned fstype = rec->fstype;
239 unsigned start = rec->start;
240 unsigned len = rec->len;
241 unsigned r1 = rec->r1;
242 unsigned r2 = rec->r2;
255 for (server=0;server<NSERVERS;server++) {
256 ret[server] = try_lock(cli[server][conn], fstype,
257 fnum[server][fstype][conn][f],
260 if (showall || ret[0] != ret[1]) {
261 printf("lock conn=%u fstype=%u f=%u range=%u:%u(%u) op=%s -> %u:%u\n",
263 start, start+len-1, len,
264 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
267 if (showall) brl_forall(print_brl, NULL);
268 if (ret[0] != ret[1]) return False;
269 } else if (r2 < LOCK_PCT+UNLOCK_PCT) {
271 for (server=0;server<NSERVERS;server++) {
272 ret[server] = try_unlock(cli[server][conn], fstype,
273 fnum[server][fstype][conn][f],
276 if (showall || (!hide_unlock_fails && (ret[0] != ret[1]))) {
277 printf("unlock conn=%u fstype=%u f=%u range=%u:%u(%u) -> %u:%u\n",
279 start, start+len-1, len,
282 if (showall) brl_forall(print_brl, NULL);
283 if (!hide_unlock_fails && ret[0] != ret[1]) return False;
285 /* reopen the file */
286 for (server=0;server<NSERVERS;server++) {
287 try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
288 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
290 if (fnum[server][fstype][conn][f] == -1) {
291 printf("failed to reopen on share1\n");
296 printf("reopen conn=%u fstype=%u f=%u\n",
298 brl_forall(print_brl, NULL);
304 static void close_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
306 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
308 int server, conn, f, fstype;
310 for (server=0;server<NSERVERS;server++)
311 for (fstype=0;fstype<NUMFSTYPES;fstype++)
312 for (conn=0;conn<NCONNECTIONS;conn++)
313 for (f=0;f<NFILES;f++) {
314 if (fnum[server][fstype][conn][f] != -1) {
315 try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
316 fnum[server][fstype][conn][f] = -1;
319 for (server=0;server<NSERVERS;server++) {
320 cli_unlink(cli[server][0], FILENAME);
324 static void open_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
326 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
328 int server, fstype, conn, f;
330 for (server=0;server<NSERVERS;server++)
331 for (fstype=0;fstype<NUMFSTYPES;fstype++)
332 for (conn=0;conn<NCONNECTIONS;conn++)
333 for (f=0;f<NFILES;f++) {
334 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
336 if (fnum[server][fstype][conn][f] == -1) {
337 fprintf(stderr,"Failed to open fnum[%u][%u][%u][%u]\n",
338 server, fstype, conn, f);
345 static int retest(struct cli_state *cli[NSERVERS][NCONNECTIONS],
347 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
351 printf("testing %u ...\n", n);
352 for (i=0; i<n; i++) {
353 if (i && i % 100 == 0) {
357 if (recorded[i].needed &&
358 !test_one(cli, nfs, fnum, &recorded[i])) return i;
364 /* each server has two connections open to it. Each connection has two file
365 descriptors open on the file - 8 file descriptors in total
367 we then do random locking ops in tamdem on the 4 fnums from each
368 server and ensure that the results match
370 static void test_locks(char *share1, char *share2, char *nfspath1, char *nfspath2)
372 struct cli_state *cli[NSERVERS][NCONNECTIONS];
374 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES];
383 recorded = SMB_MALLOC_ARRAY(struct record, numops);
385 for (n=0; n<numops; n++) {
386 recorded[n].conn = random() % NCONNECTIONS;
387 recorded[n].fstype = random() % NUMFSTYPES;
388 recorded[n].f = random() % NFILES;
389 recorded[n].start = LOCKBASE + ((unsigned)random() % (LOCKRANGE-1));
390 recorded[n].len = 1 +
391 random() % (LOCKRANGE-(recorded[n].start-LOCKBASE));
392 recorded[n].start *= RANGE_MULTIPLE;
393 recorded[n].len *= RANGE_MULTIPLE;
394 recorded[n].r1 = random() % 100;
395 recorded[n].r2 = random() % 100;
396 recorded[n].needed = True;
399 reconnect(cli, nfs, fnum, share1, share2);
400 open_files(cli, nfs, fnum);
401 n = retest(cli, nfs, fnum, numops);
403 if (n == numops || !analyze) return;
409 close_files(cli, nfs, fnum);
410 reconnect(cli, nfs, fnum, share1, share2);
411 open_files(cli, nfs, fnum);
413 for (i=0;i<n-1;i++) {
415 recorded[i].needed = False;
417 close_files(cli, nfs, fnum);
418 open_files(cli, nfs, fnum);
420 m = retest(cli, nfs, fnum, n);
422 recorded[i].needed = True;
425 memmove(&recorded[i], &recorded[i+1],
426 (m-i)*sizeof(recorded[0]));
436 close_files(cli, nfs, fnum);
437 reconnect(cli, nfs, fnum, share1, share2);
438 open_files(cli, nfs, fnum);
440 n1 = retest(cli, nfs, fnum, n);
442 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
444 close_files(cli, nfs, fnum);
447 printf("{%u, %u, %u, %u, %u, %u, %u, %u},\n",
461 static void usage(void)
465 locktest //server1/share1 //server2/share2 /path1 /path2 [options..]\n\
470 -u hide unlock fails\n\
476 /****************************************************************************
478 ****************************************************************************/
479 int main(int argc,char *argv[])
481 char *share1, *share2, *nfspath1, *nfspath2;
490 if (argc < 5 || argv[1][0] == '-') {
500 all_string_sub(share1,"/","\\",0);
501 all_string_sub(share2,"/","\\",0);
503 setup_logging(argv[0],True);
508 lp_load(get_dyn_CONFIGFILE(),True,False,False,True);
511 if (getenv("USER")) {
512 fstrcpy(username,getenv("USER"));
517 while ((opt = getopt(argc, argv, "U:s:ho:aAW:O")) != EOF) {
520 fstrcpy(username,optarg);
521 p = strchr_m(username,'%');
524 fstrcpy(password, p+1);
532 hide_unlock_fails = True;
535 numops = atoi(optarg);
550 printf("Unknown option %c (%d)\n", (char)opt, opt);
558 DEBUG(0,("seed=%u\n", seed));
561 locking_init_readonly();
562 test_locks(share1, share2, nfspath1, nfspath2);