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