s4:torture: fix a comment typo.
[obnox/samba/samba-obnox.git] / source4 / torture / basic / misc.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SMB torture tester
4    Copyright (C) Andrew Tridgell 1997-2003
5    Copyright (C) Jelmer Vernooij 2006
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "libcli/raw/libcliraw.h"
23 #include "libcli/raw/raw_proto.h"
24 #include "system/time.h"
25 #include "system/wait.h"
26 #include "system/filesys.h"
27 #include "../libcli/smb/smb_constants.h"
28 #include "libcli/libcli.h"
29 #include "lib/events/events.h"
30 #include "libcli/resolve/resolve.h"
31 #include "torture/smbtorture.h"
32 #include "torture/util.h"
33 #include "libcli/smb_composite/smb_composite.h"
34 #include "libcli/composite/composite.h"
35 #include "param/param.h"
36 #include "torture/basic/proto.h"
37
38 extern struct cli_credentials *cmdline_credentials;
39         
40 static bool wait_lock(struct smbcli_state *c, int fnum, uint32_t offset, uint32_t len)
41 {
42         while (NT_STATUS_IS_ERR(smbcli_lock(c->tree, fnum, offset, len, -1, WRITE_LOCK))) {
43                 if (!check_error(__location__, c, ERRDOS, ERRlock, NT_STATUS_LOCK_NOT_GRANTED)) return false;
44         }
45         return true;
46 }
47
48
49 static bool rw_torture(struct torture_context *tctx, struct smbcli_state *c)
50 {
51         const char *lockfname = "\\torture.lck";
52         char *fname;
53         int fnum;
54         int fnum2;
55         pid_t pid2, pid = getpid();
56         int i, j;
57         uint8_t buf[1024];
58         bool correct = true;
59
60         fnum2 = smbcli_open(c->tree, lockfname, O_RDWR | O_CREAT | O_EXCL, 
61                          DENY_NONE);
62         if (fnum2 == -1)
63                 fnum2 = smbcli_open(c->tree, lockfname, O_RDWR, DENY_NONE);
64         if (fnum2 == -1) {
65                 torture_comment(tctx, "open of %s failed (%s)\n", lockfname, smbcli_errstr(c->tree));
66                 return false;
67         }
68
69         generate_random_buffer(buf, sizeof(buf));
70
71         for (i=0;i<torture_numops;i++) {
72                 unsigned int n = (unsigned int)random()%10;
73                 if (i % 10 == 0) {
74                         if (torture_setting_bool(tctx, "progress", true)) {
75                                 torture_comment(tctx, "%d\r", i);
76                                 fflush(stdout);
77                         }
78                 }
79                 asprintf(&fname, "\\torture.%u", n);
80
81                 if (!wait_lock(c, fnum2, n*sizeof(int), sizeof(int))) {
82                         return false;
83                 }
84
85                 fnum = smbcli_open(c->tree, fname, O_RDWR | O_CREAT | O_TRUNC, DENY_ALL);
86                 if (fnum == -1) {
87                         torture_comment(tctx, "open failed (%s)\n", smbcli_errstr(c->tree));
88                         correct = false;
89                         break;
90                 }
91
92                 if (smbcli_write(c->tree, fnum, 0, &pid, 0, sizeof(pid)) != sizeof(pid)) {
93                         torture_comment(tctx, "write failed (%s)\n", smbcli_errstr(c->tree));
94                         correct = false;
95                 }
96
97                 for (j=0;j<50;j++) {
98                         if (smbcli_write(c->tree, fnum, 0, buf, 
99                                       sizeof(pid)+(j*sizeof(buf)), 
100                                       sizeof(buf)) != sizeof(buf)) {
101                                 torture_comment(tctx, "write failed (%s)\n", smbcli_errstr(c->tree));
102                                 correct = false;
103                         }
104                 }
105
106                 pid2 = 0;
107
108                 if (smbcli_read(c->tree, fnum, &pid2, 0, sizeof(pid)) != sizeof(pid)) {
109                         torture_comment(tctx, "read failed (%s)\n", smbcli_errstr(c->tree));
110                         correct = false;
111                 }
112
113                 if (pid2 != pid) {
114                         torture_comment(tctx, "data corruption!\n");
115                         correct = false;
116                 }
117
118                 if (NT_STATUS_IS_ERR(smbcli_close(c->tree, fnum))) {
119                         torture_comment(tctx, "close failed (%s)\n", smbcli_errstr(c->tree));
120                         correct = false;
121                 }
122
123                 if (NT_STATUS_IS_ERR(smbcli_unlink(c->tree, fname))) {
124                         torture_comment(tctx, "unlink failed (%s)\n", smbcli_errstr(c->tree));
125                         correct = false;
126                 }
127
128                 if (NT_STATUS_IS_ERR(smbcli_unlock(c->tree, fnum2, n*sizeof(int), sizeof(int)))) {
129                         torture_comment(tctx, "unlock failed (%s)\n", smbcli_errstr(c->tree));
130                         correct = false;
131                 }
132                 free(fname);
133         }
134
135         smbcli_close(c->tree, fnum2);
136         smbcli_unlink(c->tree, lockfname);
137
138         torture_comment(tctx, "%d\n", i);
139
140         return correct;
141 }
142
143 bool run_torture(struct torture_context *tctx, struct smbcli_state *cli, int dummy)
144 {
145         return rw_torture(tctx, cli);
146 }
147
148
149 /*
150   see how many RPC pipes we can open at once
151 */
152 bool run_pipe_number(struct torture_context *tctx, 
153                                          struct smbcli_state *cli1)
154 {
155         const char *pipe_name = "\\WKSSVC";
156         int fnum;
157         int num_pipes = 0;
158
159         while(1) {
160                 fnum = smbcli_nt_create_full(cli1->tree, pipe_name, 0, SEC_FILE_READ_DATA, FILE_ATTRIBUTE_NORMAL,
161                                    NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE, NTCREATEX_DISP_OPEN_IF, 0, 0);
162
163                 if (fnum == -1) {
164                         torture_comment(tctx, "Open of pipe %s failed with error (%s)\n", pipe_name, smbcli_errstr(cli1->tree));
165                         break;
166                 }
167                 num_pipes++;
168                 if (torture_setting_bool(tctx, "progress", true)) {
169                         torture_comment(tctx, "%d\r", num_pipes);
170                         fflush(stdout);
171                 }
172         }
173
174         torture_comment(tctx, "pipe_number test - we can open %d %s pipes.\n", num_pipes, pipe_name );
175         return true;
176 }
177
178
179
180
181 /*
182   open N connections to the server and just hold them open
183   used for testing performance when there are N idle users
184   already connected
185  */
186 bool torture_holdcon(struct torture_context *tctx)
187 {
188         int i;
189         struct smbcli_state **cli;
190         int num_dead = 0;
191
192         torture_comment(tctx, "Opening %d connections\n", torture_numops);
193         
194         cli = malloc_array_p(struct smbcli_state *, torture_numops);
195
196         for (i=0;i<torture_numops;i++) {
197                 if (!torture_open_connection(&cli[i], tctx, i)) {
198                         return false;
199                 }
200                 if (torture_setting_bool(tctx, "progress", true)) {
201                         torture_comment(tctx, "opened %d connections\r", i+1);
202                         fflush(stdout);
203                 }
204         }
205
206         torture_comment(tctx, "\nStarting pings\n");
207
208         while (1) {
209                 for (i=0;i<torture_numops;i++) {
210                         NTSTATUS status;
211                         if (cli[i]) {
212                                 status = smbcli_chkpath(cli[i]->tree, "\\");
213                                 if (!NT_STATUS_IS_OK(status)) {
214                                         torture_comment(tctx, "Connection %d is dead\n", i);
215                                         cli[i] = NULL;
216                                         num_dead++;
217                                 }
218                                 usleep(100);
219                         }
220                 }
221
222                 if (num_dead == torture_numops) {
223                         torture_comment(tctx, "All connections dead - finishing\n");
224                         break;
225                 }
226
227                 torture_comment(tctx, ".");
228                 fflush(stdout);
229         }
230
231         return true;
232 }
233
234 /*
235   open a file N times on the server and just hold them open
236   used for testing performance when there are N file handles
237   open
238  */
239 bool torture_holdopen(struct torture_context *tctx,
240                       struct smbcli_state *cli)
241 {
242         int i, fnum;
243         const char *fname = "\\holdopen.dat";
244         NTSTATUS status;
245
246         smbcli_unlink(cli->tree, fname);
247
248         fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
249         if (fnum == -1) {
250                 torture_comment(tctx, "open of %s failed (%s)\n", fname, smbcli_errstr(cli->tree));
251                 return false;
252         }
253
254         smbcli_close(cli->tree, fnum);
255
256         for (i=0;i<torture_numops;i++) {
257                 union smb_open op;
258
259                 op.generic.level = RAW_OPEN_NTCREATEX;
260                 op.ntcreatex.in.root_fid.fnum = 0;
261                 op.ntcreatex.in.flags = 0;
262                 op.ntcreatex.in.access_mask = SEC_FILE_WRITE_DATA;
263                 op.ntcreatex.in.create_options = 0;
264                 op.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
265                 op.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_MASK;
266                 op.ntcreatex.in.alloc_size = 0;
267                 op.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
268                 op.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
269                 op.ntcreatex.in.security_flags = 0;
270                 op.ntcreatex.in.fname = fname;
271                 status = smb_raw_open(cli->tree, tctx, &op);
272                 if (!NT_STATUS_IS_OK(status)) {
273                         torture_warning(tctx, "open %d failed\n", i);
274                         continue;
275                 }
276
277                 if (torture_setting_bool(tctx, "progress", true)) {
278                         torture_comment(tctx, "opened %d file\r", i);
279                         fflush(stdout);
280                 }
281         }
282
283         torture_comment(tctx, "\nStarting pings\n");
284
285         while (1) {
286                 struct smb_echo ec;
287
288                 status = smb_raw_echo(cli->transport, &ec);
289                 torture_comment(tctx, ".");
290                 fflush(stdout);
291                 sleep(15);
292         }
293 }
294
295 /*
296 test how many open files this server supports on the one socket
297 */
298 bool torture_maxfid_test(struct torture_context *tctx, struct smbcli_state *cli)
299 {
300 #define MAXFID_TEMPLATE "\\maxfid\\fid%d\\maxfid.%d.%d"
301         char *fname;
302         int fnums[0x11000], i;
303         int retries=4, maxfid;
304         bool correct = true;
305
306         if (retries <= 0) {
307                 torture_comment(tctx, "failed to connect\n");
308                 return false;
309         }
310
311         if (smbcli_deltree(cli->tree, "\\maxfid") == -1) {
312                 torture_comment(tctx, "Failed to deltree \\maxfid - %s\n",
313                        smbcli_errstr(cli->tree));
314                 return false;
315         }
316         if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, "\\maxfid"))) {
317                 torture_comment(tctx, "Failed to mkdir \\maxfid, error=%s\n", 
318                        smbcli_errstr(cli->tree));
319                 return false;
320         }
321
322         torture_comment(tctx, "Testing maximum number of open files\n");
323
324         for (i=0; i<0x11000; i++) {
325                 if (i % 1000 == 0) {
326                         asprintf(&fname, "\\maxfid\\fid%d", i/1000);
327                         if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, fname))) {
328                                 torture_comment(tctx, "Failed to mkdir %s, error=%s\n", 
329                                        fname, smbcli_errstr(cli->tree));
330                                 return false;
331                         }
332                         free(fname);
333                 }
334                 asprintf(&fname, MAXFID_TEMPLATE, i/1000, i,(int)getpid());
335                 if ((fnums[i] = smbcli_open(cli->tree, fname, 
336                                         O_RDWR|O_CREAT|O_TRUNC, DENY_NONE)) ==
337                     -1) {
338                         torture_comment(tctx, "open of %s failed (%s)\n", 
339                                fname, smbcli_errstr(cli->tree));
340                         torture_comment(tctx, "maximum fnum is %d\n", i);
341                         break;
342                 }
343                 free(fname);
344                 if (torture_setting_bool(tctx, "progress", true)) {
345                         torture_comment(tctx, "%6d\r", i);
346                         fflush(stdout);
347                 }
348         }
349         torture_comment(tctx, "%6d\n", i);
350
351         maxfid = i;
352
353         torture_comment(tctx, "cleaning up\n");
354         for (i=0;i<maxfid;i++) {
355                 asprintf(&fname, MAXFID_TEMPLATE, i/1000, i,(int)getpid());
356                 if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnums[i]))) {
357                         torture_comment(tctx, "Close of fnum %d failed - %s\n", fnums[i], smbcli_errstr(cli->tree));
358                 }
359                 if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, fname))) {
360                         torture_comment(tctx, "unlink of %s failed (%s)\n", 
361                                fname, smbcli_errstr(cli->tree));
362                         correct = false;
363                 }
364                 free(fname);
365
366                 if (torture_setting_bool(tctx, "progress", true)) {
367                         torture_comment(tctx, "%6d\r", i);
368                         fflush(stdout);
369                 }
370         }
371         torture_comment(tctx, "%6d\n", 0);
372
373         if (smbcli_deltree(cli->tree, "\\maxfid") == -1) {
374                 torture_comment(tctx, "Failed to deltree \\maxfid - %s\n",
375                        smbcli_errstr(cli->tree));
376                 return false;
377         }
378
379         torture_comment(tctx, "maxfid test finished\n");
380
381         return correct;
382 #undef MAXFID_TEMPLATE
383 }
384
385
386
387 /*
388   sees what IOCTLs are supported
389  */
390 bool torture_ioctl_test(struct torture_context *tctx, 
391                                                 struct smbcli_state *cli)
392 {
393         uint16_t device, function;
394         int fnum;
395         const char *fname = "\\ioctl.dat";
396         NTSTATUS status;
397         union smb_ioctl parms;
398         TALLOC_CTX *mem_ctx;
399
400         mem_ctx = talloc_named_const(tctx, 0, "ioctl_test");
401
402         smbcli_unlink(cli->tree, fname);
403
404         fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
405         if (fnum == -1) {
406                 torture_comment(tctx, "open of %s failed (%s)\n", fname, smbcli_errstr(cli->tree));
407                 return false;
408         }
409
410         parms.ioctl.level = RAW_IOCTL_IOCTL;
411         parms.ioctl.in.file.fnum = fnum;
412         parms.ioctl.in.request = IOCTL_QUERY_JOB_INFO;
413         status = smb_raw_ioctl(cli->tree, mem_ctx, &parms);
414         torture_comment(tctx, "ioctl job info: %s\n", smbcli_errstr(cli->tree));
415
416         for (device=0;device<0x100;device++) {
417                 torture_comment(tctx, "Testing device=0x%x\n", device);
418                 for (function=0;function<0x100;function++) {
419                         parms.ioctl.in.request = (device << 16) | function;
420                         status = smb_raw_ioctl(cli->tree, mem_ctx, &parms);
421
422                         if (NT_STATUS_IS_OK(status)) {
423                                 torture_comment(tctx, "ioctl device=0x%x function=0x%x OK : %d bytes\n", 
424                                         device, function, (int)parms.ioctl.out.blob.length);
425                         }
426                 }
427         }
428
429         return true;
430 }
431
432 static void benchrw_callback(struct smbcli_request *req);
433 enum benchrw_stage {
434         START,
435         OPEN_CONNECTION,
436         CLEANUP_TESTDIR,
437         MK_TESTDIR,
438         OPEN_FILE,
439         INITIAL_WRITE,
440         READ_WRITE_DATA,
441         MAX_OPS_REACHED,
442         ERROR,
443         CLOSE_FILE,
444         CLEANUP,
445         FINISHED
446 };
447
448 struct benchrw_state {
449         struct torture_context *tctx;
450         char *dname;
451         char *fname;
452         uint16_t fnum;
453         int nr;
454         struct smbcli_tree      *cli;           
455         uint8_t *buffer;
456         int writecnt;
457         int readcnt;
458         int completed;
459         int num_parallel_requests;
460         void *req_params;
461         enum benchrw_stage mode;
462         struct params{
463                 struct unclist{
464                         const char *host;
465                         const char *share;
466                 } **unc;
467                 const char *workgroup;
468                 int retry;
469                 unsigned int writeblocks;
470                 unsigned int blocksize;
471                 unsigned int writeratio;
472                 int num_parallel_requests;
473         } *lpcfg_params;
474 };
475
476 /* 
477         init params using lpcfg_parm_xxx
478         return number of unclist entries
479 */
480 static int init_benchrw_params(struct torture_context *tctx,
481                                struct params *lpar)
482 {
483         char **unc_list = NULL;
484         int num_unc_names = 0, conn_index=0, empty_lines=0;
485         const char *p;
486         lpar->retry = torture_setting_int(tctx, "retry",3);
487         lpar->blocksize = torture_setting_int(tctx, "blocksize",65535);
488         lpar->writeblocks = torture_setting_int(tctx, "writeblocks",15);
489         lpar->writeratio = torture_setting_int(tctx, "writeratio",5);
490         lpar->num_parallel_requests = torture_setting_int(
491                 tctx, "parallel_requests", 5);
492         lpar->workgroup = lpcfg_workgroup(tctx->lp_ctx);
493         
494         p = torture_setting_string(tctx, "unclist", NULL);
495         if (p) {
496                 char *h, *s;
497                 unc_list = file_lines_load(p, &num_unc_names, 0, NULL);
498                 if (!unc_list || num_unc_names <= 0) {
499                         torture_comment(tctx, "Failed to load unc names list "
500                                         "from '%s'\n", p);
501                         exit(1);
502                 }
503                 
504                 lpar->unc = talloc_array(tctx, struct unclist *,
505                                          (num_unc_names-empty_lines));
506                 for(conn_index = 0; conn_index < num_unc_names; conn_index++) {
507                         /* ignore empty lines */
508                         if(strlen(unc_list[conn_index % num_unc_names])==0){
509                                 empty_lines++;
510                                 continue;
511                         }
512                         if (!smbcli_parse_unc(
513                                     unc_list[conn_index % num_unc_names],
514                                     NULL, &h, &s)) {
515                                 torture_comment(
516                                         tctx, "Failed to parse UNC "
517                                         "name %s\n",
518                                         unc_list[conn_index % num_unc_names]);
519                                 exit(1);
520                         }
521                         lpar->unc[conn_index-empty_lines] =
522                                 talloc(tctx, struct unclist);
523                         lpar->unc[conn_index-empty_lines]->host = h;
524                         lpar->unc[conn_index-empty_lines]->share = s;   
525                 }
526                 return num_unc_names-empty_lines;
527         }else{
528                 lpar->unc = talloc_array(tctx, struct unclist *, 1);
529                 lpar->unc[0] = talloc(tctx,struct unclist);
530                 lpar->unc[0]->host  = torture_setting_string(tctx, "host",
531                                                              NULL);
532                 lpar->unc[0]->share = torture_setting_string(tctx, "share",
533                                                              NULL);
534                 return 1;
535         }
536 }
537
538 /*
539  Called when the reads & writes are finished. closes the file.
540 */
541 static NTSTATUS benchrw_close(struct torture_context *tctx,
542                               struct smbcli_request *req,
543                               struct benchrw_state *state)
544 {
545         union smb_close close_parms;
546         
547         NT_STATUS_NOT_OK_RETURN(req->status);
548         
549         torture_comment(tctx, "Close file %d (%d)\n",state->nr,state->fnum);
550         close_parms.close.level = RAW_CLOSE_CLOSE;
551         close_parms.close.in.file.fnum = state->fnum ;
552         close_parms.close.in.write_time = 0;
553         state->mode=CLOSE_FILE;
554         
555         req = smb_raw_close_send(state->cli, &close_parms);
556         NT_STATUS_HAVE_NO_MEMORY(req);
557         /*register the callback function!*/
558         req->async.fn = benchrw_callback;
559         req->async.private_data = state;
560         
561         return NT_STATUS_OK;
562 }
563
564 static NTSTATUS benchrw_readwrite(struct torture_context *tctx,
565                                   struct benchrw_state *state);
566 static void benchrw_callback(struct smbcli_request *req);
567
568 static void benchrw_rw_callback(struct smbcli_request *req)
569 {
570         struct benchrw_state *state = req->async.private_data;
571         struct torture_context *tctx = state->tctx;
572
573         if (!NT_STATUS_IS_OK(req->status)) {
574                 state->mode = ERROR;
575                 return;
576         }
577
578         state->completed++;
579         state->num_parallel_requests--;
580
581         if ((state->completed >= torture_numops)
582             && (state->num_parallel_requests == 0)) {
583                 benchrw_callback(req);
584                 talloc_free(req);
585                 return;
586         }
587
588         talloc_free(req);
589
590         if (state->completed + state->num_parallel_requests
591             < torture_numops) {
592                 benchrw_readwrite(tctx, state);
593         }
594 }
595
596 /*
597  Called when the initial write is completed is done. write or read a file.
598 */
599 static NTSTATUS benchrw_readwrite(struct torture_context *tctx,
600                                   struct benchrw_state *state)
601 {
602         struct smbcli_request *req;
603         union smb_read  rd;
604         union smb_write wr;
605         
606         /* randomize between writes and reads*/
607         if (random() % state->lpcfg_params->writeratio == 0) {
608                 torture_comment(tctx, "Callback WRITE file:%d (%d/%d)\n",
609                                 state->nr,state->completed,torture_numops);
610                 wr.generic.level = RAW_WRITE_WRITEX  ;
611                 wr.writex.in.file.fnum  = state->fnum ;
612                 wr.writex.in.offset     = 0;
613                 wr.writex.in.wmode      = 0             ;
614                 wr.writex.in.remaining  = 0;
615                 wr.writex.in.count      = state->lpcfg_params->blocksize;
616                 wr.writex.in.data       = state->buffer;
617                 state->readcnt=0;
618                 req = smb_raw_write_send(state->cli,&wr);
619         }
620         else {
621                 torture_comment(tctx,
622                                 "Callback READ file:%d (%d/%d) Offset:%d\n",
623                                 state->nr,state->completed,torture_numops,
624                                 (state->readcnt*state->lpcfg_params->blocksize));
625                 rd.generic.level = RAW_READ_READX;
626                 rd.readx.in.file.fnum   = state->fnum   ;
627                 rd.readx.in.offset      = state->readcnt*state->lpcfg_params->blocksize;
628                 rd.readx.in.mincnt      = state->lpcfg_params->blocksize;
629                 rd.readx.in.maxcnt      = rd.readx.in.mincnt;
630                 rd.readx.in.remaining   = 0     ;
631                 rd.readx.out.data       = state->buffer;
632                 rd.readx.in.read_for_execute = false;
633                 if(state->readcnt < state->lpcfg_params->writeblocks){
634                         state->readcnt++;       
635                 }else{
636                         /*start reading from beginn of file*/
637                         state->readcnt=0;
638                 }
639                 req = smb_raw_read_send(state->cli,&rd);
640         }
641         state->num_parallel_requests += 1;
642         NT_STATUS_HAVE_NO_MEMORY(req);
643         /*register the callback function!*/
644         req->async.fn = benchrw_rw_callback;
645         req->async.private_data = state;
646         
647         return NT_STATUS_OK;
648 }
649
650 /*
651  Called when the open is done. writes to the file.
652 */
653 static NTSTATUS benchrw_open(struct torture_context *tctx,
654                              struct smbcli_request *req,
655                              struct benchrw_state *state)
656 {
657         union smb_write wr;
658         if(state->mode == OPEN_FILE){
659                 NTSTATUS status;
660                 status = smb_raw_open_recv(req,tctx,(
661                                         union smb_open*)state->req_params);
662                 NT_STATUS_NOT_OK_RETURN(status);
663         
664                 state->fnum = ((union smb_open*)state->req_params)
665                                                 ->openx.out.file.fnum;
666                 torture_comment(tctx, "File opened (%d)\n",state->fnum);
667                 state->mode=INITIAL_WRITE;
668         }
669                 
670         torture_comment(tctx, "Write initial test file:%d (%d/%d)\n",state->nr,
671                 (state->writecnt+1)*state->lpcfg_params->blocksize,
672                 (state->lpcfg_params->writeblocks*state->lpcfg_params->blocksize));
673         wr.generic.level = RAW_WRITE_WRITEX  ;
674         wr.writex.in.file.fnum  = state->fnum ;
675         wr.writex.in.offset     = state->writecnt * 
676                                         state->lpcfg_params->blocksize;
677         wr.writex.in.wmode      = 0             ;
678         wr.writex.in.remaining  = (state->lpcfg_params->writeblocks *
679                                                 state->lpcfg_params->blocksize)-
680                                                 ((state->writecnt+1)*state->
681                                                 lpcfg_params->blocksize);
682         wr.writex.in.count      = state->lpcfg_params->blocksize;
683         wr.writex.in.data       = state->buffer;
684         state->writecnt++;
685         if(state->writecnt == state->lpcfg_params->writeblocks){
686                 state->mode=READ_WRITE_DATA;
687         }
688         req = smb_raw_write_send(state->cli,&wr);
689         NT_STATUS_HAVE_NO_MEMORY(req);
690         
691         /*register the callback function!*/
692         req->async.fn = benchrw_callback;
693         req->async.private_data = state;
694         return NT_STATUS_OK;
695
696
697 /*
698  Called when the mkdir is done. Opens a file.
699 */
700 static NTSTATUS benchrw_mkdir(struct torture_context *tctx,
701                               struct smbcli_request *req,
702                               struct benchrw_state *state)
703 {
704         union smb_open *open_parms;     
705         uint8_t *writedata;     
706                 
707         NT_STATUS_NOT_OK_RETURN(req->status);
708         
709         /* open/create the files */
710         torture_comment(tctx, "Open File %d/%d\n",state->nr+1,
711                         torture_setting_int(tctx, "nprocs", 4));
712         open_parms=talloc_zero(tctx, union smb_open);
713         NT_STATUS_HAVE_NO_MEMORY(open_parms);
714         open_parms->openx.level = RAW_OPEN_OPENX;
715         open_parms->openx.in.flags = 0;
716         open_parms->openx.in.open_mode = OPENX_MODE_ACCESS_RDWR;
717         open_parms->openx.in.search_attrs = 
718                         FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
719         open_parms->openx.in.file_attrs = 0;
720         open_parms->openx.in.write_time = 0;
721         open_parms->openx.in.open_func = OPENX_OPEN_FUNC_CREATE;
722         open_parms->openx.in.size = 0;
723         open_parms->openx.in.timeout = 0;
724         open_parms->openx.in.fname = state->fname;
725                 
726         writedata = talloc_size(tctx,state->lpcfg_params->blocksize);
727         NT_STATUS_HAVE_NO_MEMORY(writedata);
728         generate_random_buffer(writedata,state->lpcfg_params->blocksize);
729         state->buffer=writedata;
730         state->writecnt=1;
731         state->readcnt=0;
732         state->req_params=open_parms;           
733         state->mode=OPEN_FILE;  
734                         
735         req = smb_raw_open_send(state->cli,open_parms);
736         NT_STATUS_HAVE_NO_MEMORY(req);
737         
738         /*register the callback function!*/
739         req->async.fn = benchrw_callback;
740         req->async.private_data = state;
741                 
742         return NT_STATUS_OK;
743 }
744
745 /*
746  handler for completion of a sub-request of the bench-rw test
747 */
748 static void benchrw_callback(struct smbcli_request *req)
749 {
750         struct benchrw_state *state = req->async.private_data;
751         struct torture_context *tctx = state->tctx;
752         
753         /*dont send new requests when torture_numops is reached*/
754         if ((state->mode == READ_WRITE_DATA)
755             && (state->completed >= torture_numops)) {
756                 state->mode=MAX_OPS_REACHED;
757         }
758         
759         switch (state->mode) {
760         
761         case MK_TESTDIR:
762                 if (!NT_STATUS_IS_OK(benchrw_mkdir(tctx, req,state))) {
763                         torture_comment(tctx, "Failed to create the test "
764                                         "directory - %s\n", 
765                                         nt_errstr(req->status));
766                         state->mode=ERROR;
767                         return;
768                 }
769                 break;  
770         case OPEN_FILE:
771         case INITIAL_WRITE:
772                 if (!NT_STATUS_IS_OK(benchrw_open(tctx, req,state))){
773                         torture_comment(tctx, "Failed to open/write the "
774                                         "file - %s\n", 
775                                         nt_errstr(req->status));
776                         state->mode=ERROR;
777                         state->readcnt=0;
778                         return;
779                 }
780                 break;
781         case READ_WRITE_DATA:
782                 while (state->num_parallel_requests
783                        < state->lpcfg_params->num_parallel_requests) {
784                         NTSTATUS status;
785                         status = benchrw_readwrite(tctx,state);
786                         if (!NT_STATUS_IS_OK(status)){
787                                 torture_comment(tctx, "Failed to read/write "
788                                                 "the file - %s\n", 
789                                                 nt_errstr(req->status));
790                                 state->mode=ERROR;
791                                 return;
792                         }
793                 }
794                 break;
795         case MAX_OPS_REACHED:
796                 if (!NT_STATUS_IS_OK(benchrw_close(tctx,req,state))){
797                         torture_comment(tctx, "Failed to read/write/close "
798                                         "the file - %s\n", 
799                                         nt_errstr(req->status));
800                         state->mode=ERROR;
801                         return;
802                 }
803                 break;
804         case CLOSE_FILE:
805                 torture_comment(tctx, "File %d closed\n",state->nr);
806                 if (!NT_STATUS_IS_OK(req->status)) {
807                         torture_comment(tctx, "Failed to close the "
808                                         "file - %s\n",
809                                         nt_errstr(req->status));
810                         state->mode=ERROR;
811                         return;
812                 }
813                 state->mode=CLEANUP;
814                 return; 
815         default:
816                 break;
817         }
818         
819 }
820
821 /* open connection async callback function*/
822 static void async_open_callback(struct composite_context *con)
823 {
824         struct benchrw_state *state = con->async.private_data;
825         struct torture_context *tctx = state->tctx;
826         int retry = state->lpcfg_params->retry;
827                 
828         if (NT_STATUS_IS_OK(con->status)) {
829                 state->cli=((struct smb_composite_connect*)
830                                         state->req_params)->out.tree;
831                 state->mode=CLEANUP_TESTDIR;
832         }else{
833                 if(state->writecnt < retry){
834                         torture_comment(tctx, "Failed to open connection: "
835                                         "%d, Retry (%d/%d)\n",
836                                         state->nr,state->writecnt,retry);
837                         state->writecnt++;
838                         state->mode=START;
839                         usleep(1000);   
840                 }else{
841                         torture_comment(tctx, "Failed to open connection "
842                                         "(%d) - %s\n",
843                                         state->nr, nt_errstr(con->status));
844                         state->mode=ERROR;
845                 }
846                 return;
847         }       
848 }
849
850 /*
851  establishs a smbcli_tree from scratch (async)
852 */
853 static struct composite_context *torture_connect_async(
854                                 struct torture_context *tctx,
855                                 struct smb_composite_connect *smb,
856                                 TALLOC_CTX *mem_ctx,
857                                 struct tevent_context *ev,
858                                 const char *host,
859                                 const char *share,
860                                 const char *workgroup)
861 {
862         torture_comment(tctx, "Open Connection to %s/%s\n",host,share);
863         smb->in.dest_host=talloc_strdup(mem_ctx,host);
864         smb->in.service=talloc_strdup(mem_ctx,share);
865         smb->in.dest_ports=lpcfg_smb_ports(tctx->lp_ctx);
866         smb->in.socket_options = lpcfg_socket_options(tctx->lp_ctx);
867         smb->in.called_name = strupper_talloc(mem_ctx, host);
868         smb->in.service_type=NULL;
869         smb->in.credentials=cmdline_credentials;
870         smb->in.fallback_to_anonymous=false;
871         smb->in.gensec_settings = lpcfg_gensec_settings(mem_ctx, tctx->lp_ctx);
872         smb->in.workgroup=workgroup;
873         lpcfg_smbcli_options(tctx->lp_ctx, &smb->in.options);
874         lpcfg_smbcli_session_options(tctx->lp_ctx, &smb->in.session_options);
875         
876         return smb_composite_connect_send(smb,mem_ctx,
877                                           lpcfg_resolve_context(tctx->lp_ctx),ev);
878 }
879
880 bool run_benchrw(struct torture_context *tctx)
881 {
882         struct smb_composite_connect *smb_con;
883         const char *fname = "\\rwtest.dat";
884         struct smbcli_request *req;
885         struct benchrw_state **state;
886         int i , num_unc_names;
887         struct tevent_context   *ev     ;       
888         struct composite_context *req1;
889         struct params lpparams;
890         union smb_mkdir parms;
891         int finished = 0;
892         bool success=true;
893         int torture_nprocs = torture_setting_int(tctx, "nprocs", 4);
894         
895         torture_comment(tctx, "Start BENCH-READWRITE num_ops=%d "
896                         "num_nprocs=%d\n",
897                         torture_numops, torture_nprocs);
898
899         /*init talloc context*/
900         ev = tctx->ev;
901         state = talloc_array(tctx, struct benchrw_state *, torture_nprocs);
902
903         /* init params using lpcfg_parm_xxx */
904         num_unc_names = init_benchrw_params(tctx,&lpparams);
905         
906         /* init private data structs*/
907         for(i = 0; i<torture_nprocs;i++){
908                 state[i]=talloc(tctx,struct benchrw_state);
909                 state[i]->tctx = tctx;
910                 state[i]->completed=0;
911                 state[i]->num_parallel_requests=0;
912                 state[i]->lpcfg_params=&lpparams;
913                 state[i]->nr=i;
914                 state[i]->dname=talloc_asprintf(tctx,"benchrw%d",i);
915                 state[i]->fname=talloc_asprintf(tctx,"%s%s",
916                                                 state[i]->dname,fname); 
917                 state[i]->mode=START;
918                 state[i]->writecnt=0;
919         }
920         
921         torture_comment(tctx, "Starting async requests\n");     
922         while(finished != torture_nprocs){
923                 finished=0;
924                 for(i = 0; i<torture_nprocs;i++){
925                         switch (state[i]->mode){
926                         /*open multiple connections with the same userid */
927                         case START:
928                                 smb_con = talloc(
929                                         tctx,struct smb_composite_connect) ;
930                                 state[i]->req_params=smb_con; 
931                                 state[i]->mode=OPEN_CONNECTION;
932                                 req1 = torture_connect_async(
933                                         tctx, smb_con, tctx,ev,
934                                         lpparams.unc[i % num_unc_names]->host,
935                                         lpparams.unc[i % num_unc_names]->share,
936                                         lpparams.workgroup);
937                                 /* register callback fn + private data */
938                                 req1->async.fn = async_open_callback;
939                                 req1->async.private_data=state[i];
940                                 break;
941                         /*setup test dirs (sync)*/
942                         case CLEANUP_TESTDIR:
943                                 torture_comment(tctx, "Setup test dir %d\n",i);
944                                 smb_raw_exit(state[i]->cli->session);
945                                 if (smbcli_deltree(state[i]->cli, 
946                                                 state[i]->dname) == -1) {
947                                         torture_comment(
948                                                 tctx,
949                                                 "Unable to delete %s - %s\n", 
950                                                 state[i]->dname,
951                                                 smbcli_errstr(state[i]->cli));
952                                         state[i]->mode=ERROR;
953                                         break;
954                                 }
955                                 state[i]->mode=MK_TESTDIR;
956                                 parms.mkdir.level = RAW_MKDIR_MKDIR;
957                                 parms.mkdir.in.path = state[i]->dname;
958                                 req = smb_raw_mkdir_send(state[i]->cli,&parms);
959                                 /* register callback fn + private data */
960                                 req->async.fn = benchrw_callback;
961                                 req->async.private_data=state[i];
962                                 break;
963                         /* error occured , finish */
964                         case ERROR:
965                                 finished++;
966                                 success=false;
967                                 break;
968                         /* cleanup , close connection */
969                         case CLEANUP:
970                                 torture_comment(tctx, "Deleting test dir %s "
971                                                 "%d/%d\n",state[i]->dname,
972                                                 i+1,torture_nprocs);
973                                 smbcli_deltree(state[i]->cli,state[i]->dname);
974                                 if (NT_STATUS_IS_ERR(smb_tree_disconnect(
975                                                              state[i]->cli))) {
976                                         torture_comment(tctx, "ERROR: Tree "
977                                                         "disconnect failed");
978                                         state[i]->mode=ERROR;
979                                         break;
980                                 }
981                                 state[i]->mode=FINISHED;
982                         case FINISHED:
983                                 finished++;
984                                 break;
985                         default:
986                                 tevent_loop_once(ev);
987                         }
988                 }
989         }
990                                 
991         return success; 
992 }
993