summaryrefslogtreecommitdiff
path: root/source4/torture/nbench/nbio.c
blob: e35b9d14ab8de24293d2450ec99e5e1381d6c4c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
/*
  TODO: add splitting of writes for servers with signing
*/


/* 
   Unix SMB/CIFS implementation.
   SMB torture tester
   Copyright (C) Andrew Tridgell 1997-1998
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "system/time.h"
#include "system/filesys.h"
#include "lib/util/dlinklist.h"
#include "libcli/libcli.h"
#include "libcli/raw/libcliraw.h"
#include "torture/torture.h"
#include "libcli/libcli.h"
#include "torture/util.h"
#include "torture/nbench/proto.h"

extern int nbench_line_count;
static int nbio_id = -1;
static int nprocs;
static BOOL bypass_io;
static struct timeval tv_start, tv_end;
static int warmup, timelimit;
static int in_cleanup;

struct lock_info {
	struct lock_info *next, *prev;
	off_t offset;
	int size;
};

struct createx_params {
	char *fname;
	uint_t create_options;
	uint_t create_disposition;
	int handle;
};

struct ftable {
	struct ftable *next, *prev;
	int fd;     /* the fd that we got back from the server */
	int handle; /* the handle in the load file */
	struct createx_params cp;
	struct lock_info *locks;
};

static struct ftable *ftable;

static struct {
	double bytes, warmup_bytes;
	int line;
	int done;
	bool connected;
	double max_latency;
	struct timeval starttime;
} *children;

static bool nb_do_createx(struct ftable *f,
			  const char *fname,
			  uint_t create_options,
			  uint_t create_disposition,
			  int handle,
			  NTSTATUS status,
			  bool retry);

static bool nb_do_lockx(bool relock, int handle, off_t offset, int size, NTSTATUS status);

static void nb_set_createx_params(struct ftable *f,
				  const char *fname,
				  uint_t create_options,
				  uint_t create_disposition,
				  int handle)
{
	struct createx_params *cp = &f->cp;

	if (fname != NULL) {
		cp->fname = talloc_strdup(f, fname);
		if (cp->fname == NULL) {
			perror("nb_set_createx_params: strdup");
			nb_exit(1);
		}
	} else {
		cp->fname = NULL;
	}

	cp->create_options = create_options;
	cp->create_disposition = create_disposition;
	cp->handle = handle;
}

static bool nb_reestablish_locks(struct ftable *f)
{
	struct lock_info *linfo = f->locks;

	while (linfo != NULL) {
		DEBUG(1,("nb_reestablish_locks: lock for file %d at %lu\n",
			 f->handle, (unsigned long) linfo->offset));

		if (!nb_do_lockx(true, f->handle, linfo->offset, linfo->size, NT_STATUS_OK)) {
			printf("nb_reestablish_locks: failed to get lock for file %s at %lu\n",
			       f->cp.fname, (unsigned long) linfo->offset);
			return False;
		}

		linfo = linfo->next;
	}

	return True;
}

static bool nb_reopen_all_files(void)
{
	struct ftable *f = ftable;

	while (f != NULL) {
		DEBUG(1,("-- nb_reopen_all_files: opening %s (handle %d)\n",
			 f->cp.fname, f->cp.handle));

		if (!nb_do_createx(f,
				   f->cp.fname,
				   f->cp.create_options,
				   f->cp.create_disposition,
				   f->cp.handle,
				   NT_STATUS_OK,
				   True))
		{
			printf("-- nb_reopen_all_files: failed to open file %s\n", f->cp.fname);
			return False;
		}

		if (!nb_reestablish_locks(f)) {
			printf("--nb_reopen_all_files: failed to reestablish locks\n");
			return False;
		}

		f = f->next;
	}

	return True;
}

bool nb_reconnect(struct smbcli_state **cli, int client)
{
	children[client].connected = false;

	if (*cli != NULL) {
		talloc_free(*cli);
	}

	if (!torture_open_connection(cli, client)) {
		printf("nb_reconnect: failed to connect\n");
		*cli = NULL;
		return False;
	}

	nb_setup(*cli, client);

	if (!nb_reopen_all_files()) {
		printf("nb_reconnect: failed to reopen files in client %d\n", client);
		return False;
	}

	return True;
}

void nbio_target_rate(double rate)
{
	static double last_bytes;
	static struct timeval last_time;
	double tdelay;

	if (last_bytes == 0) {
		last_bytes = children[nbio_id].bytes;
		last_time = timeval_current();
		return;
	}

	tdelay = (children[nbio_id].bytes - last_bytes)/(1.0e6*rate) - timeval_elapsed(&last_time);
	if (tdelay > 0) {
		msleep(tdelay*1000);
	} else {
		children[nbio_id].max_latency = MAX(children[nbio_id].max_latency, -tdelay);
	}

	last_time = timeval_current();
	last_bytes = children[nbio_id].bytes;
}

void nbio_time_reset(void)
{
	children[nbio_id].starttime = timeval_current();	
}

void nbio_time_delay(double targett)
{
	double elapsed = timeval_elapsed(&children[nbio_id].starttime);
	if (targett > elapsed) {
		msleep(1000*(targett - elapsed));
	} else if (elapsed - targett > children[nbio_id].max_latency) {
		children[nbio_id].max_latency = MAX(elapsed - targett, children[nbio_id].max_latency);
	}
}

double nbio_result(void)
{
	int i;
	double total = 0;
	for (i=0;i<nprocs;i++) {
		total += children[i].bytes - children[i].warmup_bytes;
	}
	return 1.0e-6 * total / timeval_elapsed2(&tv_start, &tv_end);
}

double nbio_latency(void)
{
	int i;
	double max_latency = 0;
	for (i=0;i<nprocs;i++) {
		if (children[i].max_latency > max_latency) {
			max_latency = children[i].max_latency;
			children[i].max_latency = 0;
		}
	}
	return max_latency;
}

BOOL nb_tick(void)
{
	return children[nbio_id].done;
}


void nb_alarm(int sig)
{
	int i;
	int lines=0;
	double t;
	int in_warmup = 0;
	int num_connected = 0;

	if (nbio_id != -1) return;

	for (i=0;i<nprocs;i++) {
		if (children[i].connected) {
			num_connected++;
		}
		if (children[i].bytes == 0) {
			in_warmup = 1;
		}
		lines += children[i].line;
	}

	t = timeval_elapsed(&tv_start);

	if (!in_warmup && warmup>0 && t > warmup) {
		tv_start = timeval_current();
		warmup = 0;
		for (i=0;i<nprocs;i++) {
			children[i].warmup_bytes = children[i].bytes;
		}
		goto next;
	}
	if (t < warmup) {
		in_warmup = 1;
	} else if (!in_warmup && !in_cleanup && t > timelimit) {
		for (i=0;i<nprocs;i++) {
			children[i].done = 1;
		}
		tv_end = timeval_current();
		in_cleanup = 1;
	}
	if (t < 1) {
		goto next;
	}
	if (!in_cleanup) {
		tv_end = timeval_current();
	}

	if (in_warmup) {
		printf("%4d  %8d  %.2f MB/sec  warmup %.0f sec   \n", 
		       num_connected, lines/nprocs, 
		       nbio_result(), t);
	} else if (in_cleanup) {
		printf("%4d  %8d  %.2f MB/sec  cleanup %.0f sec   \n", 
		       num_connected, lines/nprocs, 
		       nbio_result(), t);
	} else {
		printf("%4d  %8d  %.2f MB/sec  execute %.0f sec  latency %.2f msec \n", 
		       num_connected, lines/nprocs, 
		       nbio_result(), t, nbio_latency() * 1.0e3);
	}

	fflush(stdout);
next:
	signal(SIGALRM, nb_alarm);
	alarm(1);	
}

void nbio_shmem(int n, int t_timelimit, int t_warmup)
{
	nprocs = n;
	children = shm_setup(sizeof(*children) * nprocs);
	if (!children) {
		printf("Failed to setup shared memory!\n");
		nb_exit(1);
	}
	memset(children, 0, sizeof(*children) * nprocs);
	timelimit = t_timelimit;
	warmup = t_warmup;
	in_cleanup = 0;
	tv_start = timeval_current();
}

static struct lock_info* find_lock(struct lock_info *linfo, off_t offset, int size)
{
	while (linfo != NULL) {
		if (linfo->offset == offset &&
		    linfo->size == size)
		{
			return linfo;
		}

		linfo = linfo->next;
	}

	return NULL;
}

static struct ftable *find_ftable(int handle)
{
	struct ftable *f;

	for (f=ftable;f;f=f->next) {
		if (f->handle == handle) return f;
	}
	return NULL;
}

static int find_handle(int handle, struct ftable **f_ret)
{
	struct ftable *f;

	if (f_ret != NULL)
		*f_ret = NULL;

	children[nbio_id].line = nbench_line_count;

	f = find_ftable(handle);
	if (f) {
		if (f_ret != NULL)
			*f_ret = f;
		return f->fd;
	}
	printf("(%d) ERROR: handle %d was not found\n", 
	       nbench_line_count, handle);
	nb_exit(1);

	return -1;		/* Not reached */
}



static struct smbcli_state *c;

/*
  a handler function for oplock break requests
*/
static BOOL oplock_handler(struct smbcli_transport *transport, uint16_t tid, 
			   uint16_t fnum, uint8_t level, void *private)
{
	struct smbcli_tree *tree = (struct smbcli_tree *)private;
	return smbcli_oplock_ack(tree, fnum, OPLOCK_BREAK_TO_NONE);
}

void nb_setup(struct smbcli_state *cli, int id)
{
	nbio_id = id;
	c = cli;
	if (bypass_io)
		printf("skipping I/O\n");

	if (cli) {
		smbcli_oplock_handler(cli->transport, oplock_handler, cli->tree);
	}

	children[id].connected = true;
}


static bool check_status(const char *op, NTSTATUS status, NTSTATUS ret)
{
	if ((NT_STATUS_EQUAL(ret, NT_STATUS_END_OF_FILE) ||
	     NT_STATUS_EQUAL(ret, NT_STATUS_NET_WRITE_FAULT) ||
	     NT_STATUS_EQUAL(ret, NT_STATUS_CONNECTION_RESET)) 
		&& !NT_STATUS_EQUAL (status, ret))
	{
		return False;
	}

	if (!NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(ret)) {
		printf("[%d] Error: %s should have failed with %s\n", 
		       nbench_line_count, op, nt_errstr(status));
		nb_exit(1);
	}

	if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(ret)) {
		printf("[%d] Error: %s should have succeeded - %s\n", 
		       nbench_line_count, op, nt_errstr(ret));
		nb_exit(1);
	}

	if (!NT_STATUS_EQUAL(status, ret)) {
		printf("[%d] Warning: got status %s but expected %s\n",
		       nbench_line_count, nt_errstr(ret), nt_errstr(status));
	}

	return True;
}


bool nb_unlink(const char *fname, int attr, NTSTATUS status, bool retry)
{
	union smb_unlink io;
	NTSTATUS ret;

	io.unlink.in.pattern = fname;

	io.unlink.in.attrib = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
	if (strchr(fname, '*') == 0) {
		io.unlink.in.attrib |= FILE_ATTRIBUTE_DIRECTORY;
	}

	ret = smb_raw_unlink(c->tree, &io);

	if (!retry)
		return check_status("Unlink", status, ret);

	return True;
}

static bool nb_do_createx(struct ftable *f,
			  const char *fname,
			  uint_t create_options,
			  uint_t create_disposition,
			  int handle,
			  NTSTATUS status,
			  bool retry)
{
	union smb_open io;	
	uint32_t desired_access;
	NTSTATUS ret;
	TALLOC_CTX *mem_ctx;
	uint_t flags = 0;

	mem_ctx = talloc_init("raw_open");

	if (create_options & NTCREATEX_OPTIONS_DIRECTORY) {
		desired_access = SEC_FILE_READ_DATA;
	} else {
		desired_access = 
			SEC_FILE_READ_DATA | 
			SEC_FILE_WRITE_DATA |
			SEC_FILE_READ_ATTRIBUTE |
			SEC_FILE_WRITE_ATTRIBUTE;
		flags = NTCREATEX_FLAGS_EXTENDED |
			NTCREATEX_FLAGS_REQUEST_OPLOCK | 
			NTCREATEX_FLAGS_REQUEST_BATCH_OPLOCK;
	}

	io.ntcreatex.level = RAW_OPEN_NTCREATEX;
	io.ntcreatex.in.flags = flags;
	io.ntcreatex.in.root_fid = 0;
	io.ntcreatex.in.access_mask = desired_access;
	io.ntcreatex.in.file_attr = 0;
	io.ntcreatex.in.alloc_size = 0;
	io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE;
	io.ntcreatex.in.open_disposition = create_disposition;
	io.ntcreatex.in.create_options = create_options;
	io.ntcreatex.in.impersonation = 0;
	io.ntcreatex.in.security_flags = 0;
	io.ntcreatex.in.fname = fname;

	if (retry) {
		/* Reopening after a disconnect. */
		io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
	} else
	if (f != NULL &&
	    f->cp.create_disposition == NTCREATEX_DISP_CREATE &&
	    NT_STATUS_IS_OK(status))
	{
		/* Reopening after nb_createx() error. */
		io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
	}

	ret = smb_raw_open(c->tree, mem_ctx, &io);

	talloc_free(mem_ctx);

	if (!check_status("NTCreateX", status, ret))
		return False;

	if (!NT_STATUS_IS_OK(ret))
		return True;

	if (f == NULL) {
		f = talloc (NULL, struct ftable);
		f->locks = NULL;
		nb_set_createx_params(f, fname, create_options, create_disposition, handle);
		DLIST_ADD_END(ftable, f, struct ftable *);
	}

	f->handle = handle;
	f->fd = io.ntcreatex.out.file.fnum;

	return True;
}

bool nb_createx(const char *fname, 
	       uint_t create_options, uint_t create_disposition, int handle,
	       NTSTATUS status)
{
	return nb_do_createx(NULL, fname, create_options, create_disposition, handle, status, False);
}

bool nb_writex(int handle, off_t offset, int size, int ret_size, NTSTATUS status)
{
	union smb_write io;
	int i;
	NTSTATUS ret;
	uint8_t *buf;

	i = find_handle(handle, NULL);

	if (bypass_io)
		return True;

	buf = malloc(size);
	memset(buf, 0xab, size);

	io.writex.level = RAW_WRITE_WRITEX;
	io.writex.in.file.fnum = i;
	io.writex.in.wmode = 0;
	io.writex.in.remaining = 0;
	io.writex.in.offset = offset;
	io.writex.in.count = size;
	io.writex.in.data = buf;

	ret = smb_raw_write(c->tree, &io);

	free(buf);

	if (!check_status("WriteX", status, ret))
		return False;

	if (NT_STATUS_IS_OK(ret) && io.writex.out.nwritten != ret_size) {
		printf("[%d] Warning: WriteX got count %d expected %d\n", 
		       nbench_line_count,
		       io.writex.out.nwritten, ret_size);
	}	

	children[nbio_id].bytes += ret_size;

	return True;
}

bool nb_write(int handle, off_t offset, int size, int ret_size, NTSTATUS status)
{
	union smb_write io;
	int i;
	NTSTATUS ret;
	uint8_t *buf;

	i = find_handle(handle, NULL);

	if (bypass_io)
		return True;

	buf = malloc(size);

	memset(buf, 0x12, size);

	io.write.level = RAW_WRITE_WRITE;
	io.write.in.file.fnum = i;
	io.write.in.remaining = 0;
	io.write.in.offset = offset;
	io.write.in.count = size;
	io.write.in.data = buf;

	ret = smb_raw_write(c->tree, &io);

	free(buf);

	if (!check_status("Write", status, ret))
		return False;

	if (NT_STATUS_IS_OK(ret) && io.write.out.nwritten != ret_size) {
		printf("[%d] Warning: Write got count %d expected %d\n", 
		       nbench_line_count,
		       io.write.out.nwritten, ret_size);
	}	

	children[nbio_id].bytes += ret_size;

	return True;
}

static bool nb_do_lockx(bool relock, int handle, off_t offset, int size, NTSTATUS status)
{
	union smb_lock io;
	int i;
	NTSTATUS ret;
	struct smb_lock_entry lck;
	struct ftable *f;

	i = find_handle(handle, &f);

	lck.pid = getpid();
	lck.offset = offset;
	lck.count = size;

	io.lockx.level = RAW_LOCK_LOCKX;
	io.lockx.in.file.fnum = i;
	io.lockx.in.mode = 0;
	io.lockx.in.timeout = 0;
	io.lockx.in.ulock_cnt = 0;
	io.lockx.in.lock_cnt = 1;
	io.lockx.in.locks = &lck;

	ret = smb_raw_lock(c->tree, &io);

	if (!check_status("LockX", status, ret))
		return False;

	if (f != NULL &&
	    !relock)
	{
		struct lock_info *linfo;
		linfo = talloc (f, struct lock_info);
		linfo->offset = offset;
		linfo->size = size;
		DLIST_ADD_END(f->locks, linfo, struct lock_info *);
	}

	return True;
}

bool nb_lockx(int handle, off_t offset, int size, NTSTATUS status)
{
	return nb_do_lockx(false, handle, offset, size, status);
}

bool nb_unlockx(int handle, uint_t offset, int size, NTSTATUS status)
{
	union smb_lock io;
	int i;
	NTSTATUS ret;
	struct smb_lock_entry lck;
	struct ftable *f;

	i = find_handle(handle, &f);

	lck.pid = getpid();
	lck.offset = offset;
	lck.count = size;

	io.lockx.level = RAW_LOCK_LOCKX;
	io.lockx.in.file.fnum = i;
	io.lockx.in.mode = 0;
	io.lockx.in.timeout = 0;
	io.lockx.in.ulock_cnt = 1;
	io.lockx.in.lock_cnt = 0;
	io.lockx.in.locks = &lck;

	ret = smb_raw_lock(c->tree, &io);

	if (!check_status("UnlockX", status, ret))
		return False;

	if (f != NULL) {
		struct lock_info *linfo;
		linfo = find_lock(f->locks, offset, size);
		if (linfo != NULL)
			DLIST_REMOVE(f->locks, linfo);
		else
			printf("nb_unlockx: unknown lock (%d)\n", handle);
	}

	return True;
}

bool nb_readx(int handle, off_t offset, int size, int ret_size, NTSTATUS status)
{
	union smb_read io;
	int i;
	NTSTATUS ret;
	uint8_t *buf;

	i = find_handle(handle, NULL);

	if (bypass_io)
		return True;

	buf = malloc(size);

	io.readx.level = RAW_READ_READX;
	io.readx.in.file.fnum = i;
	io.readx.in.offset    = offset;
	io.readx.in.mincnt    = size;
	io.readx.in.maxcnt    = size;
	io.readx.in.remaining = 0;
	io.readx.in.read_for_execute = False;
	io.readx.out.data     = buf;

	ret = smb_raw_read(c->tree, &io);

	free(buf);

	if (!check_status("ReadX", status, ret))
		return False;

	if (NT_STATUS_IS_OK(ret) && io.readx.out.nread != ret_size) {
		printf("[%d] ERROR: ReadX got count %d expected %d\n", 
		       nbench_line_count,
		       io.readx.out.nread, ret_size);
		nb_exit(1);
	}	

	children[nbio_id].bytes += ret_size;

	return True;
}

bool nb_close(int handle, NTSTATUS status)
{
	NTSTATUS ret;
	union smb_close io;
	int i;

	i = find_handle(handle, NULL);

	io.close.level = RAW_CLOSE_CLOSE;
	io.close.in.file.fnum = i;
	io.close.in.write_time = 0;

	ret = smb_raw_close(c->tree, &io);

	if (!check_status("Close", status, ret))
		return False;

	if (NT_STATUS_IS_OK(ret)) {
		struct ftable *f = find_ftable(handle);
		DLIST_REMOVE(ftable, f);
		talloc_free(f);
	}

	return True;
}

bool nb_rmdir(const char *dname, NTSTATUS status, bool retry)
{
	NTSTATUS ret;
	struct smb_rmdir io;

	io.in.path = dname;

	ret = smb_raw_rmdir(c->tree, &io);

	if (!retry)
		return check_status("Rmdir", status, ret);

	return True;
}

bool nb_mkdir(const char *dname, NTSTATUS status, bool retry)
{
	union smb_mkdir io;

	io.mkdir.level = RAW_MKDIR_MKDIR;
	io.mkdir.in.path = dname;

	/* NOTE! no error checking. Used for base fileset creation */
	smb_raw_mkdir(c->tree, &io);

	return True;
}

bool nb_rename(const char *old, const char *new, NTSTATUS status, bool retry)
{
	NTSTATUS ret;
	union smb_rename io;

	io.generic.level = RAW_RENAME_RENAME;
	io.rename.in.attrib = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY;
	io.rename.in.pattern1 = old;
	io.rename.in.pattern2 = new;

	ret = smb_raw_rename(c->tree, &io);

	if (!retry)
		return check_status("Rename", status, ret);

	return True;
}


bool nb_qpathinfo(const char *fname, int level, NTSTATUS status)
{
	union smb_fileinfo io;
	TALLOC_CTX *mem_ctx;
	NTSTATUS ret;

	mem_ctx = talloc_init("nb_qpathinfo");

	io.generic.level = level;
	io.generic.in.file.path = fname;

	ret = smb_raw_pathinfo(c->tree, mem_ctx, &io);

	talloc_free(mem_ctx);

	return check_status("Pathinfo", status, ret);
}


bool nb_qfileinfo(int fnum, int level, NTSTATUS status)
{
	union smb_fileinfo io;
	TALLOC_CTX *mem_ctx;
	NTSTATUS ret;
	int i;

	i = find_handle(fnum, NULL);

	mem_ctx = talloc_init("nb_qfileinfo");

	io.generic.level = level;
	io.generic.in.file.fnum = i;

	ret = smb_raw_fileinfo(c->tree, mem_ctx, &io);

	talloc_free(mem_ctx);

	return check_status("Fileinfo", status, ret);
}

bool nb_sfileinfo(int fnum, int level, NTSTATUS status)
{
	union smb_setfileinfo io;
	NTSTATUS ret;
	int i;

	if (level != RAW_SFILEINFO_BASIC_INFORMATION) {
		printf("[%d] Warning: setfileinfo level %d not handled\n", nbench_line_count, level);
		return True;
	}

	ZERO_STRUCT(io);

	i = find_handle(fnum, NULL);

	io.generic.level = level;
	io.generic.in.file.fnum = i;
	unix_to_nt_time(&io.basic_info.in.create_time, time(NULL));
	unix_to_nt_time(&io.basic_info.in.access_time, 0);
	unix_to_nt_time(&io.basic_info.in.write_time, 0);
	unix_to_nt_time(&io.basic_info.in.change_time, 0);
	io.basic_info.in.attrib = 0;

	ret = smb_raw_setfileinfo(c->tree, &io);

	return check_status("Setfileinfo", status, ret);
}

bool nb_qfsinfo(int level, NTSTATUS status)
{
	union smb_fsinfo io;
	TALLOC_CTX *mem_ctx;
	NTSTATUS ret;

	mem_ctx = talloc_init("smbcli_dskattr");

	io.generic.level = level;
	ret = smb_raw_fsinfo(c->tree, mem_ctx, &io);

	talloc_free(mem_ctx);
	
	return check_status("Fsinfo", status, ret);	
}

/* callback function used for trans2 search */
static BOOL findfirst_callback(void *private, const union smb_search_data *file)
{
	return True;
}

bool nb_findfirst(const char *mask, int level, int maxcnt, int count, NTSTATUS status)
{
	union smb_search_first io;
	TALLOC_CTX *mem_ctx;
	NTSTATUS ret;

	mem_ctx = talloc_init("smbcli_dskattr");

	io.t2ffirst.level = RAW_SEARCH_TRANS2;
	io.t2ffirst.data_level = level;
	io.t2ffirst.in.max_count = maxcnt;
	io.t2ffirst.in.search_attrib = FILE_ATTRIBUTE_DIRECTORY;
	io.t2ffirst.in.pattern = mask;
	io.t2ffirst.in.flags = FLAG_TRANS2_FIND_CLOSE;
	io.t2ffirst.in.storage_type = 0;
			
	ret = smb_raw_search_first(c->tree, mem_ctx, &io, NULL, findfirst_callback);

	talloc_free(mem_ctx);

	if (!check_status("Search", status, ret))
		return False;

	if (NT_STATUS_IS_OK(ret) && io.t2ffirst.out.count != count) {
		printf("[%d] Warning: got count %d expected %d\n", 
		       nbench_line_count,
		       io.t2ffirst.out.count, count);
	}

	return True;
}

bool nb_flush(int fnum, NTSTATUS status)
{
	union smb_flush io;
	NTSTATUS ret;
	int i;
	i = find_handle(fnum, NULL);

	io.flush.level		= RAW_FLUSH_FLUSH;
	io.flush.in.file.fnum	= i;

	ret = smb_raw_flush(c->tree, &io);

	return check_status("Flush", status, ret);
}

void nb_sleep(int usec, NTSTATUS status)
{
	usleep(usec);
}

bool nb_deltree(const char *dname, bool retry)
{
	int total_deleted;

	smb_raw_exit(c->session);

	while (ftable) {
		struct ftable *f = ftable;
		DLIST_REMOVE(ftable, f);
		talloc_free (f);
	}

	total_deleted = smbcli_deltree(c->tree, dname);

	if (total_deleted == -1) {
		printf("Failed to cleanup tree %s - exiting\n", dname);
		nb_exit(1);
	}

	smbcli_rmdir(c->tree, dname);

	return True;
}


void nb_exit(int status)
{
	children[nbio_id].connected = false;
	printf("[%d] client %d exiting with status %d\n",
	       nbench_line_count, nbio_id, status);
	exit(status);
}