-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathzvolIO.cpp
1370 lines (1109 loc) · 32.5 KB
/
zvolIO.cpp
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
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2013-2020, Jorgen Lundman. All rights reserved.
*/
#include <sys/types.h>
#include <IOKit/IOLib.h>
#include <IOKit/IOBSD.h>
#include <IOKit/IOKitKeys.h>
#include <IOKit/storage/IOBlockStorageDevice.h>
#include <IOKit/storage/IOBlockStorageDriver.h>
#include <IOKit/storage/IOMedia.h>
#include <IOKit/storage/IOStorageProtocolCharacteristics.h>
#include <sys/zfs_ioctl.h>
#include <sys/zfs_znode.h>
#include <sys/dataset_kstats.h>
#include <sys/zvol.h>
#include <sys/zvol_os.h>
#include <sys/zfs_boot.h>
#include <sys/spa_impl.h>
#include <sys/ZFSPool.h>
#include <sys/zvolIO.h>
/*
* ZVOL Device
*/
// Define the superclass
#define super IOBlockStorageDevice
#define ZVOL_BSIZE DEV_BSIZE
static const char *ZVOL_PRODUCT_NAME_PREFIX = "ZVOL ";
/* Wrapper for zvol_state pointer to IOKit device */
typedef struct zvol_iokit {
org_openzfsonosx_zfs_zvol_device *dev;
} zvol_iokit_t;
OSDefineMetaClassAndStructors(org_openzfsonosx_zfs_zvol_device,
IOBlockStorageDevice)
bool
org_openzfsonosx_zfs_zvol_device::init(zvol_state_t *c_zv,
OSDictionary *properties)
{
zvol_iokit_t *iokitdev = NULL;
dprintf("zvolIO_device:init\n");
if (!c_zv || c_zv->zv_zso->zvo_iokitdev != NULL) {
dprintf("zvol %s invalid c_zv\n", __func__);
return (false);
}
if ((iokitdev = (zvol_iokit_t *)kmem_alloc(sizeof (zvol_iokit_t),
KM_SLEEP)) == NULL) {
printf("zvol %s wrapper alloc failed\n", __func__);
return (false);
}
if (super::init(properties) == false) {
printf("zvol %s super init failed\n", __func__);
kmem_free(iokitdev, sizeof (zvol_iokit_t));
return (false);
}
/* Store reference to zvol_state_t in the iokitdev */
zv = c_zv;
/* Store reference to iokitdev in zvol_state_t */
iokitdev->dev = this;
/* Assign to zv once completely initialized */
zv->zv_zso->zvo_iokitdev = iokitdev;
/* Apply the name from the full dataset path */
if (strlen(zv->zv_name) != 0) {
setName(zv->zv_name);
}
return (true);
}
bool
org_openzfsonosx_zfs_zvol_device::attach(IOService* provider)
{
OSDictionary *protocolCharacteristics = 0;
OSDictionary *deviceCharacteristics = 0;
OSDictionary *storageFeatures = 0;
OSBoolean *unmapFeature = 0;
const OSSymbol *propSymbol = 0;
OSString *dataString = 0;
OSNumber *dataNumber = 0;
char product_name[strlen(ZVOL_PRODUCT_NAME_PREFIX) + MAXPATHLEN + 1];
if (!provider) {
dprintf("ZVOL attach missing provider\n");
return (false);
}
if (super::attach(provider) == false)
return (false);
/*
* We want to set some additional properties for ZVOLs, in
* particular, "Virtual Device", and type "File"
* (or is Internal better?)
*
* Finally "Generic" type.
*
* These properties are defined in *protocol* characteristics
*/
protocolCharacteristics = OSDictionary::withCapacity(3);
if (!protocolCharacteristics) {
IOLog("failed to create dict for protocolCharacteristics.\n");
return (true);
}
propSymbol = OSSymbol::withCString(
kIOPropertyPhysicalInterconnectTypeVirtual);
if (!propSymbol) {
IOLog("could not create interconnect type string\n");
return (true);
}
protocolCharacteristics->setObject(
kIOPropertyPhysicalInterconnectTypeKey, propSymbol);
propSymbol->release();
propSymbol = 0;
propSymbol = OSSymbol::withCString(kIOPropertyInterconnectFileKey);
if (!propSymbol) {
IOLog("could not create interconnect location string\n");
return (true);
}
protocolCharacteristics->setObject(
kIOPropertyPhysicalInterconnectLocationKey, propSymbol);
propSymbol->release();
propSymbol = 0;
setProperty(kIOPropertyProtocolCharacteristicsKey,
protocolCharacteristics);
protocolCharacteristics->release();
protocolCharacteristics = 0;
/*
* We want to set some additional properties for ZVOLs, in
* particular, physical block size (volblocksize) of the
* underlying ZVOL, and 'logical' block size presented by
* the virtual disk. Also set physical bytes per sector.
*
* These properties are defined in *device* characteristics
*/
deviceCharacteristics = OSDictionary::withCapacity(3);
if (!deviceCharacteristics) {
IOLog("failed to create dict for deviceCharacteristics.\n");
return (true);
}
/* Set this device to be an SSD, for priority and VM paging */
propSymbol = OSSymbol::withCString(
kIOPropertyMediumTypeSolidStateKey);
if (!propSymbol) {
IOLog("could not create medium type string\n");
return (true);
}
deviceCharacteristics->setObject(kIOPropertyMediumTypeKey,
propSymbol);
propSymbol->release();
propSymbol = 0;
/* Set logical block size to ZVOL_BSIZE (512b) */
dataNumber = OSNumber::withNumber(ZVOL_BSIZE,
8 * sizeof (ZVOL_BSIZE));
deviceCharacteristics->setObject(kIOPropertyLogicalBlockSizeKey,
dataNumber);
dprintf("logicalBlockSize %llu\n",
dataNumber->unsigned64BitValue());
dataNumber->release();
dataNumber = 0;
/* Set physical block size to match volblocksize property */
dataNumber = OSNumber::withNumber(zv->zv_volblocksize,
8 * sizeof (zv->zv_volblocksize));
deviceCharacteristics->setObject(kIOPropertyPhysicalBlockSizeKey,
dataNumber);
dprintf("physicalBlockSize %llu\n",
dataNumber->unsigned64BitValue());
dataNumber->release();
dataNumber = 0;
/* Set physical bytes per sector to match volblocksize property */
dataNumber = OSNumber::withNumber((uint64_t)(zv->zv_volblocksize),
8 * sizeof (uint64_t));
deviceCharacteristics->setObject(kIOPropertyBytesPerPhysicalSectorKey,
dataNumber);
dprintf("physicalBytesPerSector %llu\n",
dataNumber->unsigned64BitValue());
dataNumber->release();
dataNumber = 0;
/* Publish the Device / Media name */
(void) snprintf(product_name, sizeof (product_name), "%s%s",
ZVOL_PRODUCT_NAME_PREFIX, zv->zv_name);
dataString = OSString::withCString(product_name);
deviceCharacteristics->setObject(kIOPropertyProductNameKey, dataString);
dataString->release();
dataString = 0;
/* Apply these characteristics */
setProperty(kIOPropertyDeviceCharacteristicsKey,
deviceCharacteristics);
deviceCharacteristics->release();
deviceCharacteristics = 0;
/*
* ZVOL unmap support
*
* These properties are defined in IOStorageFeatures
*/
storageFeatures = OSDictionary::withCapacity(1);
if (!storageFeatures) {
IOLog("failed to create dictionary for storageFeatures.\n");
return (true);
}
/* Set unmap feature */
unmapFeature = OSBoolean::withBoolean(true);
storageFeatures->setObject(kIOStorageFeatureUnmap, unmapFeature);
unmapFeature->release();
unmapFeature = 0;
/* Apply these storage features */
setProperty(kIOStorageFeaturesKey, storageFeatures);
storageFeatures->release();
storageFeatures = 0;
/*
* Set transfer limits:
*
* Maximum transfer size (bytes)
* Maximum transfer block count
* Maximum transfer block size (bytes)
* Maximum transfer segment count
* Maximum transfer segment size (bytes)
* Minimum transfer segment size (bytes)
*
* We will need to establish safe defaults for all / per volblocksize
*
* Example: setProperty(kIOMinimumSegmentAlignmentByteCountKey, 1, 1);
*/
/*
* Finally "Generic" type, set as a device property. Tried setting this
* to the string "ZVOL" however the OS does not recognize it as a block
* storage device. This would probably be possible by extending the
* IOBlockStorage Device / Driver relationship.
*/
setProperty(kIOBlockStorageDeviceTypeKey,
kIOBlockStorageDeviceTypeGeneric);
return (true);
}
int
org_openzfsonosx_zfs_zvol_device::renameDevice(void)
{
OSDictionary *deviceDict;
OSString *nameStr;
char *newstr;
int len;
/* Length of string and null terminating character */
len = strlen(ZVOL_PRODUCT_NAME_PREFIX) + strlen(zv->zv_name) + 1;
newstr = (char *)kmem_alloc(len, KM_SLEEP);
if (!newstr) {
dprintf("%s string alloc failed\n", __func__);
return (ENOMEM);
}
/* Append prefix and dsl name */
snprintf(newstr, len, "%s%s", ZVOL_PRODUCT_NAME_PREFIX, zv->zv_name);
nameStr = OSString::withCString(newstr);
kmem_free(newstr, len);
if (!nameStr) {
dprintf("%s couldn't allocate name string\n", __func__);
return (ENOMEM);
}
/* Fetch current device characteristics dictionary */
deviceDict = OSDynamicCast(OSDictionary,
getProperty(kIOPropertyDeviceCharacteristicsKey));
if (!deviceDict || (deviceDict =
OSDictionary::withDictionary(deviceDict)) == NULL) {
dprintf("couldn't clone device characteristics\n");
/* Allocate new dict */
if (!deviceDict &&
(deviceDict = OSDictionary::withCapacity(1)) == NULL) {
dprintf("%s OSDictionary alloc failed\n", __func__);
nameStr->release();
return (ENOMEM);
}
}
/* Add or replace the product name */
if (deviceDict->setObject(kIOPropertyProductNameKey,
nameStr) == false) {
dprintf("%s couldn't set product name\n", __func__);
nameStr->release();
deviceDict->release();
return (ENXIO);
}
nameStr->release();
nameStr = 0;
/* Set IORegistry property */
if (setProperty(kIOPropertyDeviceCharacteristicsKey,
deviceDict) == false) {
dprintf("%s couldn't set IORegistry property\n", __func__);
deviceDict->release();
return (ENXIO);
}
deviceDict->release();
deviceDict = 0;
/* Apply the name from the full dataset path */
setName(zv->zv_name);
return (0);
}
int
org_openzfsonosx_zfs_zvol_device::offlineDevice(void)
{
IOService *client;
if ((client = this->getClient()) == NULL) {
return (ENOENT);
}
/* Ask IOBlockStorageDevice to offline media */
if (client->message(kIOMessageMediaStateHasChanged,
this, (void *)kIOMediaStateOffline) != kIOReturnSuccess) {
dprintf("%s failed\n", __func__);
return (ENXIO);
}
return (0);
}
int
org_openzfsonosx_zfs_zvol_device::onlineDevice(void)
{
IOService *client;
if ((client = this->getClient()) == NULL) {
return (ENOENT);
}
/* Ask IOBlockStorageDevice to online media */
if (client->message(kIOMessageMediaStateHasChanged,
this, (void *)kIOMediaStateOnline) != kIOReturnSuccess) {
dprintf("%s failed\n", __func__);
return (ENXIO);
}
return (0);
}
int
org_openzfsonosx_zfs_zvol_device::refreshDevice(void)
{
IOService *client;
if ((client = this->getClient()) == NULL) {
return (ENOENT);
}
/* Ask IOBlockStorageDevice to reset the media params */
if (client->message(kIOMessageMediaParametersHaveChanged,
this) != kIOReturnSuccess) {
dprintf("%s failed\n", __func__);
return (ENXIO);
}
return (0);
}
int
org_openzfsonosx_zfs_zvol_device::getBSDName(void)
{
IORegistryEntry *ioregdevice = 0;
OSObject *bsdnameosobj = 0;
OSString* bsdnameosstr = 0;
ioregdevice = OSDynamicCast(IORegistryEntry, this);
if (!ioregdevice)
return (-1);
bsdnameosobj = ioregdevice->getProperty(kIOBSDNameKey,
gIOServicePlane, kIORegistryIterateRecursively);
if (!bsdnameosobj)
return (-1);
bsdnameosstr = OSDynamicCast(OSString, bsdnameosobj);
IOLog("zvol: bsd name is '%s'\n",
bsdnameosstr->getCStringNoCopy());
if (!zv)
return (-1);
zv->zv_zso->zvo_bsdname[0] = 'r'; // for 'rdiskX'.
strlcpy(&zv->zv_zso->zvo_bsdname[1],
bsdnameosstr->getCStringNoCopy(),
sizeof (zv->zv_zso->zvo_bsdname)-1);
/*
* IOLog("name assigned '%s'\n", zv->zv_zso->zvo_bsdname);
*/
return (0);
}
void
org_openzfsonosx_zfs_zvol_device::detach(IOService *provider)
{
super::detach(provider);
}
void
org_openzfsonosx_zfs_zvol_device::clearState(void)
{
zv = NULL;
}
bool
org_openzfsonosx_zfs_zvol_device::handleOpen(IOService *client,
IOOptionBits options, void *argument)
{
IOStorageAccess access = (uintptr_t)argument;
bool ret = false;
int openflags = 0;
if (super::handleOpen(client, options, argument) == false)
return (false);
/* Device terminating? */
if (zv == NULL ||
zv->zv_zso == NULL ||
zv->zv_zso->zvo_iokitdev == NULL)
return (false);
if (access & kIOStorageAccessReaderWriter) {
openflags = FWRITE | ZVOL_EXCL;
} else {
openflags = FREAD;
}
/*
* Don't use 'zv' until it has been verified by zvol_os_open_zv()
* and returned as opened, then it holds an open count and can be
* used.
*/
if (zvol_os_open_zv(zv, zv->zv_zso->zvo_openflags, 0, NULL) == 0) {
ret = true;
}
if (ret)
zv->zv_zso->zvo_openflags = openflags;
dprintf("Open %s (openflags %llx)\n", (ret ? "done" : "failed"),
ret ? zv->zv_zso->zvo_openflags : 0);
if (ret == false)
super::handleClose(client, options);
return (ret);
}
void
org_openzfsonosx_zfs_zvol_device::handleClose(IOService *client,
IOOptionBits options)
{
super::handleClose(client, options);
/* Terminating ? */
if (zv == NULL ||
zv->zv_zso == NULL ||
zv->zv_zso->zvo_iokitdev == NULL)
return;
zvol_os_close_zv(zv, zv->zv_zso->zvo_openflags, 0, NULL);
}
IOReturn
org_openzfsonosx_zfs_zvol_device::doAsyncReadWrite(
IOMemoryDescriptor *buffer, UInt64 block, UInt64 nblks,
IOStorageAttributes *attributes, IOStorageCompletion *completion)
{
IODirection direction;
IOByteCount actualByteCount;
struct iomem iomem;
iomem.buf = NULL;
// Return errors for incoming I/O if we have been terminated.
if (isInactive() == true) {
dprintf("asyncReadWrite notActive fail\n");
return (kIOReturnNotAttached);
}
// These variables are set in zvol_first_open(), which should have been
// called already.
if (!zv->zv_dn) {
dprintf("asyncReadWrite no zvol dnode\n");
return (kIOReturnNotAttached);
}
// Ensure the start block is within the disk capacity.
if ((block)*(ZVOL_BSIZE) >= zv->zv_volsize) {
dprintf("asyncReadWrite start block outside volume\n");
return (kIOReturnBadArgument);
}
// Shorten the read, if beyond the end
if (((block + nblks)*(ZVOL_BSIZE)) > zv->zv_volsize) {
dprintf("asyncReadWrite block shortening needed\n");
return (kIOReturnBadArgument);
}
// Get the buffer direction, whether this is a read or a write.
direction = buffer->getDirection();
if ((direction != kIODirectionIn) && (direction != kIODirectionOut)) {
dprintf("asyncReadWrite kooky direction\n");
return (kIOReturnBadArgument);
}
// dprintf("%s offset @block %llu numblocks %llu: blksz %u\n",
// direction == kIODirectionIn ? "Read" : "Write",
// block, nblks, (ZVOL_BSIZE));
/* Perform the read or write operation through the transport driver. */
actualByteCount = (nblks*(ZVOL_BSIZE));
iomem.buf = buffer;
/* Make sure we don't go away while the command is being executed */
/* Open should be holding a retain */
if (direction == kIODirectionIn) {
if (zvol_os_read_zv(zv, (block*(ZVOL_BSIZE)),
actualByteCount, &iomem)) {
actualByteCount = 0;
}
} else {
if (zvol_os_write_zv(zv, (block*(ZVOL_BSIZE)),
actualByteCount, &iomem)) {
actualByteCount = 0;
}
}
/* Open should be holding a retain */
iomem.buf = NULL; // overkill
if (actualByteCount != nblks*(ZVOL_BSIZE))
dprintf("Read/Write operation failed\n");
// Call the completion function.
(completion->action)(completion->target, completion->parameter,
kIOReturnSuccess, actualByteCount);
return (kIOReturnSuccess);
}
IOReturn
org_openzfsonosx_zfs_zvol_device::doDiscard(UInt64 block, UInt64 nblks)
{
dprintf("doDiscard called with block, nblks (%llu, %llu)\n",
block, nblks);
uint64_t bytes = 0;
uint64_t off = 0;
/* Convert block/nblks to offset/bytes */
off = block * ZVOL_BSIZE;
bytes = nblks * ZVOL_BSIZE;
dprintf("calling zvol_unmap with offset, bytes (%llu, %llu)\n",
off, bytes);
if (zvol_os_unmap(zv, off, bytes) == 0)
return (kIOReturnSuccess);
else
return (kIOReturnError);
}
IOReturn
org_openzfsonosx_zfs_zvol_device::doUnmap(IOBlockStorageDeviceExtent *extents,
UInt32 extentsCount, UInt32 options = 0)
{
UInt32 i = 0;
IOReturn result;
dprintf("doUnmap called with (%u) extents and options (%u)\n",
(uint32_t)extentsCount, (uint32_t)options);
if (options > 0 || !extents) {
return (kIOReturnUnsupported);
}
for (i = 0; i < extentsCount; i++) {
result = doDiscard(extents[i].blockStart,
extents[i].blockCount);
if (result != kIOReturnSuccess) {
return (result);
}
}
return (kIOReturnSuccess);
}
UInt32
org_openzfsonosx_zfs_zvol_device::doGetFormatCapacities(UInt64* capacities,
UInt32 capacitiesMaxCount) const
{
dprintf("formatCap\n");
/*
* Ensure that the array is sufficient to hold all our formats
* (we require one element).
*/
if ((capacities != NULL) && (capacitiesMaxCount < 1))
return (0);
/* Error, return an array size of 0. */
/*
* The caller may provide a NULL array if it wishes to query the number
* of formats that we support.
*/
if (capacities != NULL)
capacities[0] = zv->zv_volsize;
dprintf("returning capacity[0] size %llu\n", zv->zv_volsize);
return (1);
}
char *
org_openzfsonosx_zfs_zvol_device::getProductString(void)
{
dprintf("getProduct %p\n", zv);
if (zv)
return (zv->zv_name);
return ((char *)"ZVolume");
}
IOReturn
org_openzfsonosx_zfs_zvol_device::reportBlockSize(UInt64 *blockSize)
{
dprintf("reportBlockSize %llu\n", *blockSize);
if (blockSize) *blockSize = (ZVOL_BSIZE);
return (kIOReturnSuccess);
}
IOReturn
org_openzfsonosx_zfs_zvol_device::reportMaxValidBlock(UInt64 *maxBlock)
{
dprintf("reportMaxValidBlock %llu\n", *maxBlock);
if (maxBlock) *maxBlock = ((zv->zv_volsize / (ZVOL_BSIZE)) - 1);
return (kIOReturnSuccess);
}
IOReturn
org_openzfsonosx_zfs_zvol_device::reportMediaState(bool *mediaPresent,
bool *changedState)
{
dprintf("reportMediaState\n");
if (mediaPresent) *mediaPresent = true;
if (changedState) *changedState = false;
return (kIOReturnSuccess);
}
IOReturn
org_openzfsonosx_zfs_zvol_device::reportPollRequirements(bool *pollRequired,
bool *pollIsExpensive)
{
dprintf("reportPollReq\n");
if (pollRequired) *pollRequired = false;
if (pollIsExpensive) *pollIsExpensive = false;
return (kIOReturnSuccess);
}
IOReturn
org_openzfsonosx_zfs_zvol_device::reportRemovability(bool *isRemovable)
{
dprintf("reportRemova\n");
if (isRemovable) *isRemovable = false;
return (kIOReturnSuccess);
}
IOReturn
org_openzfsonosx_zfs_zvol_device::doEjectMedia(void)
{
dprintf("ejectMedia\n");
/* XXX */
// Only 10.6 needs special work to eject
// if ((version_major == 10) && (version_minor == 8))
// destroyBlockStorageDevice(zvol);
// }
return (kIOReturnError);
return (kIOReturnSuccess);
}
IOReturn
org_openzfsonosx_zfs_zvol_device::doFormatMedia(UInt64 byteCapacity)
{
dprintf("doFormat\n");
return (kIOReturnSuccess);
}
IOReturn
org_openzfsonosx_zfs_zvol_device::doLockUnlockMedia(bool doLock)
{
dprintf("doLockUnlock\n");
return (kIOReturnSuccess);
}
IOReturn
org_openzfsonosx_zfs_zvol_device::doSynchronizeCache(void)
{
dprintf("doSync\n");
if (zv && zv->zv_zilog) {
zil_commit(zv->zv_zilog, ZVOL_OBJ);
}
return (kIOReturnSuccess);
}
char *
org_openzfsonosx_zfs_zvol_device::getVendorString(void)
{
dprintf("getVendor\n");
return ((char *)"ZVOL");
}
char *
org_openzfsonosx_zfs_zvol_device::getRevisionString(void)
{
dprintf("getRevision\n");
return ((char *)ZFS_META_VERSION);
}
char *
org_openzfsonosx_zfs_zvol_device::getAdditionalDeviceInfoString(void)
{
dprintf("getAdditional\n");
return ((char *)"ZFS Volume");
}
IOReturn
org_openzfsonosx_zfs_zvol_device::reportEjectability(bool *isEjectable)
{
dprintf("reportEjecta\n");
/*
* Which do we prefer? If you eject it, you can't get volume back until
* you import it again.
*/
if (isEjectable) *isEjectable = false;
return (kIOReturnSuccess);
}
/* XXX deprecated function */
IOReturn
org_openzfsonosx_zfs_zvol_device::reportLockability(bool *isLockable)
{
dprintf("reportLocka\n");
if (isLockable) *isLockable = true;
return (kIOReturnSuccess);
}
IOReturn
org_openzfsonosx_zfs_zvol_device::reportWriteProtection(bool *isWriteProtected)
{
dprintf("reportWritePro: %d\n", *isWriteProtected);
if (!isWriteProtected)
return (kIOReturnSuccess);
if (zv && (zv->zv_flags & ZVOL_RDONLY))
*isWriteProtected = true;
else
*isWriteProtected = false;
return (kIOReturnSuccess);
}
IOReturn
org_openzfsonosx_zfs_zvol_device::getWriteCacheState(bool *enabled)
{
dprintf("getCacheState\n");
if (enabled) *enabled = true;
return (kIOReturnSuccess);
}
IOReturn
org_openzfsonosx_zfs_zvol_device::setWriteCacheState(bool enabled)
{
dprintf("setWriteCache\n");
return (kIOReturnSuccess);
}
extern "C" {
/* C interfaces */
int
zvolCreateNewDevice(zvol_state_t *zv)
{
org_openzfsonosx_zfs_zvol_device *zvol;
ZFSPool *pool_proxy;
spa_t *spa;
dprintf("%s\n", __func__);
/* We must have a valid zvol_state_t */
if (!zv || !zv->zv_objset) {
dprintf("%s missing zv or objset\n", __func__);
return (EINVAL);
}
/* We need the spa to get the pool proxy */
if ((spa = dmu_objset_spa(zv->zv_objset)) == NULL) {
dprintf("%s couldn't get spa\n", __func__);
return (EINVAL);
}
if (spa->spa_iokit_proxy == NULL ||
(pool_proxy = spa->spa_iokit_proxy->proxy) == NULL) {
dprintf("%s missing IOKit pool proxy\n", __func__);
return (EINVAL);
}
zvol = new org_openzfsonosx_zfs_zvol_device;
/* Validate creation, initialize and attach */
if (!zvol || zvol->init(zv) == false ||
zvol->attach(pool_proxy) == false) {
dprintf("%s device creation failed\n", __func__);
if (zvol) zvol->release();
return (ENOMEM);
}
/* Start the service */
if (zvol->start(pool_proxy) == false) {
dprintf("%s device start failed\n", __func__);
zvol->detach(pool_proxy);
zvol->release();
return (ENXIO);
}
/* Open pool_proxy provider */
if (pool_proxy->open(zvol) == false) {
dprintf("%s open provider failed\n", __func__);
zvol->stop(pool_proxy);
zvol->detach(pool_proxy);
zvol->release();
return (ENXIO);
}
/* Is retained by provider */
zvol->release();
zvol = 0;
return (0);
}
int
zvolRegisterDevice(zvol_state_t *zv)
{
org_openzfsonosx_zfs_zvol_device *zvol;
OSDictionary *matching;
IOService *service = 0;
IOMedia *media = 0;
OSString *nameStr = 0, *bsdName = 0;
uint64_t timeout = (5ULL * kSecondScale);
bool ret = false;
if (!zv || !zv->zv_zso->zvo_iokitdev || zv->zv_name[0] == 0) {
dprintf("%s missing zv, iokitdev, or name\n", __func__);
return (EINVAL);
}
if ((zvol = zv->zv_zso->zvo_iokitdev->dev) == NULL) {
dprintf("%s couldn't get zvol device\n", __func__);
return (EINVAL);
}
if (!zvol->getVendorString()) {
return (EINVAL);
}
/* Create matching string and dictionary */
{
char str[MAXNAMELEN];
snprintf(str, MAXNAMELEN, "%s %s Media",
zvol->getVendorString(), zv->zv_name);
if ((nameStr = OSString::withCString(str)) == NULL) {
dprintf("%s problem with name string\n", __func__);
return (ENOMEM);
}
}
matching = IOService::serviceMatching("IOMedia");
if (!matching || !matching->setObject(gIONameMatchKey, nameStr)) {
dprintf("%s couldn't get matching dictionary\n", __func__);
nameStr->release();
return (ENOMEM);
}
/* Register device for service matching */
zvol->registerService(kIOServiceAsynchronous);
/* Wait for upper layer BSD client */
dprintf("%s waiting for IOMedia\n", __func__);
/* Wait for up to 5 seconds */
service = IOService::waitForMatchingService(matching, timeout);
dprintf("%s %s service\n", __func__, (service ? "got" : "no"));
if (!service) {
dprintf("%s couldn't get matching service\n", __func__);
nameStr->release();
matching->release();
return (false);
}
dprintf("%s casting to IOMedia\n", __func__);
media = OSDynamicCast(IOMedia, service);
if (!media) {
dprintf("%s no IOMedia\n", __func__);
nameStr->release();
matching->release();