r17930: Merge noinclude branch:
[garming/samba-autobuild/.git] / source4 / torture / nbench / nbio.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SMB torture tester
4    Copyright (C) Andrew Tridgell 1997-1998
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22 #include "system/time.h"
23 #include "system/filesys.h"
24 #include "lib/util/dlinklist.h"
25 #include "libcli/libcli.h"
26 #include "libcli/raw/libcliraw.h"
27 #include "torture/torture.h"
28 #include "libcli/libcli.h"
29 #include "torture/util.h"
30
31 extern int nbench_line_count;
32 static int nbio_id = -1;
33 static int nprocs;
34 static BOOL bypass_io;
35 static struct timeval tv_start, tv_end;
36 static int warmup, timelimit;
37 static int in_cleanup;
38
39 struct ftable {
40         struct ftable *next, *prev;
41         int fd;     /* the fd that we got back from the server */
42         int handle; /* the handle in the load file */
43 };
44
45 static struct ftable *ftable;
46
47 static struct {
48         double bytes, warmup_bytes;
49         int line;
50         int done;
51 } *children;
52
53 double nbio_result(void)
54 {
55         int i;
56         double total = 0;
57         for (i=0;i<nprocs;i++) {
58                 total += children[i].bytes - children[i].warmup_bytes;
59         }
60         return 1.0e-6 * total / timeval_elapsed2(&tv_start, &tv_end);
61 }
62
63 BOOL nb_tick(void)
64 {
65         return children[nbio_id].done;
66 }
67
68
69 void nb_alarm(int sig)
70 {
71         int i;
72         int lines=0;
73         double t;
74         int in_warmup = 0;
75
76         if (nbio_id != -1) return;
77
78         for (i=0;i<nprocs;i++) {
79                 if (children[i].bytes == 0) {
80                         in_warmup = 1;
81                 }
82                 lines += children[i].line;
83         }
84
85         t = timeval_elapsed(&tv_start);
86
87         if (!in_warmup && warmup>0 && t > warmup) {
88                 tv_start = timeval_current();
89                 warmup = 0;
90                 for (i=0;i<nprocs;i++) {
91                         children[i].warmup_bytes = children[i].bytes;
92                 }
93                 goto next;
94         }
95         if (t < warmup) {
96                 in_warmup = 1;
97         } else if (!in_warmup && !in_cleanup && t > timelimit) {
98                 for (i=0;i<nprocs;i++) {
99                         children[i].done = 1;
100                 }
101                 tv_end = timeval_current();
102                 in_cleanup = 1;
103         }
104         if (t < 1) {
105                 goto next;
106         }
107         if (!in_cleanup) {
108                 tv_end = timeval_current();
109         }
110
111         if (in_warmup) {
112                 printf("%4d  %8d  %.2f MB/sec  warmup %.0f sec   \n", 
113                        nprocs, lines/nprocs, 
114                        nbio_result(), t);
115         } else if (in_cleanup) {
116                 printf("%4d  %8d  %.2f MB/sec  cleanup %.0f sec   \n", 
117                        nprocs, lines/nprocs, 
118                        nbio_result(), t);
119         } else {
120                 printf("%4d  %8d  %.2f MB/sec  execute %.0f sec   \n", 
121                        nprocs, lines/nprocs, 
122                        nbio_result(), t);
123         }
124
125         fflush(stdout);
126 next:
127         signal(SIGALRM, nb_alarm);
128         alarm(1);       
129 }
130
131 void nbio_shmem(int n, int t_timelimit, int t_warmup)
132 {
133         nprocs = n;
134         children = shm_setup(sizeof(*children) * nprocs);
135         if (!children) {
136                 printf("Failed to setup shared memory!\n");
137                 exit(1);
138         }
139         memset(children, 0, sizeof(*children) * nprocs);
140         timelimit = t_timelimit;
141         warmup = t_warmup;
142         in_cleanup = 0;
143         tv_start = timeval_current();
144 }
145
146 static struct ftable *find_ftable(int handle)
147 {
148         struct ftable *f;
149
150         for (f=ftable;f;f=f->next) {
151                 if (f->handle == handle) return f;
152         }
153         return NULL;
154 }
155
156 static int find_handle(int handle)
157 {
158         struct ftable *f;
159
160         children[nbio_id].line = nbench_line_count;
161
162         f = find_ftable(handle);
163         if (f) {
164                 return f->fd;
165         }
166         printf("(%d) ERROR: handle %d was not found\n", 
167                nbench_line_count, handle);
168         exit(1);
169
170         return -1;              /* Not reached */
171 }
172
173
174
175 static struct smbcli_state *c;
176
177 /*
178   a handler function for oplock break requests
179 */
180 static BOOL oplock_handler(struct smbcli_transport *transport, uint16_t tid, 
181                            uint16_t fnum, uint8_t level, void *private)
182 {
183         struct smbcli_tree *tree = private;
184         return smbcli_oplock_ack(tree, fnum, OPLOCK_BREAK_TO_NONE);
185 }
186
187 void nb_setup(struct smbcli_state *cli, int id)
188 {
189         nbio_id = id;
190         c = cli;
191         if (bypass_io)
192                 printf("skipping I/O\n");
193
194         if (cli) {
195                 smbcli_oplock_handler(cli->transport, oplock_handler, cli->tree);
196         }
197 }
198
199
200 static void check_status(const char *op, NTSTATUS status, NTSTATUS ret)
201 {
202         if (!NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(ret)) {
203                 printf("[%d] Error: %s should have failed with %s\n", 
204                        nbench_line_count, op, nt_errstr(status));
205                 exit(1);
206         }
207
208         if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(ret)) {
209                 printf("[%d] Error: %s should have succeeded - %s\n", 
210                        nbench_line_count, op, nt_errstr(ret));
211                 exit(1);
212         }
213
214         if (!NT_STATUS_EQUAL(status, ret)) {
215                 printf("[%d] Warning: got status %s but expected %s\n",
216                        nbench_line_count, nt_errstr(ret), nt_errstr(status));
217         }
218 }
219
220
221 void nb_unlink(const char *fname, int attr, NTSTATUS status)
222 {
223         union smb_unlink io;
224         NTSTATUS ret;
225
226         io.unlink.in.pattern = fname;
227
228         io.unlink.in.attrib = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
229         if (strchr(fname, '*') == 0) {
230                 io.unlink.in.attrib |= FILE_ATTRIBUTE_DIRECTORY;
231         }
232
233         ret = smb_raw_unlink(c->tree, &io);
234
235         check_status("Unlink", status, ret);
236 }
237
238
239 void nb_createx(const char *fname, 
240                 uint_t create_options, uint_t create_disposition, int handle,
241                 NTSTATUS status)
242 {
243         union smb_open io;      
244         uint32_t desired_access;
245         NTSTATUS ret;
246         TALLOC_CTX *mem_ctx;
247         uint_t flags = 0;
248         struct ftable *f;
249
250         mem_ctx = talloc_init("raw_open");
251
252         if (create_options & NTCREATEX_OPTIONS_DIRECTORY) {
253                 desired_access = SEC_FILE_READ_DATA;
254         } else {
255                 desired_access = 
256                         SEC_FILE_READ_DATA | 
257                         SEC_FILE_WRITE_DATA |
258                         SEC_FILE_READ_ATTRIBUTE |
259                         SEC_FILE_WRITE_ATTRIBUTE;
260                 flags = NTCREATEX_FLAGS_EXTENDED |
261                         NTCREATEX_FLAGS_REQUEST_OPLOCK | 
262                         NTCREATEX_FLAGS_REQUEST_BATCH_OPLOCK;
263         }
264
265         io.ntcreatex.level = RAW_OPEN_NTCREATEX;
266         io.ntcreatex.in.flags = flags;
267         io.ntcreatex.in.root_fid = 0;
268         io.ntcreatex.in.access_mask = desired_access;
269         io.ntcreatex.in.file_attr = 0;
270         io.ntcreatex.in.alloc_size = 0;
271         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE;
272         io.ntcreatex.in.open_disposition = create_disposition;
273         io.ntcreatex.in.create_options = create_options;
274         io.ntcreatex.in.impersonation = 0;
275         io.ntcreatex.in.security_flags = 0;
276         io.ntcreatex.in.fname = fname;
277
278         ret = smb_raw_open(c->tree, mem_ctx, &io);
279
280         talloc_free(mem_ctx);
281
282         check_status("NTCreateX", status, ret);
283
284         if (!NT_STATUS_IS_OK(ret)) return;
285
286         f = malloc_p(struct ftable);
287         f->handle = handle;
288         f->fd = io.ntcreatex.out.file.fnum;
289
290         DLIST_ADD_END(ftable, f, struct ftable *);
291 }
292
293 void nb_writex(int handle, int offset, int size, int ret_size, NTSTATUS status)
294 {
295         union smb_write io;
296         int i;
297         NTSTATUS ret;
298         uint8_t *buf;
299
300         i = find_handle(handle);
301
302         if (bypass_io) return;
303
304         buf = malloc(size);
305         memset(buf, 0xab, size);
306
307         io.writex.level = RAW_WRITE_WRITEX;
308         io.writex.in.file.fnum = i;
309         io.writex.in.wmode = 0;
310         io.writex.in.remaining = 0;
311         io.writex.in.offset = offset;
312         io.writex.in.count = size;
313         io.writex.in.data = buf;
314
315         ret = smb_raw_write(c->tree, &io);
316
317         free(buf);
318
319         check_status("WriteX", status, ret);
320
321         if (NT_STATUS_IS_OK(ret) && io.writex.out.nwritten != ret_size) {
322                 printf("[%d] Warning: WriteX got count %d expected %d\n", 
323                        nbench_line_count,
324                        io.writex.out.nwritten, ret_size);
325         }       
326
327         children[nbio_id].bytes += ret_size;
328 }
329
330 void nb_write(int handle, int offset, int size, int ret_size, NTSTATUS status)
331 {
332         union smb_write io;
333         int i;
334         NTSTATUS ret;
335         uint8_t *buf;
336
337         i = find_handle(handle);
338
339         if (bypass_io) return;
340
341         buf = malloc(size);
342
343         memset(buf, 0x12, size);
344
345         io.write.level = RAW_WRITE_WRITE;
346         io.write.in.file.fnum = i;
347         io.write.in.remaining = 0;
348         io.write.in.offset = offset;
349         io.write.in.count = size;
350         io.write.in.data = buf;
351
352         ret = smb_raw_write(c->tree, &io);
353
354         free(buf);
355
356         check_status("Write", status, ret);
357
358         if (NT_STATUS_IS_OK(ret) && io.write.out.nwritten != ret_size) {
359                 printf("[%d] Warning: Write got count %d expected %d\n", 
360                        nbench_line_count,
361                        io.write.out.nwritten, ret_size);
362         }       
363
364         children[nbio_id].bytes += ret_size;
365 }
366
367
368 void nb_lockx(int handle, uint_t offset, int size, NTSTATUS status)
369 {
370         union smb_lock io;
371         int i;
372         NTSTATUS ret;
373         struct smb_lock_entry lck;
374
375         i = find_handle(handle);
376
377         lck.pid = getpid();
378         lck.offset = offset;
379         lck.count = size;
380
381         io.lockx.level = RAW_LOCK_LOCKX;
382         io.lockx.in.file.fnum = i;
383         io.lockx.in.mode = 0;
384         io.lockx.in.timeout = 0;
385         io.lockx.in.ulock_cnt = 0;
386         io.lockx.in.lock_cnt = 1;
387         io.lockx.in.locks = &lck;
388
389         ret = smb_raw_lock(c->tree, &io);
390
391         check_status("LockX", status, ret);
392 }
393
394 void nb_unlockx(int handle, uint_t offset, int size, NTSTATUS status)
395 {
396         union smb_lock io;
397         int i;
398         NTSTATUS ret;
399         struct smb_lock_entry lck;
400
401         i = find_handle(handle);
402
403         lck.pid = getpid();
404         lck.offset = offset;
405         lck.count = size;
406
407         io.lockx.level = RAW_LOCK_LOCKX;
408         io.lockx.in.file.fnum = i;
409         io.lockx.in.mode = 0;
410         io.lockx.in.timeout = 0;
411         io.lockx.in.ulock_cnt = 1;
412         io.lockx.in.lock_cnt = 0;
413         io.lockx.in.locks = &lck;
414
415         ret = smb_raw_lock(c->tree, &io);
416
417         check_status("UnlockX", status, ret);
418 }
419
420 void nb_readx(int handle, int offset, int size, int ret_size, NTSTATUS status)
421 {
422         union smb_read io;
423         int i;
424         NTSTATUS ret;
425         uint8_t *buf;
426
427         i = find_handle(handle);
428
429         if (bypass_io) return;
430
431         buf = malloc(size);
432
433         io.readx.level = RAW_READ_READX;
434         io.readx.in.file.fnum = i;
435         io.readx.in.offset    = offset;
436         io.readx.in.mincnt    = size;
437         io.readx.in.maxcnt    = size;
438         io.readx.in.remaining = 0;
439         io.readx.in.read_for_execute = False;
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.in.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.in.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.in.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.in.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 = RAW_SEARCH_TRANS2;
619         io.t2ffirst.data_level = level;
620         io.t2ffirst.in.max_count = maxcnt;
621         io.t2ffirst.in.search_attrib = FILE_ATTRIBUTE_DIRECTORY;
622         io.t2ffirst.in.pattern = mask;
623         io.t2ffirst.in.flags = FLAG_TRANS2_FIND_CLOSE;
624         io.t2ffirst.in.storage_type = 0;
625                         
626         ret = smb_raw_search_first(c->tree, mem_ctx, &io, NULL, findfirst_callback);
627
628         talloc_free(mem_ctx);
629
630         check_status("Search", status, ret);
631
632         if (NT_STATUS_IS_OK(ret) && io.t2ffirst.out.count != count) {
633                 printf("[%d] Warning: got count %d expected %d\n", 
634                        nbench_line_count,
635                        io.t2ffirst.out.count, count);
636         }
637 }
638
639 void nb_flush(int fnum, NTSTATUS status)
640 {
641         union smb_flush io;
642         NTSTATUS ret;
643         int i;
644         i = find_handle(fnum);
645
646         io.flush.level          = RAW_FLUSH_FLUSH;
647         io.flush.in.file.fnum   = i;
648
649         ret = smb_raw_flush(c->tree, &io);
650
651         check_status("Flush", status, ret);
652 }
653
654 void nb_sleep(int usec, NTSTATUS status)
655 {
656         usleep(usec);
657 }
658
659 void nb_deltree(const char *dname)
660 {
661         int total_deleted;
662
663         smb_raw_exit(c->session);
664
665         while (ftable) {
666                 struct ftable *f = ftable;
667                 DLIST_REMOVE(ftable, f);
668                 free(f);
669         }
670
671         total_deleted = smbcli_deltree(c->tree, dname);
672
673         if (total_deleted == -1) {
674                 printf("Failed to cleanup tree %s - exiting\n", dname);
675                 exit(1);
676         }
677
678         smbcli_rmdir(c->tree, dname);
679 }
680