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 "libsmb/libsmb.h"
22 #include "system/filesys.h"
23 #include "locking/proto.h"
25 static fstring password
;
26 static fstring username
;
28 static int numops
= 1000;
31 static bool hide_unlock_fails
;
32 static bool use_oplocks
;
37 #define FILENAME "\\locktest.dat"
42 #define LOCKBASE (0x40000000 - 50)
48 #define RANGE_MULTIPLE 1
51 #define NCONNECTIONS 2
54 #define LOCK_TIMEOUT 0
66 static struct record
*recorded
;
68 static int try_open(struct cli_state
*c
, char *nfs
, int fstype
, const char *fname
, int flags
)
76 if (!NT_STATUS_IS_OK(cli_openx(c
, fname
, flags
, DENY_NONE
, &fd
))) {
83 if (asprintf(&path
, "%s%s", nfs
, fname
) > 0) {
85 string_replace(path
,'\\', '/');
86 ret
= open(path
, flags
, 0666);
96 static bool try_close(struct cli_state
*c
, int fstype
, int fd
)
100 return NT_STATUS_IS_OK(cli_close(c
, fd
));
103 return close(fd
) == 0;
109 static bool try_lock(struct cli_state
*c
, int fstype
,
110 int fd
, unsigned start
, unsigned len
,
117 return NT_STATUS_IS_OK(cli_lock32(c
, fd
, start
, len
,
121 lock
.l_type
= (op
==READ_LOCK
) ? F_RDLCK
:F_WRLCK
;
122 lock
.l_whence
= SEEK_SET
;
123 lock
.l_start
= start
;
125 lock
.l_pid
= getpid();
126 return fcntl(fd
,F_SETLK
,&lock
) == 0;
132 static bool try_unlock(struct cli_state
*c
, int fstype
,
133 int fd
, unsigned start
, unsigned len
)
139 return NT_STATUS_IS_OK(cli_unlock(c
, fd
, start
, len
));
142 lock
.l_type
= F_UNLCK
;
143 lock
.l_whence
= SEEK_SET
;
144 lock
.l_start
= start
;
146 lock
.l_pid
= getpid();
147 return fcntl(fd
,F_SETLK
,&lock
) == 0;
153 static void print_brl(struct file_id id
, struct server_id pid
,
154 enum brl_type lock_type
,
155 enum brl_flavour lock_flav
,
156 br_off start
, br_off size
,
159 printf("%6d %s %s %.0f:%.0f(%.0f)\n",
160 (int)procid_to_pid(&pid
), file_id_string_tos(&id
),
161 lock_type
==READ_LOCK
?"R":"W",
162 (double)start
, (double)start
+size
-1,(double)size
);
166 /*****************************************************
167 return a connection to a server
168 *******************************************************/
169 static struct cli_state
*connect_one(char *share
)
178 fstrcpy(server
,share
+2);
179 share
= strchr_m(server
,'\\');
180 if (!share
) return NULL
;
190 rc
= samba_getpass("Password: ", pwd
, sizeof(pwd
), false, false);
192 fstrcpy(password
, pwd
);
196 slprintf(myname
,sizeof(myname
), "lock-%lu-%u", (unsigned long)getpid(), count
++);
198 nt_status
= cli_full_connection(&c
, myname
, server_n
, NULL
, 0, share
, "?????",
199 username
, lp_workgroup(), password
, 0,
200 SMB_SIGNING_DEFAULT
);
202 if (!NT_STATUS_IS_OK(nt_status
)) {
203 DEBUG(0, ("cli_full_connection failed with error %s\n", nt_errstr(nt_status
)));
207 c
->use_oplocks
= use_oplocks
;
213 static void reconnect(struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
],
215 int fnum
[NSERVERS
][NUMFSTYPES
][NCONNECTIONS
][NFILES
],
216 char *share1
, char *share2
)
218 int server
, conn
, f
, fstype
;
225 for (server
=0;server
<NSERVERS
;server
++)
226 for (conn
=0;conn
<NCONNECTIONS
;conn
++) {
227 if (cli
[server
][conn
]) {
228 for (f
=0;f
<NFILES
;f
++) {
229 cli_close(cli
[server
][conn
], fnum
[server
][fstype
][conn
][f
]);
231 cli_ulogoff(cli
[server
][conn
]);
232 cli_shutdown(cli
[server
][conn
]);
234 cli
[server
][conn
] = connect_one(share
[server
]);
235 if (!cli
[server
][conn
]) {
236 DEBUG(0,("Failed to connect to %s\n", share
[server
]));
244 static bool test_one(struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
],
246 int fnum
[NSERVERS
][NUMFSTYPES
][NCONNECTIONS
][NFILES
],
249 unsigned conn
= rec
->conn
;
251 unsigned fstype
= rec
->fstype
;
252 unsigned start
= rec
->start
;
253 unsigned len
= rec
->len
;
254 unsigned r1
= rec
->r1
;
255 unsigned r2
= rec
->r2
;
268 for (server
=0;server
<NSERVERS
;server
++) {
269 ret
[server
] = try_lock(cli
[server
][conn
], fstype
,
270 fnum
[server
][fstype
][conn
][f
],
273 if (showall
|| ret
[0] != ret
[1]) {
274 printf("lock conn=%u fstype=%u f=%u range=%u:%u(%u) op=%s -> %u:%u\n",
276 start
, start
+len
-1, len
,
277 op
==READ_LOCK
?"READ_LOCK":"WRITE_LOCK",
280 if (showall
) brl_forall(print_brl
, NULL
);
281 if (ret
[0] != ret
[1]) return False
;
282 } else if (r2
< LOCK_PCT
+UNLOCK_PCT
) {
284 for (server
=0;server
<NSERVERS
;server
++) {
285 ret
[server
] = try_unlock(cli
[server
][conn
], fstype
,
286 fnum
[server
][fstype
][conn
][f
],
289 if (showall
|| (!hide_unlock_fails
&& (ret
[0] != ret
[1]))) {
290 printf("unlock conn=%u fstype=%u f=%u range=%u:%u(%u) -> %u:%u\n",
292 start
, start
+len
-1, len
,
295 if (showall
) brl_forall(print_brl
, NULL
);
296 if (!hide_unlock_fails
&& ret
[0] != ret
[1]) return False
;
298 /* reopen the file */
299 for (server
=0;server
<NSERVERS
;server
++) {
300 try_close(cli
[server
][conn
], fstype
, fnum
[server
][fstype
][conn
][f
]);
301 fnum
[server
][fstype
][conn
][f
] = try_open(cli
[server
][conn
], nfs
[server
], fstype
, FILENAME
,
303 if (fnum
[server
][fstype
][conn
][f
] == -1) {
304 printf("failed to reopen on share1\n");
309 printf("reopen conn=%u fstype=%u f=%u\n",
311 brl_forall(print_brl
, NULL
);
317 static void close_files(struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
],
319 int fnum
[NSERVERS
][NUMFSTYPES
][NCONNECTIONS
][NFILES
])
321 int server
, conn
, f
, fstype
;
323 for (server
=0;server
<NSERVERS
;server
++)
324 for (fstype
=0;fstype
<NUMFSTYPES
;fstype
++)
325 for (conn
=0;conn
<NCONNECTIONS
;conn
++)
326 for (f
=0;f
<NFILES
;f
++) {
327 if (fnum
[server
][fstype
][conn
][f
] != -1) {
328 try_close(cli
[server
][conn
], fstype
, fnum
[server
][fstype
][conn
][f
]);
329 fnum
[server
][fstype
][conn
][f
] = -1;
332 for (server
=0;server
<NSERVERS
;server
++) {
333 cli_unlink(cli
[server
][0], FILENAME
, FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
);
337 static void open_files(struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
],
339 int fnum
[NSERVERS
][NUMFSTYPES
][NCONNECTIONS
][NFILES
])
341 int server
, fstype
, conn
, f
;
343 for (server
=0;server
<NSERVERS
;server
++)
344 for (fstype
=0;fstype
<NUMFSTYPES
;fstype
++)
345 for (conn
=0;conn
<NCONNECTIONS
;conn
++)
346 for (f
=0;f
<NFILES
;f
++) {
347 fnum
[server
][fstype
][conn
][f
] = try_open(cli
[server
][conn
], nfs
[server
], fstype
, FILENAME
,
349 if (fnum
[server
][fstype
][conn
][f
] == -1) {
350 fprintf(stderr
,"Failed to open fnum[%u][%u][%u][%u]\n",
351 server
, fstype
, conn
, f
);
358 static int retest(struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
],
360 int fnum
[NSERVERS
][NUMFSTYPES
][NCONNECTIONS
][NFILES
],
364 printf("testing %u ...\n", n
);
365 for (i
=0; i
<n
; i
++) {
366 if (i
&& i
% 100 == 0) {
370 if (recorded
[i
].needed
&&
371 !test_one(cli
, nfs
, fnum
, &recorded
[i
])) return i
;
377 /* each server has two connections open to it. Each connection has two file
378 descriptors open on the file - 8 file descriptors in total
380 we then do random locking ops in tamdem on the 4 fnums from each
381 server and ensure that the results match
383 static void test_locks(char *share1
, char *share2
, char *nfspath1
, char *nfspath2
)
385 struct cli_state
*cli
[NSERVERS
][NCONNECTIONS
];
387 int fnum
[NSERVERS
][NUMFSTYPES
][NCONNECTIONS
][NFILES
];
396 recorded
= SMB_MALLOC_ARRAY(struct record
, numops
);
398 for (n
=0; n
<numops
; n
++) {
399 recorded
[n
].conn
= random() % NCONNECTIONS
;
400 recorded
[n
].fstype
= random() % NUMFSTYPES
;
401 recorded
[n
].f
= random() % NFILES
;
402 recorded
[n
].start
= LOCKBASE
+ ((unsigned)random() % (LOCKRANGE
-1));
403 recorded
[n
].len
= 1 +
404 random() % (LOCKRANGE
-(recorded
[n
].start
-LOCKBASE
));
405 recorded
[n
].start
*= RANGE_MULTIPLE
;
406 recorded
[n
].len
*= RANGE_MULTIPLE
;
407 recorded
[n
].r1
= random() % 100;
408 recorded
[n
].r2
= random() % 100;
409 recorded
[n
].needed
= True
;
412 reconnect(cli
, nfs
, fnum
, share1
, share2
);
413 open_files(cli
, nfs
, fnum
);
414 n
= retest(cli
, nfs
, fnum
, numops
);
416 if (n
== numops
|| !analyze
) return;
422 close_files(cli
, nfs
, fnum
);
423 reconnect(cli
, nfs
, fnum
, share1
, share2
);
424 open_files(cli
, nfs
, fnum
);
426 for (i
=0;i
<n
-1;i
++) {
428 recorded
[i
].needed
= False
;
430 close_files(cli
, nfs
, fnum
);
431 open_files(cli
, nfs
, fnum
);
433 m
= retest(cli
, nfs
, fnum
, n
);
435 recorded
[i
].needed
= True
;
438 memmove(&recorded
[i
], &recorded
[i
+1],
439 (m
-i
)*sizeof(recorded
[0]));
449 close_files(cli
, nfs
, fnum
);
450 reconnect(cli
, nfs
, fnum
, share1
, share2
);
451 open_files(cli
, nfs
, fnum
);
453 n1
= retest(cli
, nfs
, fnum
, n
);
455 printf("ERROR - inconsistent result (%u %u)\n", n1
, n
);
457 close_files(cli
, nfs
, fnum
);
460 printf("{%u, %u, %u, %u, %u, %u, %u, %u},\n",
474 static void usage(void)
478 locktest //server1/share1 //server2/share2 /path1 /path2 [options..]\n\
483 -u hide unlock fails\n\
489 /****************************************************************************
491 ****************************************************************************/
492 int main(int argc
,char *argv
[])
494 char *share1
, *share2
, *nfspath1
, *nfspath2
;
501 if (argc
< 5 || argv
[1][0] == '-') {
506 setup_logging(argv
[0], DEBUG_STDOUT
);
513 all_string_sub(share1
,"/","\\",0);
514 all_string_sub(share2
,"/","\\",0);
519 lp_load_global(get_dyn_CONFIGFILE());
522 if (getenv("USER")) {
523 fstrcpy(username
,getenv("USER"));
528 while ((opt
= getopt(argc
, argv
, "U:s:ho:aAW:O")) != EOF
) {
531 fstrcpy(username
,optarg
);
532 p
= strchr_m(username
,'%');
535 fstrcpy(password
, p
+1);
543 hide_unlock_fails
= True
;
546 numops
= atoi(optarg
);
561 printf("Unknown option %c (%d)\n", (char)opt
, opt
);
569 DEBUG(0,("seed=%u\n", seed
));
572 locking_init_readonly();
573 test_locks(share1
, share2
, nfspath1
, nfspath2
);