r26654: libcli/smb_composite: Rather than specifying each of the gazillion options...
[samba.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 "system/time.h"
24 #include "system/wait.h"
25 #include "system/filesys.h"
26 #include "libcli/raw/ioctl.h"
27 #include "libcli/libcli.h"
28 #include "lib/events/events.h"
29 #include "libcli/resolve/resolve.h"
30 #include "auth/credentials/credentials.h"
31 #include "librpc/gen_ndr/ndr_nbt.h"
32 #include "torture/torture.h"
33 #include "torture/util.h"
34 #include "libcli/smb_composite/smb_composite.h"
35 #include "libcli/composite/composite.h"
36 #include "param/param.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                 uint_t n = (uint_t)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);
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 test how many open files this server supports on the one socket
236 */
237 bool run_maxfidtest(struct torture_context *tctx, struct smbcli_state *cli, int dummy)
238 {
239 #define MAXFID_TEMPLATE "\\maxfid\\fid%d\\maxfid.%d.%d"
240         char *fname;
241         int fnums[0x11000], i;
242         int retries=4, maxfid;
243         bool correct = true;
244
245         if (retries <= 0) {
246                 torture_comment(tctx, "failed to connect\n");
247                 return false;
248         }
249
250         if (smbcli_deltree(cli->tree, "\\maxfid") == -1) {
251                 torture_comment(tctx, "Failed to deltree \\maxfid - %s\n",
252                        smbcli_errstr(cli->tree));
253                 return false;
254         }
255         if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, "\\maxfid"))) {
256                 torture_comment(tctx, "Failed to mkdir \\maxfid, error=%s\n", 
257                        smbcli_errstr(cli->tree));
258                 return false;
259         }
260
261         torture_comment(tctx, "Testing maximum number of open files\n");
262
263         for (i=0; i<0x11000; i++) {
264                 if (i % 1000 == 0) {
265                         asprintf(&fname, "\\maxfid\\fid%d", i/1000);
266                         if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, fname))) {
267                                 torture_comment(tctx, "Failed to mkdir %s, error=%s\n", 
268                                        fname, smbcli_errstr(cli->tree));
269                                 return false;
270                         }
271                         free(fname);
272                 }
273                 asprintf(&fname, MAXFID_TEMPLATE, i/1000, i,(int)getpid());
274                 if ((fnums[i] = smbcli_open(cli->tree, fname, 
275                                         O_RDWR|O_CREAT|O_TRUNC, DENY_NONE)) ==
276                     -1) {
277                         torture_comment(tctx, "open of %s failed (%s)\n", 
278                                fname, smbcli_errstr(cli->tree));
279                         torture_comment(tctx, "maximum fnum is %d\n", i);
280                         break;
281                 }
282                 free(fname);
283                 if (torture_setting_bool(tctx, "progress", true)) {
284                         torture_comment(tctx, "%6d\r", i);
285                         fflush(stdout);
286                 }
287         }
288         torture_comment(tctx, "%6d\n", i);
289         i--;
290
291         maxfid = i;
292
293         torture_comment(tctx, "cleaning up\n");
294         for (i=0;i<maxfid/2;i++) {
295                 asprintf(&fname, MAXFID_TEMPLATE, i/1000, i,(int)getpid());
296                 if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnums[i]))) {
297                         torture_comment(tctx, "Close of fnum %d failed - %s\n", fnums[i], smbcli_errstr(cli->tree));
298                 }
299                 if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, fname))) {
300                         torture_comment(tctx, "unlink of %s failed (%s)\n", 
301                                fname, smbcli_errstr(cli->tree));
302                         correct = false;
303                 }
304                 free(fname);
305
306                 asprintf(&fname, MAXFID_TEMPLATE, (maxfid-i)/1000, maxfid-i,(int)getpid());
307                 if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnums[maxfid-i]))) {
308                         torture_comment(tctx, "Close of fnum %d failed - %s\n", fnums[maxfid-i], smbcli_errstr(cli->tree));
309                 }
310                 if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, fname))) {
311                         torture_comment(tctx, "unlink of %s failed (%s)\n", 
312                                fname, smbcli_errstr(cli->tree));
313                         correct = false;
314                 }
315                 free(fname);
316
317                 if (torture_setting_bool(tctx, "progress", true)) {
318                         torture_comment(tctx, "%6d %6d\r", i, maxfid-i);
319                         fflush(stdout);
320                 }
321         }
322         torture_comment(tctx, "%6d\n", 0);
323
324         if (smbcli_deltree(cli->tree, "\\maxfid") == -1) {
325                 torture_comment(tctx, "Failed to deltree \\maxfid - %s\n",
326                        smbcli_errstr(cli->tree));
327                 return false;
328         }
329
330         torture_comment(tctx, "maxfid test finished\n");
331         if (!torture_close_connection(cli)) {
332                 correct = false;
333         }
334         return correct;
335 #undef MAXFID_TEMPLATE
336 }
337
338
339
340 /*
341   sees what IOCTLs are supported
342  */
343 bool torture_ioctl_test(struct torture_context *tctx, 
344                                                 struct smbcli_state *cli)
345 {
346         uint16_t device, function;
347         int fnum;
348         const char *fname = "\\ioctl.dat";
349         NTSTATUS status;
350         union smb_ioctl parms;
351         TALLOC_CTX *mem_ctx;
352
353         mem_ctx = talloc_named_const(tctx, 0, "ioctl_test");
354
355         smbcli_unlink(cli->tree, fname);
356
357         fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
358         if (fnum == -1) {
359                 torture_comment(tctx, "open of %s failed (%s)\n", fname, smbcli_errstr(cli->tree));
360                 return false;
361         }
362
363         parms.ioctl.level = RAW_IOCTL_IOCTL;
364         parms.ioctl.in.file.fnum = fnum;
365         parms.ioctl.in.request = IOCTL_QUERY_JOB_INFO;
366         status = smb_raw_ioctl(cli->tree, mem_ctx, &parms);
367         torture_comment(tctx, "ioctl job info: %s\n", smbcli_errstr(cli->tree));
368
369         for (device=0;device<0x100;device++) {
370                 torture_comment(tctx, "testing device=0x%x\n", device);
371                 for (function=0;function<0x100;function++) {
372                         parms.ioctl.in.request = (device << 16) | function;
373                         status = smb_raw_ioctl(cli->tree, mem_ctx, &parms);
374
375                         if (NT_STATUS_IS_OK(status)) {
376                                 torture_comment(tctx, "ioctl device=0x%x function=0x%x OK : %d bytes\n", 
377                                         device, function, (int)parms.ioctl.out.blob.length);
378                         }
379                 }
380         }
381
382         return true;
383 }
384
385 static void benchrw_callback(struct smbcli_request *req);
386 enum benchrw_stage {
387         START,
388         OPEN_CONNECTION,
389         CLEANUP_TESTDIR,
390         MK_TESTDIR,
391         OPEN_FILE,
392         INITIAL_WRITE,
393         READ_WRITE_DATA,
394         MAX_OPS_REACHED,
395         ERROR,
396         CLOSE_FILE,
397         CLEANUP,
398         FINISHED
399 };
400
401 struct benchrw_state {
402         struct torture_context *tctx;
403         char *dname;
404         char *fname;
405         uint16_t fnum;
406         int nr;
407         struct smbcli_tree      *cli;           
408         uint8_t *buffer;
409         int writecnt;
410         int readcnt;
411         int completed;
412         int num_parallel_requests;
413         void *req_params;
414         enum benchrw_stage mode;
415         struct params{
416                 struct unclist{
417                         const char *host;
418                         const char *share;
419                 } **unc;
420                 const char *workgroup;
421                 int retry;
422                 unsigned int writeblocks;
423                 unsigned int blocksize;
424                 unsigned int writeratio;
425                 int num_parallel_requests;
426         } *lp_params;
427 };
428
429 /* 
430         init params using lp_parm_xxx 
431         return number of unclist entries
432 */
433 static int init_benchrw_params(struct torture_context *tctx,
434                                struct params *lpar)
435 {
436         char **unc_list = NULL;
437         int num_unc_names = 0, conn_index=0, empty_lines=0;
438         const char *p;
439         lpar->retry = torture_setting_int(tctx, "retry",3);
440         lpar->blocksize = torture_setting_int(tctx, "blocksize",65535);
441         lpar->writeblocks = torture_setting_int(tctx, "writeblocks",15);
442         lpar->writeratio = torture_setting_int(tctx, "writeratio",5);
443         lpar->num_parallel_requests = torture_setting_int(
444                 tctx, "parallel_requests", 5);
445         lpar->workgroup = lp_workgroup(tctx->lp_ctx);
446         
447         p = torture_setting_string(tctx, "unclist", NULL);
448         if (p) {
449                 char *h, *s;
450                 unc_list = file_lines_load(p, &num_unc_names, NULL);
451                 if (!unc_list || num_unc_names <= 0) {
452                         torture_comment(tctx, "Failed to load unc names list "
453                                         "from '%s'\n", p);
454                         exit(1);
455                 }
456                 
457                 lpar->unc = talloc_array(tctx, struct unclist *,
458                                          (num_unc_names-empty_lines));
459                 for(conn_index = 0; conn_index < num_unc_names; conn_index++) {
460                         /* ignore empty lines */
461                         if(strlen(unc_list[conn_index % num_unc_names])==0){
462                                 empty_lines++;
463                                 continue;
464                         }
465                         if (!smbcli_parse_unc(
466                                     unc_list[conn_index % num_unc_names],
467                                     NULL, &h, &s)) {
468                                 torture_comment(
469                                         tctx, "Failed to parse UNC "
470                                         "name %s\n",
471                                         unc_list[conn_index % num_unc_names]);
472                                 exit(1);
473                         }
474                         lpar->unc[conn_index-empty_lines] =
475                                 talloc(tctx, struct unclist);
476                         lpar->unc[conn_index-empty_lines]->host = h;
477                         lpar->unc[conn_index-empty_lines]->share = s;   
478                 }
479                 return num_unc_names-empty_lines;
480         }else{
481                 lpar->unc = talloc_array(tctx, struct unclist *, 1);
482                 lpar->unc[0] = talloc(tctx,struct unclist);
483                 lpar->unc[0]->host  = torture_setting_string(tctx, "host",
484                                                              NULL);
485                 lpar->unc[0]->share = torture_setting_string(tctx, "share",
486                                                              NULL);
487                 return 1;
488         }
489 }
490
491 /*
492  Called when the reads & writes are finished. closes the file.
493 */
494 static NTSTATUS benchrw_close(struct torture_context *tctx,
495                               struct smbcli_request *req,
496                               struct benchrw_state *state)
497 {
498         union smb_close close_parms;
499         
500         NT_STATUS_NOT_OK_RETURN(req->status);
501         
502         torture_comment(tctx, "Close file %d (%d)\n",state->nr,state->fnum);
503         close_parms.close.level = RAW_CLOSE_CLOSE;
504         close_parms.close.in.file.fnum = state->fnum ;
505         close_parms.close.in.write_time = 0;
506         state->mode=CLOSE_FILE;
507         
508         req = smb_raw_close_send(state->cli, &close_parms);
509         NT_STATUS_HAVE_NO_MEMORY(req);
510         /*register the callback function!*/
511         req->async.fn = benchrw_callback;
512         req->async.private = state;
513         
514         return NT_STATUS_OK;
515 }
516
517 static NTSTATUS benchrw_readwrite(struct torture_context *tctx,
518                                   struct benchrw_state *state);
519 static void benchrw_callback(struct smbcli_request *req);
520
521 static void benchrw_rw_callback(struct smbcli_request *req)
522 {
523         struct benchrw_state *state = req->async.private;
524         struct torture_context *tctx = state->tctx;
525
526         if (!NT_STATUS_IS_OK(req->status)) {
527                 state->mode = ERROR;
528                 return;
529         }
530
531         state->completed++;
532         state->num_parallel_requests--;
533
534         if ((state->completed >= torture_numops)
535             && (state->num_parallel_requests == 0)) {
536                 benchrw_callback(req);
537                 talloc_free(req);
538                 return;
539         }
540
541         talloc_free(req);
542
543         if (state->completed + state->num_parallel_requests
544             < torture_numops) {
545                 benchrw_readwrite(tctx, state);
546         }
547 }
548
549 /*
550  Called when the initial write is completed is done. write or read a file.
551 */
552 static NTSTATUS benchrw_readwrite(struct torture_context *tctx,
553                                   struct benchrw_state *state)
554 {
555         struct smbcli_request *req;
556         union smb_read  rd;
557         union smb_write wr;
558         
559         /* randomize between writes and reads*/
560         if (random() % state->lp_params->writeratio == 0) {
561                 torture_comment(tctx, "Callback WRITE file:%d (%d/%d)\n",
562                                 state->nr,state->completed,torture_numops);
563                 wr.generic.level = RAW_WRITE_WRITEX  ;
564                 wr.writex.in.file.fnum  = state->fnum ;
565                 wr.writex.in.offset     = 0;
566                 wr.writex.in.wmode      = 0             ;
567                 wr.writex.in.remaining  = 0;
568                 wr.writex.in.count      = state->lp_params->blocksize;
569                 wr.writex.in.data       = state->buffer;
570                 state->readcnt=0;
571                 req = smb_raw_write_send(state->cli,&wr);
572         }
573         else {
574                 torture_comment(tctx,
575                                 "Callback READ file:%d (%d/%d) Offset:%d\n",
576                                 state->nr,state->completed,torture_numops,
577                                 (state->readcnt*state->lp_params->blocksize));
578                 rd.generic.level = RAW_READ_READX;
579                 rd.readx.in.file.fnum   = state->fnum   ;
580                 rd.readx.in.offset      = state->readcnt*state->lp_params->blocksize; 
581                 rd.readx.in.mincnt      = state->lp_params->blocksize;
582                 rd.readx.in.maxcnt      = rd.readx.in.mincnt;
583                 rd.readx.in.remaining   = 0     ;
584                 rd.readx.out.data       = state->buffer;
585                 rd.readx.in.read_for_execute = false;
586                 if(state->readcnt < state->lp_params->writeblocks){
587                         state->readcnt++;       
588                 }else{
589                         /*start reading from beginn of file*/
590                         state->readcnt=0;
591                 }
592                 req = smb_raw_read_send(state->cli,&rd);
593         }
594         state->num_parallel_requests += 1;
595         NT_STATUS_HAVE_NO_MEMORY(req);
596         /*register the callback function!*/
597         req->async.fn = benchrw_rw_callback;
598         req->async.private = state;
599         
600         return NT_STATUS_OK;
601 }
602
603 /*
604  Called when the open is done. writes to the file.
605 */
606 static NTSTATUS benchrw_open(struct torture_context *tctx,
607                              struct smbcli_request *req,
608                              struct benchrw_state *state)
609 {
610         union smb_write wr;
611         if(state->mode == OPEN_FILE){
612                 NTSTATUS status;
613                 status = smb_raw_open_recv(req,tctx,(
614                                         union smb_open*)state->req_params);
615                 NT_STATUS_NOT_OK_RETURN(status);
616         
617                 state->fnum = ((union smb_open*)state->req_params)
618                                                 ->openx.out.file.fnum;
619                 torture_comment(tctx, "File opened (%d)\n",state->fnum);
620                 state->mode=INITIAL_WRITE;
621         }
622                 
623         torture_comment(tctx, "Write initial test file:%d (%d/%d)\n",state->nr,
624                 (state->writecnt+1)*state->lp_params->blocksize,
625                 (state->lp_params->writeblocks*state->lp_params->blocksize));
626         wr.generic.level = RAW_WRITE_WRITEX  ;
627         wr.writex.in.file.fnum  = state->fnum ;
628         wr.writex.in.offset     = state->writecnt * 
629                                         state->lp_params->blocksize;
630         wr.writex.in.wmode      = 0             ;
631         wr.writex.in.remaining  = (state->lp_params->writeblocks *
632                                                 state->lp_params->blocksize)-
633                                                 ((state->writecnt+1)*state->
634                                                 lp_params->blocksize);
635         wr.writex.in.count      = state->lp_params->blocksize;
636         wr.writex.in.data       = state->buffer;
637         state->writecnt++;
638         if(state->writecnt == state->lp_params->writeblocks){
639                 state->mode=READ_WRITE_DATA;
640         }
641         req = smb_raw_write_send(state->cli,&wr);
642         NT_STATUS_HAVE_NO_MEMORY(req);
643         
644         /*register the callback function!*/
645         req->async.fn = benchrw_callback;
646         req->async.private = state;
647         return NT_STATUS_OK;
648
649
650 /*
651  Called when the mkdir is done. Opens a file.
652 */
653 static NTSTATUS benchrw_mkdir(struct torture_context *tctx,
654                               struct smbcli_request *req,
655                               struct benchrw_state *state)
656 {
657         union smb_open *open_parms;     
658         uint8_t *writedata;     
659                 
660         NT_STATUS_NOT_OK_RETURN(req->status);
661         
662         /* open/create the files */
663         torture_comment(tctx, "Open File %d/%d\n",state->nr+1,
664                         torture_setting_int(tctx, "nprocs", 4));
665         open_parms=talloc_zero(tctx, union smb_open);
666         NT_STATUS_HAVE_NO_MEMORY(open_parms);
667         open_parms->openx.level = RAW_OPEN_OPENX;
668         open_parms->openx.in.flags = 0;
669         open_parms->openx.in.open_mode = OPENX_MODE_ACCESS_RDWR;
670         open_parms->openx.in.search_attrs = 
671                         FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
672         open_parms->openx.in.file_attrs = 0;
673         open_parms->openx.in.write_time = 0;
674         open_parms->openx.in.open_func = OPENX_OPEN_FUNC_CREATE;
675         open_parms->openx.in.size = 0;
676         open_parms->openx.in.timeout = 0;
677         open_parms->openx.in.fname = state->fname;
678                 
679         writedata = talloc_size(tctx,state->lp_params->blocksize);
680         NT_STATUS_HAVE_NO_MEMORY(writedata);
681         generate_random_buffer(writedata,state->lp_params->blocksize);
682         state->buffer=writedata;
683         state->writecnt=1;
684         state->readcnt=0;
685         state->req_params=open_parms;           
686         state->mode=OPEN_FILE;  
687                         
688         req = smb_raw_open_send(state->cli,open_parms);
689         NT_STATUS_HAVE_NO_MEMORY(req);
690         
691         /*register the callback function!*/
692         req->async.fn = benchrw_callback;
693         req->async.private = state;
694                 
695         return NT_STATUS_OK;
696 }
697
698 /*
699  handler for completion of a sub-request of the bench-rw test
700 */
701 static void benchrw_callback(struct smbcli_request *req)
702 {
703         struct benchrw_state *state = req->async.private;
704         struct torture_context *tctx = state->tctx;
705         
706         /*dont send new requests when torture_numops is reached*/
707         if ((state->mode == READ_WRITE_DATA)
708             && (state->completed >= torture_numops)) {
709                 state->mode=MAX_OPS_REACHED;
710         }
711         
712         switch (state->mode) {
713         
714         case MK_TESTDIR:
715                 if (!NT_STATUS_IS_OK(benchrw_mkdir(tctx, req,state))) {
716                         torture_comment(tctx, "Failed to create the test "
717                                         "directory - %s\n", 
718                                         nt_errstr(req->status));
719                         state->mode=ERROR;
720                         return;
721                 }
722                 break;  
723         case OPEN_FILE:
724         case INITIAL_WRITE:
725                 if (!NT_STATUS_IS_OK(benchrw_open(tctx, req,state))){
726                         torture_comment(tctx, "Failed to open/write the "
727                                         "file - %s\n", 
728                                         nt_errstr(req->status));
729                         state->mode=ERROR;
730                         state->readcnt=0;
731                         return;
732                 }
733                 break;
734         case READ_WRITE_DATA:
735                 while (state->num_parallel_requests
736                        < state->lp_params->num_parallel_requests) {
737                         NTSTATUS status;
738                         status = benchrw_readwrite(tctx,state);
739                         if (!NT_STATUS_IS_OK(status)){
740                                 torture_comment(tctx, "Failed to read/write "
741                                                 "the file - %s\n", 
742                                                 nt_errstr(req->status));
743                                 state->mode=ERROR;
744                                 return;
745                         }
746                 }
747                 break;
748         case MAX_OPS_REACHED:
749                 if (!NT_STATUS_IS_OK(benchrw_close(tctx,req,state))){
750                         torture_comment(tctx, "Failed to read/write/close "
751                                         "the file - %s\n", 
752                                         nt_errstr(req->status));
753                         state->mode=ERROR;
754                         return;
755                 }
756                 break;
757         case CLOSE_FILE:
758                 torture_comment(tctx, "File %d closed\n",state->nr);
759                 if (!NT_STATUS_IS_OK(req->status)) {
760                         torture_comment(tctx, "Failed to close the "
761                                         "file - %s\n",
762                                         nt_errstr(req->status));
763                         state->mode=ERROR;
764                         return;
765                 }
766                 state->mode=CLEANUP;
767                 return; 
768         default:
769                 break;
770         }
771         
772 }
773
774 /* open connection async callback function*/
775 static void async_open_callback(struct composite_context *con)
776 {
777         struct benchrw_state *state = con->async.private_data;
778         struct torture_context *tctx = state->tctx;
779         int retry = state->lp_params->retry;
780                 
781         if (NT_STATUS_IS_OK(con->status)) {
782                 state->cli=((struct smb_composite_connect*)
783                                         state->req_params)->out.tree;
784                 state->mode=CLEANUP_TESTDIR;
785         }else{
786                 if(state->writecnt < retry){
787                         torture_comment(tctx, "Failed to open connection: "
788                                         "%d, Retry (%d/%d)\n",
789                                         state->nr,state->writecnt,retry);
790                         state->writecnt++;
791                         state->mode=START;
792                         usleep(1000);   
793                 }else{
794                         torture_comment(tctx, "Failed to open connection "
795                                         "(%d) - %s\n",
796                                         state->nr, nt_errstr(con->status));
797                         state->mode=ERROR;
798                 }
799                 return;
800         }       
801 }
802
803 /*
804  establishs a smbcli_tree from scratch (async)
805 */
806 static struct composite_context *torture_connect_async(
807                                 struct torture_context *tctx,
808                                 struct smb_composite_connect *smb,
809                                 TALLOC_CTX *mem_ctx,
810                                 struct event_context *ev,
811                                 const char *host,
812                                 const char *share,
813                                 const char *workgroup)
814 {
815         torture_comment(tctx, "Open Connection to %s/%s\n",host,share);
816         smb->in.dest_host=talloc_strdup(mem_ctx,host);
817         smb->in.service=talloc_strdup(mem_ctx,share);
818         smb->in.dest_ports=lp_smb_ports(tctx->lp_ctx);
819         smb->in.called_name = strupper_talloc(mem_ctx, host);
820         smb->in.service_type=NULL;
821         smb->in.credentials=cmdline_credentials;
822         smb->in.fallback_to_anonymous=false;
823         smb->in.workgroup=workgroup;
824         lp_smbcli_options(tctx->lp_ctx, &smb->in.options);
825         
826         return smb_composite_connect_send(smb,mem_ctx,
827                                           lp_resolve_context(tctx->lp_ctx),ev);
828 }
829
830 bool run_benchrw(struct torture_context *tctx)
831 {
832         struct smb_composite_connect *smb_con;
833         const char *fname = "\\rwtest.dat";
834         struct smbcli_request *req;
835         struct benchrw_state **state;
836         int i , num_unc_names;
837         struct event_context    *ev     ;       
838         struct composite_context *req1;
839         struct params lpparams;
840         union smb_mkdir parms;
841         int finished = 0;
842         bool success=true;
843         int torture_nprocs = torture_setting_int(tctx, "nprocs", 4);
844         
845         torture_comment(tctx, "Start BENCH-READWRITE num_ops=%d "
846                         "num_nprocs=%d\n",
847                         torture_numops, torture_nprocs);
848
849         /*init talloc context*/
850         ev = tctx->ev;
851         state = talloc_array(tctx, struct benchrw_state *, torture_nprocs);
852
853         /* init params using lp_parm_xxx */
854         num_unc_names = init_benchrw_params(tctx,&lpparams);
855         
856         /* init private data structs*/
857         for(i = 0; i<torture_nprocs;i++){
858                 state[i]=talloc(tctx,struct benchrw_state);
859                 state[i]->tctx = tctx;
860                 state[i]->completed=0;
861                 state[i]->num_parallel_requests=0;
862                 state[i]->lp_params=&lpparams;
863                 state[i]->nr=i;
864                 state[i]->dname=talloc_asprintf(tctx,"benchrw%d",i);
865                 state[i]->fname=talloc_asprintf(tctx,"%s%s",
866                                                 state[i]->dname,fname); 
867                 state[i]->mode=START;
868                 state[i]->writecnt=0;
869         }
870         
871         torture_comment(tctx, "Starting async requests\n");     
872         while(finished != torture_nprocs){
873                 finished=0;
874                 for(i = 0; i<torture_nprocs;i++){
875                         switch (state[i]->mode){
876                         /*open multiple connections with the same userid */
877                         case START:
878                                 smb_con = talloc(
879                                         tctx,struct smb_composite_connect) ;
880                                 state[i]->req_params=smb_con; 
881                                 state[i]->mode=OPEN_CONNECTION;
882                                 req1 = torture_connect_async(
883                                         tctx, smb_con, tctx,ev,
884                                         lpparams.unc[i % num_unc_names]->host,
885                                         lpparams.unc[i % num_unc_names]->share,
886                                         lpparams.workgroup);
887                                 /* register callback fn + private data */
888                                 req1->async.fn = async_open_callback;
889                                 req1->async.private_data=state[i];
890                                 break;
891                         /*setup test dirs (sync)*/
892                         case CLEANUP_TESTDIR:
893                                 torture_comment(tctx, "Setup test dir %d\n",i);
894                                 smb_raw_exit(state[i]->cli->session);
895                                 if (smbcli_deltree(state[i]->cli, 
896                                                 state[i]->dname) == -1) {
897                                         torture_comment(
898                                                 tctx,
899                                                 "Unable to delete %s - %s\n", 
900                                                 state[i]->dname,
901                                                 smbcli_errstr(state[i]->cli));
902                                         state[i]->mode=ERROR;
903                                         break;
904                                 }
905                                 state[i]->mode=MK_TESTDIR;
906                                 parms.mkdir.level = RAW_MKDIR_MKDIR;
907                                 parms.mkdir.in.path = state[i]->dname;
908                                 req = smb_raw_mkdir_send(state[i]->cli,&parms);
909                                 /* register callback fn + private data */
910                                 req->async.fn = benchrw_callback;
911                                 req->async.private=state[i];
912                                 break;
913                         /* error occured , finish */
914                         case ERROR:
915                                 finished++;
916                                 success=false;
917                                 break;
918                         /* cleanup , close connection */
919                         case CLEANUP:
920                                 torture_comment(tctx, "Deleting test dir %s "
921                                                 "%d/%d\n",state[i]->dname,
922                                                 i+1,torture_nprocs);
923                                 smbcli_deltree(state[i]->cli,state[i]->dname);
924                                 if (NT_STATUS_IS_ERR(smb_tree_disconnect(
925                                                              state[i]->cli))) {
926                                         torture_comment(tctx, "ERROR: Tree "
927                                                         "disconnect failed");
928                                         state[i]->mode=ERROR;
929                                         break;
930                                 }
931                                 state[i]->mode=FINISHED;
932                         case FINISHED:
933                                 finished++;
934                                 break;
935                         default:
936                                 event_loop_once(ev);
937                         }
938                 }
939         }
940                                 
941         return success; 
942 }
943