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