r14173: change smb interface structures to always use
[jelmer/samba4-debian.git] / source / torture / nbench / nbio.c
1 #define NBDEBUG 0
2
3 /* 
4    Unix SMB/CIFS implementation.
5    SMB torture tester
6    Copyright (C) Andrew Tridgell 1997-1998
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "system/time.h"
25 #include "system/filesys.h"
26 #include "dlinklist.h"
27 #include "libcli/libcli.h"
28 #include "libcli/raw/libcliraw.h"
29 #include "torture/torture.h"
30
31 #define MAX_FILES 100
32
33 extern int nbench_line_count;
34 static int nbio_id = -1;
35 static int nprocs;
36 static BOOL bypass_io;
37 static struct timeval tv_start, tv_end;
38 static int warmup, timelimit;
39 static int in_cleanup;
40
41 struct ftable {
42         struct ftable *next, *prev;
43         int fd;     /* the fd that we got back from the server */
44         int handle; /* the handle in the load file */
45 };
46
47 static struct ftable *ftable;
48
49 static struct {
50         double bytes, warmup_bytes;
51         int line;
52         int done;
53 } *children;
54
55 double nbio_result(void)
56 {
57         int i;
58         double total = 0;
59         for (i=0;i<nprocs;i++) {
60                 total += children[i].bytes - children[i].warmup_bytes;
61         }
62         return 1.0e-6 * total / timeval_elapsed2(&tv_start, &tv_end);
63 }
64
65 BOOL nb_tick(void)
66 {
67         return children[nbio_id].done;
68 }
69
70
71 void nb_alarm(int sig)
72 {
73         int i;
74         int lines=0;
75         double t;
76         int in_warmup = 0;
77
78         if (nbio_id != -1) return;
79
80         for (i=0;i<nprocs;i++) {
81                 if (children[i].bytes == 0) {
82                         in_warmup = 1;
83                 }
84                 lines += children[i].line;
85         }
86
87         t = timeval_elapsed(&tv_start);
88
89         if (!in_warmup && warmup>0 && t > warmup) {
90                 tv_start = timeval_current();
91                 warmup = 0;
92                 for (i=0;i<nprocs;i++) {
93                         children[i].warmup_bytes = children[i].bytes;
94                 }
95                 goto next;
96         }
97         if (t < warmup) {
98                 in_warmup = 1;
99         } else if (!in_warmup && !in_cleanup && t > timelimit) {
100                 for (i=0;i<nprocs;i++) {
101                         children[i].done = 1;
102                 }
103                 tv_end = timeval_current();
104                 in_cleanup = 1;
105         }
106         if (t < 1) {
107                 goto next;
108         }
109         if (!in_cleanup) {
110                 tv_end = timeval_current();
111         }
112
113         if (in_warmup) {
114                 printf("%4d  %8d  %.2f MB/sec  warmup %.0f sec   \n", 
115                        nprocs, lines/nprocs, 
116                        nbio_result(), t);
117         } else if (in_cleanup) {
118                 printf("%4d  %8d  %.2f MB/sec  cleanup %.0f sec   \n", 
119                        nprocs, lines/nprocs, 
120                        nbio_result(), t);
121         } else {
122                 printf("%4d  %8d  %.2f MB/sec  execute %.0f sec   \n", 
123                        nprocs, lines/nprocs, 
124                        nbio_result(), t);
125         }
126
127         fflush(stdout);
128 next:
129         signal(SIGALRM, nb_alarm);
130         alarm(1);       
131 }
132
133 void nbio_shmem(int n, int t_timelimit, int t_warmup)
134 {
135         nprocs = n;
136         children = shm_setup(sizeof(*children) * nprocs);
137         if (!children) {
138                 printf("Failed to setup shared memory!\n");
139                 exit(1);
140         }
141         memset(children, 0, sizeof(*children) * nprocs);
142         timelimit = t_timelimit;
143         warmup = t_warmup;
144         in_cleanup = 0;
145         tv_start = timeval_current();
146 }
147
148 static struct ftable *find_ftable(int handle)
149 {
150         struct ftable *f;
151
152         for (f=ftable;f;f=f->next) {
153                 if (f->handle == handle) return f;
154         }
155         return NULL;
156 }
157
158 static int find_handle(int handle)
159 {
160         struct ftable *f;
161
162         children[nbio_id].line = nbench_line_count;
163
164         f = find_ftable(handle);
165         if (f) {
166                 return f->fd;
167         }
168         printf("(%d) ERROR: handle %d was not found\n", 
169                nbench_line_count, handle);
170         exit(1);
171
172         return -1;              /* Not reached */
173 }
174
175
176
177 static struct smbcli_state *c;
178
179 /*
180   a handler function for oplock break requests
181 */
182 static BOOL oplock_handler(struct smbcli_transport *transport, uint16_t tid, uint16_t fnum, uint8_t level, void *private)
183 {
184         struct smbcli_tree *tree = private;
185         return smbcli_oplock_ack(tree, fnum, level);
186 }
187
188 void nb_setup(struct smbcli_state *cli, int id)
189 {
190         nbio_id = id;
191         c = cli;
192         if (bypass_io)
193                 printf("skipping I/O\n");
194
195         if (cli) {
196                 smbcli_oplock_handler(cli->transport, oplock_handler, cli->tree);
197         }
198 }
199
200
201 static void check_status(const char *op, NTSTATUS status, NTSTATUS ret)
202 {
203         if (!NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(ret)) {
204                 printf("[%d] Error: %s should have failed with %s\n", 
205                        nbench_line_count, op, nt_errstr(status));
206                 exit(1);
207         }
208
209         if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(ret)) {
210                 printf("[%d] Error: %s should have succeeded - %s\n", 
211                        nbench_line_count, op, nt_errstr(ret));
212                 exit(1);
213         }
214
215         if (!NT_STATUS_EQUAL(status, ret)) {
216                 printf("[%d] Warning: got status %s but expected %s\n",
217                        nbench_line_count, nt_errstr(ret), nt_errstr(status));
218         }
219 }
220
221
222 void nb_unlink(const char *fname, int attr, NTSTATUS status)
223 {
224         union smb_unlink io;
225         NTSTATUS ret;
226
227         io.unlink.in.pattern = fname;
228
229         io.unlink.in.attrib = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
230         if (strchr(fname, '*') == 0) {
231                 io.unlink.in.attrib |= FILE_ATTRIBUTE_DIRECTORY;
232         }
233
234         ret = smb_raw_unlink(c->tree, &io);
235
236         check_status("Unlink", status, ret);
237 }
238
239
240 void nb_createx(const char *fname, 
241                 uint_t create_options, uint_t create_disposition, int handle,
242                 NTSTATUS status)
243 {
244         union smb_open io;      
245         uint32_t desired_access;
246         NTSTATUS ret;
247         TALLOC_CTX *mem_ctx;
248         uint_t flags = 0;
249         struct ftable *f;
250
251         mem_ctx = talloc_init("raw_open");
252
253         if (create_options & NTCREATEX_OPTIONS_DIRECTORY) {
254                 desired_access = SEC_FILE_READ_DATA;
255         } else {
256                 desired_access = 
257                         SEC_FILE_READ_DATA | 
258                         SEC_FILE_WRITE_DATA |
259                         SEC_FILE_READ_ATTRIBUTE |
260                         SEC_FILE_WRITE_ATTRIBUTE;
261                 flags = NTCREATEX_FLAGS_EXTENDED |
262                         NTCREATEX_FLAGS_REQUEST_OPLOCK | 
263                         NTCREATEX_FLAGS_REQUEST_BATCH_OPLOCK;
264         }
265
266         io.ntcreatex.level = RAW_OPEN_NTCREATEX;
267         io.ntcreatex.in.flags = flags;
268         io.ntcreatex.in.root_fid = 0;
269         io.ntcreatex.in.access_mask = desired_access;
270         io.ntcreatex.in.file_attr = 0;
271         io.ntcreatex.in.alloc_size = 0;
272         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE;
273         io.ntcreatex.in.open_disposition = create_disposition;
274         io.ntcreatex.in.create_options = create_options;
275         io.ntcreatex.in.impersonation = 0;
276         io.ntcreatex.in.security_flags = 0;
277         io.ntcreatex.in.fname = fname;
278
279         ret = smb_raw_open(c->tree, mem_ctx, &io);
280
281         talloc_free(mem_ctx);
282
283         check_status("NTCreateX", status, ret);
284
285         if (!NT_STATUS_IS_OK(ret)) return;
286
287         f = malloc_p(struct ftable);
288         f->handle = handle;
289         f->fd = io.ntcreatex.file.fnum;
290
291         DLIST_ADD_END(ftable, f, struct ftable *);
292 }
293
294 void nb_writex(int handle, int offset, int size, int ret_size, NTSTATUS status)
295 {
296         union smb_write io;
297         int i;
298         NTSTATUS ret;
299         uint8_t *buf;
300
301         i = find_handle(handle);
302
303         if (bypass_io) return;
304
305         buf = malloc(size);
306         memset(buf, 0xab, size);
307
308         io.writex.level = RAW_WRITE_WRITEX;
309         io.writex.file.fnum = i;
310         io.writex.in.wmode = 0;
311         io.writex.in.remaining = 0;
312         io.writex.in.offset = offset;
313         io.writex.in.count = size;
314         io.writex.in.data = buf;
315
316         ret = smb_raw_write(c->tree, &io);
317
318         free(buf);
319
320         check_status("WriteX", status, ret);
321
322         if (NT_STATUS_IS_OK(ret) && io.writex.out.nwritten != ret_size) {
323                 printf("[%d] Warning: WriteX got count %d expected %d\n", 
324                        nbench_line_count,
325                        io.writex.out.nwritten, ret_size);
326         }       
327
328         children[nbio_id].bytes += ret_size;
329 }
330
331 void nb_write(int handle, int offset, int size, int ret_size, NTSTATUS status)
332 {
333         union smb_write io;
334         int i;
335         NTSTATUS ret;
336         uint8_t *buf;
337
338         i = find_handle(handle);
339
340         if (bypass_io) return;
341
342         buf = malloc(size);
343
344         memset(buf, 0x12, size);
345
346         io.write.level = RAW_WRITE_WRITE;
347         io.write.file.fnum = i;
348         io.write.in.remaining = 0;
349         io.write.in.offset = offset;
350         io.write.in.count = size;
351         io.write.in.data = buf;
352
353         ret = smb_raw_write(c->tree, &io);
354
355         free(buf);
356
357         check_status("Write", status, ret);
358
359         if (NT_STATUS_IS_OK(ret) && io.write.out.nwritten != ret_size) {
360                 printf("[%d] Warning: Write got count %d expected %d\n", 
361                        nbench_line_count,
362                        io.write.out.nwritten, ret_size);
363         }       
364
365         children[nbio_id].bytes += ret_size;
366 }
367
368
369 void nb_lockx(int handle, uint_t offset, int size, NTSTATUS status)
370 {
371         union smb_lock io;
372         int i;
373         NTSTATUS ret;
374         struct smb_lock_entry lck;
375
376         i = find_handle(handle);
377
378         lck.pid = getpid();
379         lck.offset = offset;
380         lck.count = size;
381
382         io.lockx.level = RAW_LOCK_LOCKX;
383         io.lockx.file.fnum = i;
384         io.lockx.in.mode = 0;
385         io.lockx.in.timeout = 0;
386         io.lockx.in.ulock_cnt = 0;
387         io.lockx.in.lock_cnt = 1;
388         io.lockx.in.locks = &lck;
389
390         ret = smb_raw_lock(c->tree, &io);
391
392         check_status("LockX", status, ret);
393 }
394
395 void nb_unlockx(int handle, uint_t offset, int size, NTSTATUS status)
396 {
397         union smb_lock io;
398         int i;
399         NTSTATUS ret;
400         struct smb_lock_entry lck;
401
402         i = find_handle(handle);
403
404         lck.pid = getpid();
405         lck.offset = offset;
406         lck.count = size;
407
408         io.lockx.level = RAW_LOCK_LOCKX;
409         io.lockx.file.fnum = i;
410         io.lockx.in.mode = 0;
411         io.lockx.in.timeout = 0;
412         io.lockx.in.ulock_cnt = 1;
413         io.lockx.in.lock_cnt = 0;
414         io.lockx.in.locks = &lck;
415
416         ret = smb_raw_lock(c->tree, &io);
417
418         check_status("UnlockX", status, ret);
419 }
420
421 void nb_readx(int handle, int offset, int size, int ret_size, NTSTATUS status)
422 {
423         union smb_read io;
424         int i;
425         NTSTATUS ret;
426         uint8_t *buf;
427
428         i = find_handle(handle);
429
430         if (bypass_io) return;
431
432         buf = malloc(size);
433
434         io.readx.level = RAW_READ_READX;
435         io.readx.file.fnum = i;
436         io.readx.in.offset    = offset;
437         io.readx.in.mincnt    = size;
438         io.readx.in.maxcnt    = size;
439         io.readx.in.remaining = 0;
440         io.readx.out.data     = buf;
441                 
442         ret = smb_raw_read(c->tree, &io);
443
444         free(buf);
445
446         check_status("ReadX", status, ret);
447
448         if (NT_STATUS_IS_OK(ret) && io.readx.out.nread != ret_size) {
449                 printf("[%d] ERROR: ReadX got count %d expected %d\n", 
450                        nbench_line_count,
451                        io.readx.out.nread, ret_size);
452                 exit(1);
453         }       
454
455         children[nbio_id].bytes += ret_size;
456 }
457
458 void nb_close(int handle, NTSTATUS status)
459 {
460         NTSTATUS ret;
461         union smb_close io;
462         int i;
463
464         i = find_handle(handle);
465
466         io.close.level = RAW_CLOSE_CLOSE;
467         io.close.file.fnum = i;
468         io.close.in.write_time = 0;
469
470         ret = smb_raw_close(c->tree, &io);
471
472         check_status("Close", status, ret);
473
474         if (NT_STATUS_IS_OK(ret)) {
475                 struct ftable *f = find_ftable(handle);
476                 DLIST_REMOVE(ftable, f);
477                 free(f);
478         }
479 }
480
481 void nb_rmdir(const char *dname, NTSTATUS status)
482 {
483         NTSTATUS ret;
484         struct smb_rmdir io;
485
486         io.in.path = dname;
487
488         ret = smb_raw_rmdir(c->tree, &io);
489
490         check_status("Rmdir", status, ret);
491 }
492
493 void nb_mkdir(const char *dname, NTSTATUS status)
494 {
495         union smb_mkdir io;
496
497         io.mkdir.level = RAW_MKDIR_MKDIR;
498         io.mkdir.in.path = dname;
499
500         /* NOTE! no error checking. Used for base fileset creation */
501         smb_raw_mkdir(c->tree, &io);
502 }
503
504 void nb_rename(const char *old, const char *new, NTSTATUS status)
505 {
506         NTSTATUS ret;
507         union smb_rename io;
508
509         io.generic.level = RAW_RENAME_RENAME;
510         io.rename.in.attrib = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY;
511         io.rename.in.pattern1 = old;
512         io.rename.in.pattern2 = new;
513
514         ret = smb_raw_rename(c->tree, &io);
515
516         check_status("Rename", status, ret);
517 }
518
519
520 void nb_qpathinfo(const char *fname, int level, NTSTATUS status)
521 {
522         union smb_fileinfo io;
523         TALLOC_CTX *mem_ctx;
524         NTSTATUS ret;
525
526         mem_ctx = talloc_init("nb_qpathinfo");
527
528         io.generic.level = level;
529         io.generic.file.path = fname;
530
531         ret = smb_raw_pathinfo(c->tree, mem_ctx, &io);
532
533         talloc_free(mem_ctx);
534
535         check_status("Pathinfo", status, ret);
536 }
537
538
539 void nb_qfileinfo(int fnum, int level, NTSTATUS status)
540 {
541         union smb_fileinfo io;
542         TALLOC_CTX *mem_ctx;
543         NTSTATUS ret;
544         int i;
545
546         i = find_handle(fnum);
547
548         mem_ctx = talloc_init("nb_qfileinfo");
549
550         io.generic.level = level;
551         io.generic.file.fnum = i;
552
553         ret = smb_raw_fileinfo(c->tree, mem_ctx, &io);
554
555         talloc_free(mem_ctx);
556
557         check_status("Fileinfo", status, ret);
558 }
559
560 void nb_sfileinfo(int fnum, int level, NTSTATUS status)
561 {
562         union smb_setfileinfo io;
563         NTSTATUS ret;
564         int i;
565
566         if (level != RAW_SFILEINFO_BASIC_INFORMATION) {
567                 printf("[%d] Warning: setfileinfo level %d not handled\n", nbench_line_count, level);
568                 return;
569         }
570
571         ZERO_STRUCT(io);
572
573         i = find_handle(fnum);
574
575         io.generic.level = level;
576         io.generic.file.fnum = i;
577         unix_to_nt_time(&io.basic_info.in.create_time, time(NULL));
578         unix_to_nt_time(&io.basic_info.in.access_time, 0);
579         unix_to_nt_time(&io.basic_info.in.write_time, 0);
580         unix_to_nt_time(&io.basic_info.in.change_time, 0);
581         io.basic_info.in.attrib = 0;
582
583         ret = smb_raw_setfileinfo(c->tree, &io);
584
585         check_status("Setfileinfo", status, ret);
586 }
587
588 void nb_qfsinfo(int level, NTSTATUS status)
589 {
590         union smb_fsinfo io;
591         TALLOC_CTX *mem_ctx;
592         NTSTATUS ret;
593
594         mem_ctx = talloc_init("smbcli_dskattr");
595
596         io.generic.level = level;
597         ret = smb_raw_fsinfo(c->tree, mem_ctx, &io);
598
599         talloc_free(mem_ctx);
600         
601         check_status("Fsinfo", status, ret);    
602 }
603
604 /* callback function used for trans2 search */
605 static BOOL findfirst_callback(void *private, union smb_search_data *file)
606 {
607         return True;
608 }
609
610 void nb_findfirst(const char *mask, int level, int maxcnt, int count, NTSTATUS status)
611 {
612         union smb_search_first io;
613         TALLOC_CTX *mem_ctx;
614         NTSTATUS ret;
615
616         mem_ctx = talloc_init("smbcli_dskattr");
617
618         io.t2ffirst.level = level;
619         io.t2ffirst.in.max_count = maxcnt;
620         io.t2ffirst.in.search_attrib = FILE_ATTRIBUTE_DIRECTORY;
621         io.t2ffirst.in.pattern = mask;
622         io.t2ffirst.in.flags = FLAG_TRANS2_FIND_CLOSE;
623         io.t2ffirst.in.storage_type = 0;
624                         
625         ret = smb_raw_search_first(c->tree, mem_ctx, &io, NULL, findfirst_callback);
626
627         talloc_free(mem_ctx);
628
629         check_status("Search", status, ret);
630
631         if (NT_STATUS_IS_OK(ret) && io.t2ffirst.out.count != count) {
632                 printf("[%d] Warning: got count %d expected %d\n", 
633                        nbench_line_count,
634                        io.t2ffirst.out.count, count);
635         }
636 }
637
638 void nb_flush(int fnum, NTSTATUS status)
639 {
640         union smb_flush io;
641         NTSTATUS ret;
642         int i;
643         i = find_handle(fnum);
644
645         io.flush.file.fnum = i;
646
647         ret = smb_raw_flush(c->tree, &io);
648
649         check_status("Flush", status, ret);
650 }
651
652 void nb_sleep(int usec, NTSTATUS status)
653 {
654         usleep(usec);
655 }
656
657 void nb_deltree(const char *dname)
658 {
659         int total_deleted;
660
661         smb_raw_exit(c->session);
662
663         while (ftable) {
664                 struct ftable *f = ftable;
665                 DLIST_REMOVE(ftable, f);
666                 free(f);
667         }
668
669         total_deleted = smbcli_deltree(c->tree, dname);
670
671         if (total_deleted == -1) {
672                 printf("Failed to cleanup tree %s - exiting\n", dname);
673                 exit(1);
674         }
675
676         smbcli_rmdir(c->tree, dname);
677 }
678