-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathdtinfo.c
1333 lines (1244 loc) · 42.8 KB
/
dtinfo.c
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
998
999
1000
/****************************************************************************
* *
* COPYRIGHT (c) 1988 - 2023 *
* This Software Provided *
* By *
* Robin's Nest Software Inc. *
* *
* Permission to use, copy, modify, distribute and sell this software and *
* its documentation for any purpose and without fee is hereby granted, *
* provided that the above copyright notice appear in all copies and that *
* both that copyright notice and this permission notice appear in the *
* supporting documentation, and that the name of the author not be used *
* in advertising or publicity pertaining to distribution of the software *
* without specific, written prior permission. *
* *
* THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, *
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN *
* NO EVENT SHALL HE BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL *
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR *
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS *
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF *
* THIS SOFTWARE. *
* *
****************************************************************************/
/*
* Module: dtinfo.c
* Author: Robin T. Miller
* Date: September 8, 1993
*
* Description:
* Setup device and/or system information for 'dt' program.
*
* Modification History:
*
* September 20th, 2023 by Robin T. Miller
* When setting up device information, beware of overwriting the user
* specified device type.
*
* February 4th, 2020 by Robin T. Miller
* For Windows and existing file, set the filesize like we do for Unix!
*
* December 28th, 2019 by Robin T. Miller
* For regular files, do NOT override the user specified block sizes
* for regular files unless Direct I/O is specified, otherwise we cannot
* perform non-block aligned I/O. In my haste to fix (override) incorrect
* min/max/incr values for user specified device sizes (dsize=value), I
* accidently broke this previous (desirable) file system behavior.
*
* December 20th, 2019 by Robin T. Miller
* With changing dispose mode to KEEP_ON_ERROR, ensure existing files
* do *not* get deleted by setting dispose to KEEP_FILE.
*
* July 19th, 2019 by Robin T. Miller
* When user specifies an alternate device size (dsize=4k), ensure all
* device size and I/O parameters are updated accordingly.
* Note: This mismatch caused a false corruption due to I/O sizes NOT being
* modulo the user device size, and did not enforce aligned I/O on the same.
* When using block tags, the CRC generated on short reads was incorrect!
*
* July 8th, 2019 by Robin T. Miller
* Update setup_device_info() to always call os_system_device_info() so
* disk specific information from the OS via IOCTL gets setup properly. This
* is only an issue when dtype=disk is specified, but if the IOCTL information
* is less than the actual disk capacity, EINVAL occurs to unreachable offsets.
* It's not invalid to return less disk capacity, since some may be reserved.
* Note: Without refactoring code, the user device type may get overwritten!
*
* April 12th, 2018 by Robin T. Miller
* For Linux, add function to set device block size. This is required
* for file systems when direct I/O is enabled, where I/O sizes muct be
* modulo the device size, otherwise EINVAL errors occur (misleading).
*
* June 15th, 2014 by Robin T. Miller
* On Linux for direct disks, ensure the DIO flag is set true, since
* new logic for handling Direct I/O and buffering mode, requires this!
*
* June 7th, 2014 by Robin T. Miller
* For regular files that exist, use consistent logic for setting the
* user capacity for both reads and writes. This is consistent with dt v18,
* and required so random reads will match random writes. Failure to do this
* results in the wrong read offsets and thus false data corruptions! :-(
*
* Auguest 17th, 2013 by Robin T. Miller
* Always setup the disk capacity, if we can acquire it, since new tests
* and sanity checks like copy/verify need this information. Previosuly, the
* capacity was only set for random I/O or slices, so sequentail I/O could
* test EOF/EOM conditions. This latter testing proved problematic for some OS's,
* such as Windows and Solaris, since apparently POSIX does NOT define proper end
* of media errors for direct (raw) disk access.
*
* June 20th, 2013 by Robin T Miller
* Mostly a rewrite for multithreaded IO, so starting with new history!
*/
#include "dt.h"
#if !defined(_QNX_SOURCE)
# if !defined(sun) && !defined(WIN32)
# include <sys/ioctl.h>
# endif /* !defined(sun) */
#endif /* !defined(_QNX_SOURCE) */
#include <sys/stat.h>
/*
* Forward References:
*/
void setup_device_defaults(struct dinfo *dip);
void SetupRegularFile(struct dinfo *dip, large_t file_size);
#if defined(DEC)
int SetupDiskAttributes(struct dinfo *dip, int fd);
#endif /* defined(DEC) */
void os_system_device_info(struct dinfo *dip);
large_t GetMaxUserCapacity(dinfo_t *dip, hbool_t use_records);
/************************************************************************
* *
* setup_device_type() - Setup the device type. *
* *
* Description: *
* This function sets up the specific device type to be tested. *
* Since each operating system has a different method of identifying *
* devices, so this table lookup allows the user to specify the device *
* type. Of course, this could be used to force errors. *
* *
* Inputs: str = Pointer to device type string. *
* *
* Return Value: *
* Returns pointer to device type table entry or NULL. *
* *
************************************************************************/
struct dtype dtype_table[] = {
{ "block", DT_BLOCK },
{ "character", DT_CHARACTER },
{ "comm", DT_COMM },
{ "disk", DT_DISK },
{ "directory", DT_DIRECTORY },
{ "graphics", DT_GRAPHICS },
{ "memory", DT_MEMORY },
{ "mmap", DT_MMAP },
{ "network", DT_NETWORK },
{ "pipe", DT_PIPE },
{ "processor", DT_PROCESSOR },
{ "regular", DT_REGULAR },
{ "socket", DT_SOCKET },
{ "special", DT_SPECIAL },
{ "streams", DT_STREAMS },
{ "tape", DT_TAPE },
{ "unknown", DT_UNKNOWN }
};
int num_dtypes = sizeof(dtype_table) / sizeof(dtype_table[0]);
struct dtype *
setup_device_type(char *str)
{
int i;
struct dtype *dtp;
for (dtp = dtype_table, i = 0; i < num_dtypes; i++, dtp++) {
if (strcmp(str, dtp->dt_type) == 0) {
return (dtp);
}
}
Fprint(NULL, "Device type '%s' is invalid, valid entrys are:\n", str);
for (dtp = dtype_table, i = 0; i < num_dtypes; i++, dtp++) {
if ( (i % 4) == 0) Fprint(NULL, "\n");
Fprint(NULL, " %-12s", dtp->dt_type);
}
Fprint(NULL, "\n");
return ((struct dtype *) 0);
}
/************************************************************************
* *
* setup_device_defaults() - Setup the device defaults. *
* *
* Description: *
* This function sets up the specific device type defaults, for *
* test parameters which were not specified. *
* *
* Inputs: dip = The device information pointer. *
* *
* Return Value: *
* Returns pointer to device type table entry or NULL. *
* *
* Note: This function may get called twice! On Tru64 Unix, after we *
* open the device, the initial device type may get overridden. *
* *
************************************************************************/
void
setup_device_defaults(struct dinfo *dip)
{
struct dtype *dtp = dip->di_dtype;
if ( (dtp->dt_dtype == DT_BLOCK) ||
(dtp->dt_dtype == DT_DISK) ||
(dtp->dt_dtype == DT_MMAP) ||
(dtp->dt_dtype == DT_REGULAR) || dip->di_random_io ) {
/*
* Note: For regular files without DIO, the device size should
* be set to one (1), since that's the smallest I/O transfer size.
* But that said, changing this now may break other sanity checks!
* Why bother? We can't do modulo dsize/bs sanity checks as it is!
*/
if (dip->di_debug_flag) {
Printf(dip, "Device size: %u, Real Device Size: %u, User Device Size: %u\n",
dip->di_dsize, dip->di_rdsize, dip->di_device_size);
}
if (dip->di_device_size) {
dip->di_dsize = dip->di_device_size; /* Override, with user dsize! */
}
if ((dip->di_device_size == 0) && dip->di_rdsize) {
dip->di_device_size = dip->di_rdsize; /* Use real device block size. */
}
if (dip->di_device_size == 0) {
dip->di_device_size = BLOCK_SIZE; /* Set our default block size. */
}
if (dip->di_lbdata_size == 0) {
dip->di_lbdata_size = dip->di_device_size; /* Set lbdata size also for IOT. */
}
if (dip->di_max_size && (dip->di_user_min == False)) {
dip->di_min_size = dip->di_device_size; /* Set a min value, if none specified. */
}
if (dip->di_min_size && (dip->di_user_incr == False)) {
dip->di_incr_count = dip->di_device_size; /* Set increment value, if none specified. */
}
/* Ensure min and incr values are non-zero, if user specified ranges. */
if (dip->di_max_size && (dip->di_min_size == 0)) {
dip->di_min_size = dip->di_device_size; /* Set a min value, required with max. */
}
if (dip->di_min_size && (dip->di_incr_count == 0)) {
dip->di_incr_count = dip->di_device_size; /* Set an incr value, required with min. */
}
/* Ensure variable sizes are in line with the device size (user or OS block size). */
/* When using direct I/O or specifying a device size, we override to make correct. */
/* Note to self: If user values are *not* correct, then options need updated! */
if ( isDiskDevice(dip) || (dip->di_dio_flag == True) ) {
if (dip->di_block_size < dip->di_device_size) {
if (dip->di_debug_flag) {
Wprintf(dip, "Block size %u, overridden with device size %u.\n",
dip->di_block_size, dip->di_device_size);
}
dip->di_block_size = dip->di_device_size;
}
if (dip->di_min_size && (dip->di_min_size < dip->di_device_size)) {
if (dip->di_debug_flag) {
Wprintf(dip, "Minimum size %u, overridden with device size %u.\n",
dip->di_min_size, dip->di_device_size);
}
dip->di_min_size = dip->di_device_size;
}
if (dip->di_max_size && (dip->di_max_size < dip->di_device_size)) {
if (dip->di_debug_flag) {
Wprintf(dip, "Maximum size %u, overridden with device size %u.\n",
dip->di_max_size, dip->di_device_size);
}
dip->di_max_size = dip->di_device_size;
}
if (dip->di_incr_count && (dip->di_incr_count < dip->di_device_size)) {
if (dip->di_debug_flag) {
Wprintf(dip, "Increment count %u, overridden with device size %u.\n",
dip->di_incr_count, dip->di_device_size);
}
dip->di_incr_count = dip->di_device_size;
}
}
/* End of device size sanity checks! */
if (dip->di_fsalign_flag && dip->di_random_io) {
if (dip->di_random_align == 0) {
dip->di_random_align = dip->di_device_size; /* Align to device size. */
}
}
if (dip->di_sleep_res == SLEEP_DEFAULT) {
dip->di_sleep_res = SLEEP_USECS; /* Disks get microsecond delays! */
}
if (dip->di_fsync_flag == UNINITIALIZED) {
if ( (dtp->dt_dtype == DT_BLOCK) ||
(dtp->dt_dtype == DT_REGULAR) ) {
dip->di_fsync_flag = True;
} else if (dtp->dt_dtype == DT_DISK) {
/*
* Devices identified as DT_DISK should be the raw (character) device.
* Since some OS's, such as AIX don't like fsync() to disks, we'll disable
* it since it really only has meaning to block or regular (FS) files.
*/
dip->di_fsync_flag = False;
}
}
/*
* Additional setup for direct disk testing.
*/
if (dtp->dt_dtype == DT_DISK) {
if (dip->di_block_size < dip->di_device_size) {
dip->di_block_size = dip->di_device_size;
}
#if defined(__linux__)
dip->di_dio_flag = True;
dip->di_open_flags |= O_DIRECT;
#endif /* defined(__linux__) */
}
} else { /* Tapes, pipes, serial lines, etc. */
if (!dip->di_device_size) dip->di_device_size = 1;
if (!dip->di_lbdata_size) dip->di_lbdata_size = dip->di_device_size;
if (dip->di_max_size && !dip->di_user_min) dip->di_min_size = 1;
if (dip->di_min_size && !dip->di_user_incr) dip->di_incr_count = 1;
/* Ensure min and incr values are non-zero! */
if (dip->di_max_size && !dip->di_min_size) dip->di_min_size = 1;
if (dip->di_min_size && !dip->di_incr_count) dip->di_incr_count = 1;
dip->di_fsync_flag = False;
}
return;
}
/************************************************************************
* *
* os_system_device_info() - Get OS System Device Information. *
* *
* Description: *
* This function attempts to obtain device information necessary *
* for device specific testing, by using system dependent syscalls. *
* Note: This function is called _after_ the device/file is opened. *
* *
* Inputs: dip = The device information pointer. *
* *
* Return Value: *
* None. *
* *
************************************************************************/
#if defined(AIX)
#include <sys/devinfo.h>
void
os_system_device_info(struct dinfo *dip)
{
struct devinfo devinfo;
struct devinfo *devinfop = &devinfo;
int fd = dip->di_fd;
hbool_t temp_fd = False;
short category;
int i;
if (fd == NoFd) {
temp_fd = True;
if ( (fd = open(dip->di_dname, O_RDONLY)) < 0) {
return;
}
}
(void)memset(devinfop, '\0', sizeof(*devinfop));
if (ioctl (fd, IOCINFO, devinfop) == SUCCESS) {
switch (devinfop->devtype) {
case DD_DISK: { /* Includes LV's! */
if (devinfop->flags & DF_LGDSK) {
dip->di_rdsize = devinfop->un.dk64.bytpsec;
if (!dip->di_dsize) dip->di_dsize = dip->di_rdsize;
dip->di_capacity = (large_t)
((u_int64_t)devinfop->un.dk64.hi_numblks << 32L) |
(uint32_t)devinfop->un.dk64.lo_numblks;
} else {
dip->di_rdsize = devinfop->un.dk.bytpsec;
if (!dip->di_dsize) dip->di_dsize = dip->di_rdsize;
dip->di_capacity = (large_t)devinfop->un.dk.numblks;
}
break;
}
case DD_SCDISK: {
if (devinfop->flags & DF_LGDSK) {
dip->di_rdsize = devinfop->un.scdk64.blksize;
if (!dip->di_dsize) dip->di_dsize = dip->di_rdsize;
dip->di_capacity = (large_t)
((u_int64_t)devinfop->un.scdk64.hi_numblks << 32L) |
(uint32_t)devinfop->un.scdk64.lo_numblks;
} else {
dip->di_rdsize = devinfop->un.scdk.blksize;
if (!dip->di_dsize) dip->di_dsize = dip->di_rdsize;
dip->di_capacity = (large_t)devinfop->un.scdk.numblks;
}
break;
}
case DD_TAPE:
case DD_SCTAPE:
dip->di_dtype = setup_device_type("tape");
break;
default:
break;
}
/*
* Common Disk Setup:
*/
if ( (devinfop->devtype == DD_DISK) || (devinfop->devtype == DD_SCDISK) ) {
if ( (dip->di_max_capacity == False) && !dip->di_user_capacity ) {
dip->di_max_capacity = True;
dip->di_user_capacity = (dip->di_capacity * (large_t)dip->di_rdsize);
}
if (dip->di_debug_flag) {
Printf(dip, "IOCINFO Capacity: " LUF " blocks, device size %u bytes.\n",
dip->di_capacity, dip->di_dsize);
}
if (dip->di_dsize && !dip->di_user_lbsize && !dip->di_lbdata_size) {
dip->di_lbdata_size = dip->di_dsize;
}
dip->di_dtype = setup_device_type("disk");
}
}
if (temp_fd) (void)close(fd);
return;
}
#endif /* defined(AIX) */
#if defined(DEC)
#include <sys/devio.h>
#include <sys/ioctl.h>
#if defined(DEC)
# include <sys/ioctl_compat.h>
# include <io/common/devgetinfo.h>
#endif /* defined(DEC) */
void
os_system_device_info(struct dinfo *dip)
{
struct devget devget, *devgp = NULL;
device_info_t devinfo, *devip = NULL;
int fd = dip->di_fd;
hbool_t temp_fd = False;
short category;
int i, status;
if (fd == NoFd) {
temp_fd = True;
if ( (fd = open (dip->di_dname, (O_RDONLY | O_NDELAY))) < 0) {
return;
}
}
/*
* Attempt to obtain the device information.
*/
memset(&devinfo, '\0', sizeof(devinfo));
if (ioctl (fd, DEVGETINFO, (char *) &devinfo) == SUCCESS) {
devip = &devinfo;
category = devip->v1.category;
if ( NEL (devip->v1.device, DEV_UNKNOWN, DEV_STRING_SIZE) ) {
dip->di_device = Malloc(dip, (DEV_STRING_SIZE + 1) );
(void) strncpy (dip->di_device, devip->v1.device, DEV_STRING_SIZE);
} else if ( NEL (devip->v1.dev_name, DEV_UNKNOWN, DEV_STRING_SIZE) ) {
dip->di_device = Malloc(dip, (DEV_STRING_SIZE + 1) );
(void) strncpy (dip->di_device, devip->v1.dev_name, DEV_STRING_SIZE);
}
if (dip->di_device) {
/*
* In Steel, device names have trailing spaces. grrr!
*/
for (i = (DEV_STRING_SIZE); i--; ) {
if ( isspace(dip->di_device[i]) ) {
dip->di_device[i] = '\0';
} else {
break;
}
}
}
} else { /* Try the old DEVIOCGET IOCTL... */
memset(&devget, '\0', sizeof(devget));
if (ioctl (fd, DEVIOCGET, (char *) &devget) < 0) {
if (temp_fd) (void)close(fd);
return;
}
devgp = &devget;
category = devgp->category;
if ( NEL (devgp->device, DEV_UNKNOWN, DEV_SIZE) ) {
dip->di_device = Malloc(dip, (DEV_SIZE + 1) );
(void) strncpy (dip->di_device, devgp->device, DEV_SIZE);
} else if ( NEL (devgp->dev_name, DEV_UNKNOWN, DEV_SIZE) ) {
dip->di_device = Malloc(dip, (DEV_SIZE + 1) );
(void) strncpy (dip->di_device, devgp->dev_name, DEV_SIZE);
}
if (dip->di_device) {
/*
* In Steel, device names have trailing spaces. grrr!
*/
for (i = (DEV_SIZE); i--; ) {
if ( isspace(dip->di_device[i]) ) {
dip->di_device[i] = '\0';
} else {
break;
}
}
}
}
/*
* Setup the device type based on the category.
*/
switch (category) {
case DEV_TAPE: /* Tape category. */
dip->di_dtype = setup_device_type("tape");
break;
case DEV_DISK: { /* Disk category. */
/*
* If using partition 'c', setup to use the whole capacity.
*/
if (dip->di_dname[strlen(dip->di_dname)-1] == 'c') {
if ( (dip->di_max_capacity == False) && !dip->di_user_capacity ) {
dip->di_max_capacity = True;
}
}
/****************************************************************
* Attempt to get disk attributes using DEVGETINFO first, since *
* for SCSI disks we get more information, which we plan to use *
* one day, and we also get the real block (sector) size. *
****************************************************************/
if (devip && (devip->version == VERSION_1) ) {
v1_disk_dev_info_t *diskinfo;
diskinfo = &devip->v1.devinfo.disk;
dip->di_rdsize = diskinfo->blocksz;
if (!dip->di_dsize) dip->di_dsize = dip->di_rdsize;
/*
* NOTE: capacity is whole disk, not the open partition,
* so we don't use it unless selected by the user.
*/
if ( (dip->di_max_capacity == False) && !dip->di_user_capacity ) {
dip->di_capacity = diskinfo->capacity;
dip->di_user_capacity = (dip->di_capacity * (large_t)dip->di_rdsize);
if (dip->di_debug_flag) {
Printf(dip, "DEVGETINFO Capacity: " LUF " blocks.\n", dip->di_capacity);
}
}
if (dip->di_dsize && !dip->di_user_lbsize && !dip->di_lbdata_size) {
dip->di_lbdata_size = dip->di_dsize;
}
} else {
(void)SetupDiskAttributes(dip, fd);
}
dip->di_dtype = setup_device_type("disk");
/*
* TODO: Need to read disklabel to pickup partition sizes,
* and to check for mounted file systems. More work!
*/
break;
}
case DEV_SPECIAL: /* Special category. */
/*
* On Tru64 Unix, LSM volumes are really disks!
*/
if (SetupDiskAttributes(dip, fd) != SUCCESS) {
dip->di_dtype = setup_device_type("special");
}
break;
default:
break;
}
if (temp_fd) (void)close(fd);
return;
}
/*
* SetupDiskAttributes() - Setup Disk Attributes using DEVGETGEOM.
*
* Description:
* This function is used for disk devices which don't support
* the newer DEVGETINFO IOCTL, like LSM devices.
*
* Inputs:
* dip = The device information pointer.
* fd = The file descriptor (NoFd == Not open).
*
* Outputs:
* Returns 0 or -1 for Success/Failure.
*/
int
SetupDiskAttributes (struct dinfo *dip, int fd)
{
int status;
hbool_t temp_fd = False;
DEVGEOMST devgeom;
if (fd == NoFd) {
temp_fd = True;
if ( (fd = open (dip->di_dname, O_RDONLY)) < 0) {
return (FAILURE);
}
}
/*
* If using partition 'c', setup to use the whole capacity.
*
* Note: Only setup maximum capacity for random I/O, or else
* we will inhibit End of Media (EOM) testing.
*/
if (dip->di_random_io || dip->di_slices) {
if ( (dip->di_device && EQ(dip->di_device,"LSM")) ||
(dip->di_dname[strlen(dip->di_dname)-1] == 'c') ) {
if ( (dip->di_max_capacity == False) && !dip->di_user_capacity ) {
dip->di_max_capacity = True;
}
}
}
/*
* Attempt to obtain the disk geometry. Works for LSM, etc.
*
* NOTE: DEVGETGEOM *fails* on read-only devices (shit!).
*/
memset(&devgeom, '\0', sizeof(devgeom));
if ((status = ioctl (fd, DEVGETGEOM, (char *) &devgeom)) == SUCCESS) {
dip->di_rdsize = devgeom.geom_info.sector_size;
if (!dip->di_dsize) dip->di_dsize = dip->di_rdsize;
/*
* NOTE: dev_size is whole disk, not the open partition,
* so we don't use it unless selected by the user.
*/
if ( (dip->di_max_capacity == False) && !dip->di_user_capacity ) {
dip->di_capacity = devgeom.geom_info.dev_size;
dip->di_user_capacity = (dip->di_capacity * (large_t)dip->di_rdsize);
if (dip->di_debug_flag) {
Printf(dip, "DEVGETGEOM Capacity: " LUF " blocks.\n", dip->di_capacity);
}
}
if (dip->di_dsize && !dip->di_user_lbsize && !dip->di_lbdata_size) {
dip->di_lbdata_size = dip->di_dsize;
}
dip->di_dtype = setup_device_type("disk");
}
/*
* TODO: Need to read disklabel to pickup partition sizes,
* and to check for mounted file systems. More work!
*/
if (temp_fd) (void)close(fd);
return (status);
}
#endif /* defined(DEC) */
#if defined(HP_UX)
#include <sys/diskio.h>
#include <sys/scsi.h>
static int get_queue_depth(dinfo_t *dip, int fd, unsigned int *qdepth);
static int set_queue_depth(dinfo_t *dip, int fd, unsigned int qdepth);
void
os_system_device_info(struct dinfo *dip)
{
disk_describe_type disk_type, *disktp = &disk_type;
union inquiry_data inquiry;
int fd = dip->di_fd;
hbool_t temp_fd = False;
short category;
int i;
if (fd == NoFd) {
temp_fd = True;
if ( (fd = open (dip->di_dname, (O_RDONLY | O_NDELAY))) < 0) {
return;
}
}
/*
* Attempt to obtain the device information.
*/
memset(disktp, '\0', sizeof(*disktp));
if (ioctl (fd, DIOC_DESCRIBE, disktp) == SUCCESS) {
if (disktp->dev_type != UNKNOWN_DEV_TYPE) {
size_t size = sizeof(disktp->model_num);
dip->di_device = Malloc(dip, (size + 1) );
(void) strncpy (dip->di_device, disktp->model_num, size);
/*
* Strip trailing spaces from the device name.
*/
for (i = size; i--; ) {
if ( isspace(dip->di_device[i]) ) {
dip->di_device[i] = '\0';
} else {
break;
}
}
dip->di_rdsize = disktp->lgblksz;
if (!dip->di_dsize) dip->di_dsize = dip->di_rdsize;
if ( (dip->di_max_capacity == False) && !dip->di_user_capacity ) {
dip->di_max_capacity = True;
dip->di_capacity = (disktp->maxsva + 1);
dip->di_user_capacity = (dip->di_capacity * (large_t)dip->di_rdsize);
if (dip->di_debug_flag) {
Printf(dip, "DIOC_DESCRIBE Capacity: " LUF " blocks (%u byte blocks).\n",
dip->di_capacity, dip->di_rdsize);
}
}
}
switch (disktp->dev_type) {
case CDROM_DEV_TYPE: /* CDROM device */
case DISK_DEV_TYPE: /* Disk device */
case WORM_DEV_TYPE: /* Write once read many optical device */
case MO_DEV_TYPE: /* Magneto Optical device */
dip->di_dtype = setup_device_type("disk");
if (dip->di_qdepth != 0xFFFFFFFF) {
(void)set_queue_depth(dip, fd, dip->di_qdepth);
}
break;
case CTD_DEV_TYPE: /* Cartridge tape device */
dip->di_dtype = setup_device_type("tape");
break;
default:
break;
}
} else if (ioctl (fd, SIOC_INQUIRY, &inquiry) == SUCCESS) {
struct inquiry_2 *inq = (struct inquiry_2 *)&inquiry;
size_t size = sizeof(inq->product_id);
if (dip->di_debug_flag) {
Printf(dip, "SIOC_INQUIRY device type %u\n", inq->dev_type);
}
dip->di_device = Malloc(dip, (size + 1) );
(void) strncpy (dip->di_device, inq->product_id, size);
for (i = size; i--; ) {
if ( isspace(dip->di_device[i]) ) {
dip->di_device[i] = '\0';
} else {
break;
}
}
switch (inq->dev_type) {
case SCSI_DIRECT_ACCESS:
case SCSI_WORM:
case SCSI_CDROM:
case SCSI_MO:
dip->di_dtype = setup_device_type("disk");
if (dip->di_qdepth != 0xFFFFFFFF) {
(void)set_queue_depth(dip, fd, dip->di_qdepth);
}
break;
case SCSI_SEQUENTIAL_ACCESS:
dip->di_dtype = setup_device_type("tape");
break;
default:
break;
}
}
if (temp_fd) (void)close(fd);
return;
}
static int
get_queue_depth(dinfo_t *dip, int fd, unsigned int *qdepth)
{
struct sioc_lun_limits lun_limits;
int status;
(void)memset(&lun_limits, '\0', sizeof(lun_limits));
if ( (status = ioctl(fd, SIOC_GET_LUN_LIMITS, &lun_limits)) < 0) {
if (dip->di_debug_flag) {
perror("SIOC_SET_LUN_LIMITS failed");
}
} else {
*qdepth = lun_limits.max_q_depth;
}
return (status);
}
static int
set_queue_depth(dinfo_t *dip, int fd, unsigned int qdepth)
{
struct sioc_lun_limits lun_limits;
int status;
if (dip->di_debug_flag) {
unsigned int qd;
if (get_queue_depth (dip, fd, &qd) == 0) {
Printf(dip, "Current queue depth is %u\n", qd);
}
}
(void)memset(&lun_limits, '\0', sizeof(lun_limits));
lun_limits.max_q_depth = qdepth;
/*
* For performance testing, allow disabling tags.
*/
if (qdepth == 0) {
#if defined(SCTL_DISABLE_TAGS)
lun_limits.flags = SCTL_DISABLE_TAGS;
#else /* !defined(SCTL_DISABLE_TAGS) */
lun_limits.flags = 0;
#endif /* defined(SCTL_DISABLE_TAGS) */
} else {
lun_limits.flags = SCTL_ENABLE_TAGS;
}
if ( (status = ioctl(fd, SIOC_SET_LUN_LIMITS, &lun_limits)) < 0) {
if (dip->di_debug_flag) {
perror("SIOC_SET_LUN_LIMITS failed");
}
} else if (dip->di_debug_flag) {
Printf(dip, "Queue depth set to %u\n", qdepth);
}
return (status);
}
#endif /* defined(HP_UX) */
#if defined(__linux__)
/* Ugly stuff to avoid conflict with Linux BLOCK_SIZE definition. */
#undef BLOCK_SIZE
#include <linux/fs.h>
#undef BLOCK_SIZE
#define BLOCK_SIZE 512
void
os_system_device_info(struct dinfo *dip)
{
int fd = dip->di_fd;
hbool_t temp_fd = False;
unsigned long nr_sects;
int sect_size;
if (fd == NoFd) {
temp_fd = True;
if ( (fd = open (dip->di_dname, (O_RDONLY | O_NDELAY))) < 0) {
return;
}
}
/*
* Try to obtain the sector size.
*/
if (ioctl (fd, BLKSSZGET, §_size) == SUCCESS) {
dip->di_rdsize = sect_size;
if (!dip->di_dsize) dip->di_dsize = dip->di_rdsize;
if (dip->di_debug_flag) {
Printf(dip, "BLKSSZGET Sector Size: %u bytes\n", dip->di_rdsize);
}
dip->di_dtype = setup_device_type("disk");
}
/*
* If this IOCTL succeeds, we will assume it's a disk device.
*
* Note: The size returned is for the partition (thank-you!).
*/
if (ioctl (fd, BLKGETSIZE, &nr_sects) == SUCCESS) {
if (!dip->di_rdsize) dip->di_rdsize = BLOCK_SIZE;
if ( (dip->di_max_capacity == False) && !dip->di_user_capacity ) {
dip->di_max_capacity = True;
dip->di_capacity = nr_sects;
dip->di_user_capacity = (dip->di_capacity * (large_t)BLOCK_SIZE);
if (dip->di_debug_flag || (dip->di_capacity == 0) ) {
Printf(dip, "BLKGETSIZE Capacity: " LUF " blocks (%u byte blocks).\n",
dip->di_capacity, BLOCK_SIZE);
}
}
}
if (temp_fd) (void)close(fd);
return;
}
/* TODO: Move OS functions to OS file and decouple from dt. */
/* Note: This is a duplication of above, but does not set device type. */
/* This is being called for mounted file systems to get device size. */
void
os_get_block_size(dinfo_t *dip, int fd, char *device_name)
{
hbool_t temp_fd = False;
int sect_size;
if (fd == NoFd) {
temp_fd = True;
/* Note: Only works when running as root, of course! */
if ( (fd = open(device_name, (O_RDONLY | O_NDELAY))) < 0) {
if (dip->di_debug_flag) {
os_error_t error = os_get_error();
INIT_ERROR_INFO(eip, device_name, OS_OPEN_FILE_OP, OPEN_OP,
NULL, 0, (Offset_t)0, (size_t)0,
error, logLevelWarn, PRT_NOFLAGS,
(RPT_NORETRYS|RPT_NODEVINFO|RPT_NOERRORNUM|RPT_NOHISTORY|RPT_NOXERRORS));
(void)ReportRetryableError(dip, eip, "Failed to open file %s", device_name);
}
return;
}
}
/*
* Try to obtain the sector size. (actually works with some file systems)
*/
if (ioctl (fd, BLKSSZGET, §_size) == SUCCESS) {
dip->di_rdsize = sect_size;
/* Note: The device size may have been set by user device size! */
if (!dip->di_dsize) dip->di_dsize = dip->di_rdsize;
//dip->di_dsize = dip->di_rdsize; /* override previous dsize! */
if (dip->di_debug_flag) {
Printf(dip, "BLKSSZGET Sector Size: %u bytes\n", dip->di_rdsize);
}
}
if (temp_fd) (void)close(fd);
return;
}
#endif /* defined(__linux__) */
#if defined(WIN32)
void
os_system_device_info(struct dinfo *dip)
{
HANDLE fd;
STORAGE_DEVICE_NUMBER sdn;
PSTORAGE_DEVICE_NUMBER psdn = &sdn;
DWORD count;
if ( (fd = dip->di_fd) == INVALID_HANDLE_VALUE) {
fd = CreateFile(dip->di_dname, GENERIC_READ, (FILE_SHARE_READ|FILE_SHARE_WRITE), NULL, OPEN_EXISTING, 0, NULL);
if (fd == INVALID_HANDLE_VALUE) return;
}
if (!DeviceIoControl(fd, IOCTL_STORAGE_GET_DEVICE_NUMBER, NULL, 0, psdn, sizeof(*psdn), &count, NULL)) {
if (dip->di_fd == INVALID_HANDLE_VALUE) {
(void)CloseHandle(fd);
}
return;
}
switch (psdn->DeviceType) {
case FILE_DEVICE_DISK: {
DISK_GEOMETRY_EX dg;
PDISK_GEOMETRY_EX pdg = &dg;
if (dip->di_debug_flag) {
Printf(dip, "Device type is %d (FILE_DEVICE_DISK)\n", psdn->DeviceType);
}
if (DeviceIoControl(fd, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX, NULL, 0, pdg, sizeof(*pdg), &count, NULL)) {
dip->di_rdsize = pdg->Geometry.BytesPerSector;
if (!dip->di_dsize) dip->di_dsize = dip->di_rdsize;
if ( (dip->di_max_capacity == False) && !dip->di_user_capacity ) {
dip->di_max_capacity = True;
dip->di_capacity = (large_t)pdg->DiskSize.QuadPart;
dip->di_capacity /= dip->di_rdsize; /* Capacity in blocks. */
dip->di_user_capacity = (large_t)pdg->DiskSize.QuadPart; /* In bytes! */
if (dip->di_debug_flag) {
Printf(dip, "DISK_GEOMETRY_EX Capacity: " LUF " blocks (%u byte blocks).\n",
dip->di_capacity, dip->di_rdsize);
}
}
}
dip->di_random_access = True;
dip->di_dtype = setup_device_type("disk");
setup_device_defaults(dip);
break;
}
default:
if (dip->di_debug_flag) {
Printf(dip, "Device type is %d, no special setup performed!\n", psdn->DeviceType);
}
break;
}
if (dip->di_fd == INVALID_HANDLE_VALUE) {
(void)CloseHandle(fd);
}
return;
}
#endif /* defined(WIN32) */
/*
* setup_device_info() - Setup Initial Device Information.
*
* Description:
* This function allocates a device information entry, and does
* the initial setup of certain information based on known options.
* This function is meant to be called prior to opening the device so
* test specific functions are known for initial processing.
*
* Inputs:
* dip = The device information pointer.
* dname = The device name.
* dtp = The device type pointer.
*
* Outputs:
* Various device information is initialized, including the device type.
*
* Return Value:
* Returns SUCCESS/FAILURE
*/
int
setup_device_info(struct dinfo *dip, char *dname, struct dtype *dtp)
{
#if !defined(WIN32)