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/passwd.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;
32 #define FILENAME "\\locktest.dat"
37 #define LOCKBASE (0x40000000 - 50)
43 #define RANGE_MULTIPLE 1
46 #define NCONNECTIONS 2
49 #define LOCK_TIMEOUT 0
61 static struct record *recorded;
63 static int try_open(struct smbcli_state *c, char *nfs, int fstype, const char *fname, int flags)
69 return smbcli_open(c, fname, flags, DENY_NONE);
72 slprintf(path, sizeof(path), "%s%s", nfs, fname);
73 pstring_sub(path,"\\", "/");
74 return open(path, flags, 0666);
80 static bool try_close(struct smbcli_state *c, int fstype, int fd)
84 return smbcli_close(c, fd);
87 return close(fd) == 0;
93 static bool try_lock(struct smbcli_state *c, int fstype,
94 int fd, uint_t start, uint_t len,
101 return smbcli_lock(c, fd, start, len, LOCK_TIMEOUT, op);
104 lock.l_type = (op==READ_LOCK) ? F_RDLCK:F_WRLCK;
105 lock.l_whence = SEEK_SET;
106 lock.l_start = start;
108 lock.l_pid = getpid();
109 return fcntl(fd,F_SETLK,&lock) == 0;
115 static bool try_unlock(struct smbcli_state *c, int fstype,
116 int fd, uint_t start, uint_t len)
122 return smbcli_unlock(c, fd, start, len);
125 lock.l_type = F_UNLCK;
126 lock.l_whence = SEEK_SET;
127 lock.l_start = start;
129 lock.l_pid = getpid();
130 return fcntl(fd,F_SETLK,&lock) == 0;
136 /*****************************************************
137 return a connection to a server
138 *******************************************************/
139 static struct smbcli_state *connect_one(char *share, const char **ports)
141 struct smbcli_state *c;
148 fstrcpy(server,share+2);
149 share = strchr_m(server,'\\');
150 if (!share) return NULL;
157 char *pass = getpass("Password: ");
159 fstrcpy(password, pass);
163 slprintf(myname,sizeof(myname), "lock-%u-%u", getpid(), count++);
165 nt_status = smbcli_full_connection(NULL,
166 &c, myname, server_n, ports, share, NULL,
167 username, lp_workgroup(), password, NULL);
168 if (!NT_STATUS_IS_OK(nt_status)) {
169 DEBUG(0, ("smbcli_full_connection failed with error %s\n", nt_errstr(nt_status)));
173 c->use_oplocks = use_oplocks;
179 static void reconnect(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
181 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
183 char *share1, char *share2)
185 int server, conn, f, fstype;
192 for (server=0;server<NSERVERS;server++)
193 for (conn=0;conn<NCONNECTIONS;conn++) {
194 if (cli[server][conn]) {
195 for (f=0;f<NFILES;f++) {
196 smbcli_close(cli[server][conn], fnum[server][fstype][conn][f]);
198 smbcli_ulogoff(cli[server][conn]);
199 talloc_free(cli[server][conn]);
201 cli[server][conn] = connect_one(share[server], ports);
202 if (!cli[server][conn]) {
203 DEBUG(0,("Failed to connect to %s\n", share[server]));
211 static bool test_one(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
213 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
216 uint_t conn = rec->conn;
218 uint_t fstype = rec->fstype;
219 uint_t start = rec->start;
220 uint_t len = rec->len;
235 for (server=0;server<NSERVERS;server++) {
236 ret[server] = try_lock(cli[server][conn], fstype,
237 fnum[server][fstype][conn][f],
240 if (showall || ret[0] != ret[1]) {
241 printf("lock conn=%u fstype=%u f=%u range=%u:%u(%u) op=%s -> %u:%u\n",
243 start, start+len-1, len,
244 op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
247 if (ret[0] != ret[1]) return false;
248 } else if (r2 < LOCK_PCT+UNLOCK_PCT) {
250 for (server=0;server<NSERVERS;server++) {
251 ret[server] = try_unlock(cli[server][conn], fstype,
252 fnum[server][fstype][conn][f],
255 if (showall || (!hide_unlock_fails && (ret[0] != ret[1]))) {
256 printf("unlock conn=%u fstype=%u f=%u range=%u:%u(%u) -> %u:%u\n",
258 start, start+len-1, len,
261 if (!hide_unlock_fails && ret[0] != ret[1]) return false;
263 /* reopen the file */
264 for (server=0;server<NSERVERS;server++) {
265 try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
266 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
268 if (fnum[server][fstype][conn][f] == -1) {
269 printf("failed to reopen on share1\n");
274 printf("reopen conn=%u fstype=%u f=%u\n",
281 static void close_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
283 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
285 int server, conn, f, fstype;
287 for (server=0;server<NSERVERS;server++)
288 for (fstype=0;fstype<NUMFSTYPES;fstype++)
289 for (conn=0;conn<NCONNECTIONS;conn++)
290 for (f=0;f<NFILES;f++) {
291 if (fnum[server][fstype][conn][f] != -1) {
292 try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
293 fnum[server][fstype][conn][f] = -1;
296 for (server=0;server<NSERVERS;server++) {
297 smbcli_unlink(cli[server][0], FILENAME);
301 static void open_files(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
303 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
305 int server, fstype, conn, f;
307 for (server=0;server<NSERVERS;server++)
308 for (fstype=0;fstype<NUMFSTYPES;fstype++)
309 for (conn=0;conn<NCONNECTIONS;conn++)
310 for (f=0;f<NFILES;f++) {
311 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
313 if (fnum[server][fstype][conn][f] == -1) {
314 fprintf(stderr,"Failed to open fnum[%u][%u][%u][%u]\n",
315 server, fstype, conn, f);
322 static int retest(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
324 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
328 printf("testing %u ...\n", n);
329 for (i=0; i<n; i++) {
330 if (i && i % 100 == 0) {
334 if (recorded[i].needed &&
335 !test_one(cli, nfs, fnum, &recorded[i])) return i;
341 /* each server has two connections open to it. Each connection has two file
342 descriptors open on the file - 8 file descriptors in total
344 we then do random locking ops in tamdem on the 4 fnums from each
345 server and ensure that the results match
347 static void test_locks(char *share1, char *share2, char *nfspath1, char *nfspath2, const char **ports)
349 struct smbcli_state *cli[NSERVERS][NCONNECTIONS];
351 int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES];
360 recorded = malloc_array_p(struct record, numops);
362 for (n=0; n<numops; n++) {
363 recorded[n].conn = random() % NCONNECTIONS;
364 recorded[n].fstype = random() % NUMFSTYPES;
365 recorded[n].f = random() % NFILES;
366 recorded[n].start = LOCKBASE + ((uint_t)random() % (LOCKRANGE-1));
367 recorded[n].len = 1 +
368 random() % (LOCKRANGE-(recorded[n].start-LOCKBASE));
369 recorded[n].start *= RANGE_MULTIPLE;
370 recorded[n].len *= RANGE_MULTIPLE;
371 recorded[n].r1 = random() % 100;
372 recorded[n].r2 = random() % 100;
373 recorded[n].needed = true;
376 reconnect(cli, nfs, fnum, share1, share2, ports);
377 open_files(cli, nfs, fnum);
378 n = retest(cli, nfs, fnum, numops);
380 if (n == numops || !analyze) return;
386 close_files(cli, nfs, fnum);
387 reconnect(cli, nfs, fnum, share1, share2, ports);
388 open_files(cli, nfs, fnum);
390 for (i=0;i<n-1;i++) {
392 recorded[i].needed = false;
394 close_files(cli, nfs, fnum);
395 open_files(cli, nfs, fnum);
397 m = retest(cli, nfs, fnum, n);
399 recorded[i].needed = true;
402 memmove(&recorded[i], &recorded[i+1],
403 (m-i)*sizeof(recorded[0]));
413 close_files(cli, nfs, fnum);
414 reconnect(cli, nfs, fnum, share1, share2, ports);
415 open_files(cli, nfs, fnum);
417 n1 = retest(cli, nfs, fnum, n);
419 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
421 close_files(cli, nfs, fnum);
424 printf("{%u, %u, %u, %u, %u, %u, %u, %u},\n",
438 static void usage(void)
442 locktest //server1/share1 //server2/share2 /path1 /path2 [options..]\n\
447 -u hide unlock fails\n\
453 /****************************************************************************
455 ****************************************************************************/
456 int main(int argc,char *argv[])
458 char *share1, *share2, *nfspath1, *nfspath2;
464 struct loadparm_context *lp_ctx;
470 if (argc < 5 || argv[1][0] == '-') {
480 all_string_sub(share1,"/","\\",0);
481 all_string_sub(share2,"/","\\",0);
483 setup_logging(argv[0], DEBUG_STDOUT);
488 lp_ctx = loadparm_init(talloc_autofree_context());
489 lp_load(lp_ctx, dyn_CONFIGFILE);
491 if (getenv("USER")) {
492 fstrcpy(username,getenv("USER"));
497 while ((opt = getopt(argc, argv, "U:s:ho:aAW:O")) != EOF) {
500 fstrcpy(username,optarg);
501 p = strchr_m(username,'%');
504 fstrcpy(password, p+1);
512 hide_unlock_fails = true;
515 numops = atoi(optarg);
530 printf("Unknown option %c (%d)\n", (char)opt, opt);
538 DEBUG(0,("seed=%u\n", seed));
542 test_locks(share1, share2, nfspath1, nfspath2, lp_smb_ports(lp_ctx));