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/>.
21 #include "system/filesys.h"
23 static fstring password;
24 static fstring username;
26 static int numops = 1000;
29 static bool hide_unlock_fails;
30 static bool use_oplocks;
35 #define FILENAME "\\locktest.dat"
40 #define LOCKBASE (0x40000000 - 50)
46 #define RANGE_MULTIPLE 1
49 #define NCONNECTIONS 2
52 #define LOCK_TIMEOUT 0
64 static struct record *recorded;
66 static int try_open(struct cli_state *c, char *nfs, int fstype, const char *fname, int flags)
74 if (!NT_STATUS_IS_OK(cli_open(c, fname, flags, DENY_NONE, &fd))) {
81 if (asprintf(&path, "%s%s", nfs, fname) > 0) {
83 string_replace(path,'\\', '/');
84 ret = open(path, flags, 0666);
94 static bool try_close(struct cli_state *c, int fstype, int fd)
98 return NT_STATUS_IS_OK(cli_close(c, fd));
101 return close(fd) == 0;
107 static bool try_lock(struct cli_state *c, int fstype,
108 int fd, unsigned start, unsigned len,
115 return cli_lock(c, fd, start, len, LOCK_TIMEOUT, op);
118 lock.l_type = (op==READ_LOCK) ? F_RDLCK:F_WRLCK;
119 lock.l_whence = SEEK_SET;
120 lock.l_start = start;
122 lock.l_pid = getpid();
123 return fcntl(fd,F_SETLK,&lock) == 0;
129 static bool try_unlock(struct cli_state *c, int fstype,
130 int fd, unsigned start, unsigned len)
136 return NT_STATUS_IS_OK(cli_unlock(c, fd, start, len));
139 lock.l_type = F_UNLCK;
140 lock.l_whence = SEEK_SET;
141 lock.l_start = start;
143 lock.l_pid = getpid();
144 return fcntl(fd,F_SETLK,&lock) == 0;
150 static void print_brl(struct file_id id, struct server_id pid,
151 enum brl_type lock_type,
152 enum brl_flavour lock_flav,
153 br_off start, br_off size,
156 printf("%6d %s %s %.0f:%.0f(%.0f)\n",
157 (int)procid_to_pid(&pid), file_id_string_tos(&id),
158 lock_type==READ_LOCK?"R":"W",
159 (double)start, (double)start+size-1,(double)size);
163 /*****************************************************
164 return a connection to a server
165 *******************************************************/
166 static struct cli_state *connect_one(char *share)
175 fstrcpy(server,share+2);
176 share = strchr_m(server,'\\');
177 if (!share) return NULL;
184 char *pass = getpass("Password: ");
186 fstrcpy(password, pass);
190 slprintf(myname,sizeof(myname), "lock-%lu-%u", (unsigned long)getpid(), count++);
192 nt_status = cli_full_connection(&c, myname, server_n, NULL, 0, share, "?????",
193 username, lp_workgroup(), password, 0,
196 if (!NT_STATUS_IS_OK(nt_status)) {
197 DEBUG(0, ("cli_full_connection failed with error %s\n", nt_errstr(nt_status)));
201 c->use_oplocks = use_oplocks;
207 static void reconnect(struct cli_state *cli[NSERVERS][NCONNECTIONS],
209 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
210 char *share1, char *share2)
212 int server, conn, f, fstype;
219 for (server=0;server<NSERVERS;server++)
220 for (conn=0;conn<NCONNECTIONS;conn++) {
221 if (cli[server][conn]) {
222 for (f=0;f<NFILES;f++) {
223 cli_close(cli[server][conn], fnum[server][fstype][conn][f]);
225 cli_ulogoff(cli[server][conn]);
226 cli_shutdown(cli[server][conn]);
228 cli[server][conn] = connect_one(share[server]);
229 if (!cli[server][conn]) {
230 DEBUG(0,("Failed to connect to %s\n", share[server]));
238 static bool test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS],
240 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
243 unsigned conn = rec->conn;
245 unsigned fstype = rec->fstype;
246 unsigned start = rec->start;
247 unsigned len = rec->len;
248 unsigned r1 = rec->r1;
249 unsigned r2 = rec->r2;
262 for (server=0;server<NSERVERS;server++) {
263 ret[server] = try_lock(cli[server][conn], fstype,
264 fnum[server][fstype][conn][f],
267 if (showall || ret[0] != ret[1]) {
268 printf("lock conn=%u fstype=%u f=%u range=%u:%u(%u) op=%s -> %u:%u\n",
270 start, start+len-1, len,
271 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
274 if (showall) brl_forall(print_brl, NULL);
275 if (ret[0] != ret[1]) return False;
276 } else if (r2 < LOCK_PCT+UNLOCK_PCT) {
278 for (server=0;server<NSERVERS;server++) {
279 ret[server] = try_unlock(cli[server][conn], fstype,
280 fnum[server][fstype][conn][f],
283 if (showall || (!hide_unlock_fails && (ret[0] != ret[1]))) {
284 printf("unlock conn=%u fstype=%u f=%u range=%u:%u(%u) -> %u:%u\n",
286 start, start+len-1, len,
289 if (showall) brl_forall(print_brl, NULL);
290 if (!hide_unlock_fails && ret[0] != ret[1]) return False;
292 /* reopen the file */
293 for (server=0;server<NSERVERS;server++) {
294 try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
295 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
297 if (fnum[server][fstype][conn][f] == -1) {
298 printf("failed to reopen on share1\n");
303 printf("reopen conn=%u fstype=%u f=%u\n",
305 brl_forall(print_brl, NULL);
311 static void close_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
313 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
315 int server, conn, f, fstype;
317 for (server=0;server<NSERVERS;server++)
318 for (fstype=0;fstype<NUMFSTYPES;fstype++)
319 for (conn=0;conn<NCONNECTIONS;conn++)
320 for (f=0;f<NFILES;f++) {
321 if (fnum[server][fstype][conn][f] != -1) {
322 try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
323 fnum[server][fstype][conn][f] = -1;
326 for (server=0;server<NSERVERS;server++) {
327 cli_unlink(cli[server][0], FILENAME, aSYSTEM | aHIDDEN);
331 static void open_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
333 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
335 int server, fstype, conn, f;
337 for (server=0;server<NSERVERS;server++)
338 for (fstype=0;fstype<NUMFSTYPES;fstype++)
339 for (conn=0;conn<NCONNECTIONS;conn++)
340 for (f=0;f<NFILES;f++) {
341 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
343 if (fnum[server][fstype][conn][f] == -1) {
344 fprintf(stderr,"Failed to open fnum[%u][%u][%u][%u]\n",
345 server, fstype, conn, f);
352 static int retest(struct cli_state *cli[NSERVERS][NCONNECTIONS],
354 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
358 printf("testing %u ...\n", n);
359 for (i=0; i<n; i++) {
360 if (i && i % 100 == 0) {
364 if (recorded[i].needed &&
365 !test_one(cli, nfs, fnum, &recorded[i])) return i;
371 /* each server has two connections open to it. Each connection has two file
372 descriptors open on the file - 8 file descriptors in total
374 we then do random locking ops in tamdem on the 4 fnums from each
375 server and ensure that the results match
377 static void test_locks(char *share1, char *share2, char *nfspath1, char *nfspath2)
379 struct cli_state *cli[NSERVERS][NCONNECTIONS];
381 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES];
390 recorded = SMB_MALLOC_ARRAY(struct record, numops);
392 for (n=0; n<numops; n++) {
393 recorded[n].conn = random() % NCONNECTIONS;
394 recorded[n].fstype = random() % NUMFSTYPES;
395 recorded[n].f = random() % NFILES;
396 recorded[n].start = LOCKBASE + ((unsigned)random() % (LOCKRANGE-1));
397 recorded[n].len = 1 +
398 random() % (LOCKRANGE-(recorded[n].start-LOCKBASE));
399 recorded[n].start *= RANGE_MULTIPLE;
400 recorded[n].len *= RANGE_MULTIPLE;
401 recorded[n].r1 = random() % 100;
402 recorded[n].r2 = random() % 100;
403 recorded[n].needed = True;
406 reconnect(cli, nfs, fnum, share1, share2);
407 open_files(cli, nfs, fnum);
408 n = retest(cli, nfs, fnum, numops);
410 if (n == numops || !analyze) return;
416 close_files(cli, nfs, fnum);
417 reconnect(cli, nfs, fnum, share1, share2);
418 open_files(cli, nfs, fnum);
420 for (i=0;i<n-1;i++) {
422 recorded[i].needed = False;
424 close_files(cli, nfs, fnum);
425 open_files(cli, nfs, fnum);
427 m = retest(cli, nfs, fnum, n);
429 recorded[i].needed = True;
432 memmove(&recorded[i], &recorded[i+1],
433 (m-i)*sizeof(recorded[0]));
443 close_files(cli, nfs, fnum);
444 reconnect(cli, nfs, fnum, share1, share2);
445 open_files(cli, nfs, fnum);
447 n1 = retest(cli, nfs, fnum, n);
449 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
451 close_files(cli, nfs, fnum);
454 printf("{%u, %u, %u, %u, %u, %u, %u, %u},\n",
468 static void usage(void)
472 locktest //server1/share1 //server2/share2 /path1 /path2 [options..]\n\
477 -u hide unlock fails\n\
483 /****************************************************************************
485 ****************************************************************************/
486 int main(int argc,char *argv[])
488 char *share1, *share2, *nfspath1, *nfspath2;
495 if (argc < 5 || argv[1][0] == '-') {
500 setup_logging(argv[0], DEBUG_STDOUT);
507 all_string_sub(share1,"/","\\",0);
508 all_string_sub(share2,"/","\\",0);
513 lp_load(get_dyn_CONFIGFILE(),True,False,False,True);
516 if (getenv("USER")) {
517 fstrcpy(username,getenv("USER"));
522 while ((opt = getopt(argc, argv, "U:s:ho:aAW:O")) != EOF) {
525 fstrcpy(username,optarg);
526 p = strchr_m(username,'%');
529 fstrcpy(password, p+1);
537 hide_unlock_fails = True;
540 numops = atoi(optarg);
555 printf("Unknown option %c (%d)\n", (char)opt, opt);
563 DEBUG(0,("seed=%u\n", seed));
566 locking_init_readonly();
567 test_locks(share1, share2, nfspath1, nfspath2);