-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathzvol_os.c
1187 lines (955 loc) · 26.5 KB
/
zvol_os.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
/*
* 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) 2020 by Jorgen Lundman. All rights reserved.
*/
#include <sys/dataset_kstats.h>
#include <sys/disk.h>
#include <sys/dbuf.h>
#include <sys/dmu_traverse.h>
#include <sys/dsl_dataset.h>
#include <sys/dsl_prop.h>
#include <sys/dsl_dir.h>
#include <sys/zap.h>
#include <sys/zfeature.h>
#include <sys/zil_impl.h>
#include <sys/dmu_tx.h>
#include <sys/zio.h>
#include <sys/zfs_rlock.h>
#include <sys/spa_impl.h>
#include <sys/zvol.h>
#include <sys/zvol_impl.h>
#include <sys/zvol_os.h>
#include <sys/zvolIO.h>
#include <sys/fm/fs/zfs.h>
#include <libkern/OSDebug.h>
static uint32_t zvol_major = ZVOL_MAJOR;
unsigned int zvol_request_sync = 0;
unsigned int zvol_prefetch_bytes = (128 * 1024);
unsigned long zvol_max_discard_blocks = 16384;
unsigned int zvol_threads = 8;
taskq_t *zvol_taskq;
extern unsigned int spl_split_stack_below;
_Atomic unsigned int spl_lowest_zvol_stack_remaining = UINT_MAX;
typedef struct zv_request {
zvol_state_t *zv_zv;
union {
void (*zv_func)(zvol_state_t *, void *);
int (*zv_ifunc)(zvol_state_t *, void *);
};
void *zv_arg;
int zv_rv;
/* Used with zv_ifunc to wait for completion */
kmutex_t zv_lock;
kcondvar_t zv_cv;
taskq_ent_t zv_ent;
} zv_request_t;
#define ZVOL_LOCK_HELD (1<<0)
#define ZVOL_LOCK_SPA (1<<1)
#define ZVOL_LOCK_SUSPEND (1<<2)
static void
zvol_os_spawn_cb(void *param)
{
zv_request_t *zvr = (zv_request_t *)param;
zvr->zv_func(zvr->zv_zv, zvr->zv_arg);
kmem_free(zvr, sizeof (zv_request_t));
}
static void
zvol_os_spawn(zvol_state_t *zv,
void (*func)(zvol_state_t *, void *), void *arg)
{
zv_request_t *zvr;
zvr = kmem_alloc(sizeof (zv_request_t), KM_SLEEP);
zvr->zv_zv = zv;
zvr->zv_arg = arg;
zvr->zv_func = func;
taskq_init_ent(&zvr->zv_ent);
taskq_dispatch_ent(zvol_taskq,
zvol_os_spawn_cb, zvr, 0, &zvr->zv_ent);
}
static void
zvol_os_spawn_wait_cb(void *param)
{
zv_request_t *zvr = (zv_request_t *)param;
zvr->zv_rv = zvr->zv_ifunc(zvr->zv_zv, zvr->zv_arg);
zvr->zv_func = NULL;
mutex_enter(&zvr->zv_lock);
cv_broadcast(&zvr->zv_cv);
mutex_exit(&zvr->zv_lock);
}
static int
zvol_os_spawn_wait(zvol_state_t *zv,
int (*func)(zvol_state_t *, void *), void *arg)
{
int rv;
zv_request_t *zvr;
zvr = kmem_alloc(sizeof (zv_request_t), KM_SLEEP);
zvr->zv_zv = zv;
zvr->zv_arg = arg;
zvr->zv_ifunc = func;
taskq_init_ent(&zvr->zv_ent);
cv_init(&zvr->zv_cv, NULL, CV_DEFAULT, NULL);
mutex_init(&zvr->zv_lock, NULL, MUTEX_DEFAULT, NULL);
mutex_enter(&zvr->zv_lock);
taskq_dispatch_ent(zvol_taskq,
zvol_os_spawn_wait_cb, zvr, 0, &zvr->zv_ent);
/* Make sure it ran, by waiting */
cv_wait(&zvr->zv_cv, &zvr->zv_lock);
mutex_exit(&zvr->zv_lock);
mutex_destroy(&zvr->zv_lock);
cv_destroy(&zvr->zv_cv);
VERIFY3P(zvr->zv_ifunc, ==, NULL);
rv = zvr->zv_rv;
kmem_free(zvr, sizeof (zv_request_t));
return (rv);
}
/*
* Given a path, return TRUE if path is a ZVOL.
*/
static boolean_t
zvol_os_is_zvol(const char *device)
{
if (device == NULL)
return (B_FALSE);
return (zvol_os_is_zvol_impl(device));
}
/*
* Make sure zv is still in the list (not freed) and if it is
* grab the locks in the correct order.
* Can we rely on list_link_active() instead of looping list?
* Return value:
* 0 : not found. No locks.
* ZVOL_LOCK_HELD : found and zv->zv_state_lock held
* |ZVOL_LOCK_SPA : spa_namespace_lock held
* |ZVOL_LOCK_SUSPEND : zv->zv_state_lock held
* call zvol_os_verify_lock_exit() to release
*/
static int
zvol_os_verify_and_lock(zvol_state_t *node, boolean_t takesuspend)
{
zvol_state_t *zv;
int ret = ZVOL_LOCK_HELD;
retry:
rw_enter(&zvol_state_lock, RW_READER);
for (zv = list_head(&zvol_state_list); zv != NULL;
zv = list_next(&zvol_state_list, zv)) {
/* Until we find the node ... */
if (zv != node)
continue;
/* If this is to be first open, deal with spa_namespace */
if (zv->zv_open_count == 0 &&
!mutex_owned(&spa_namespace_lock)) {
/*
* We need to guarantee that the namespace lock is held
* to avoid spurious failures in zvol_first_open.
*/
ret |= ZVOL_LOCK_SPA;
if (!mutex_tryenter(&spa_namespace_lock)) {
rw_exit(&zvol_state_lock);
mutex_enter(&spa_namespace_lock);
/* Sadly, this will restart for loop */
goto retry;
}
}
mutex_enter(&zv->zv_state_lock);
/*
* make sure zvol is not suspended during first open
* (hold zv_suspend_lock) and respect proper lock acquisition
* ordering - zv_suspend_lock before zv_state_lock
*/
if (zv->zv_open_count == 0 || takesuspend) {
ret |= ZVOL_LOCK_SUSPEND;
if (!rw_tryenter(&zv->zv_suspend_lock, RW_READER)) {
mutex_exit(&zv->zv_state_lock);
/* If we hold spa_namespace, we can deadlock */
if (ret & ZVOL_LOCK_SPA) {
rw_exit(&zvol_state_lock);
mutex_exit(&spa_namespace_lock);
ret &= ~ZVOL_LOCK_SPA;
dprintf("%s: spa_namespace loop\n",
__func__);
/* Let's not busy loop */
delay(hz>>2);
goto retry;
}
rw_enter(&zv->zv_suspend_lock, RW_READER);
mutex_enter(&zv->zv_state_lock);
/* check to see if zv_suspend_lock is needed */
if (zv->zv_open_count != 0) {
rw_exit(&zv->zv_suspend_lock);
ret &= ~ZVOL_LOCK_SUSPEND;
}
}
}
rw_exit(&zvol_state_lock);
/* Success */
return (ret);
} /* for */
/* Not found */
rw_exit(&zvol_state_lock);
/* It's possible we grabbed spa, but then didn't re-find zv */
if (ret & ZVOL_LOCK_SPA)
mutex_exit(&spa_namespace_lock);
return (0);
}
static void
zvol_os_verify_lock_exit(zvol_state_t *zv, int locks)
{
if (locks & ZVOL_LOCK_SPA)
mutex_exit(&spa_namespace_lock);
mutex_exit(&zv->zv_state_lock);
if (locks & ZVOL_LOCK_SUSPEND)
rw_exit(&zv->zv_suspend_lock);
}
static void
zvol_os_register_device_cb(zvol_state_t *zv, void *param)
{
int locks;
if ((locks = zvol_os_verify_and_lock(zv, zv->zv_open_count == 0)) == 0)
return;
zvol_os_verify_lock_exit(zv, locks);
/* This is a bit racy? */
zvolRegisterDevice(zv);
}
int
zvol_os_write(dev_t dev, struct uio *uio, int p)
{
return (ENOTSUP);
}
int
zvol_os_read(dev_t dev, struct uio *uio, int p)
{
return (ENOTSUP);
}
static int
zvol_os_write_zv_impl(zvol_state_t *zv, void *param)
{
zfs_uio_t *uio = (zfs_uio_t *)param;
int error = 0;
if (zv == NULL)
return (ENXIO);
rw_enter(&zv->zv_suspend_lock, RW_READER);
/* Some requests are just for flush and nothing else. */
if (zfs_uio_resid(uio) == 0) {
rw_exit(&zv->zv_suspend_lock);
return (0);
}
ssize_t start_resid = zfs_uio_resid(uio);
boolean_t sync = (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS);
/*
* Open a ZIL if this is the first time we have written to this
* zvol. We protect zv->zv_zilog with zv_suspend_lock rather
* than zv_state_lock so that we don't need to acquire an
* additional lock in this path.
*/
if (zv->zv_zilog == NULL) {
rw_exit(&zv->zv_suspend_lock);
rw_enter(&zv->zv_suspend_lock, RW_WRITER);
if (zv->zv_zilog == NULL) {
zv->zv_zilog = zil_open(zv->zv_objset,
zvol_get_data);
zv->zv_flags |= ZVOL_WRITTEN_TO;
}
rw_downgrade(&zv->zv_suspend_lock);
}
zfs_locked_range_t *lr = zfs_rangelock_enter(&zv->zv_rangelock,
zfs_uio_offset(uio), zfs_uio_resid(uio), RL_WRITER);
uint64_t volsize = zv->zv_volsize;
while (zfs_uio_resid(uio) > 0 && zfs_uio_offset(uio) < volsize) {
uint64_t bytes = MIN(zfs_uio_resid(uio), DMU_MAX_ACCESS >> 1);
uint64_t off = zfs_uio_offset(uio);
dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
if (bytes > volsize - off) /* don't write past the end */
bytes = volsize - off;
dmu_tx_hold_write_by_dnode(tx, zv->zv_dn, off, bytes);
/* This will only fail for ENOSPC */
error = dmu_tx_assign(tx, TXG_WAIT);
if (error) {
dmu_tx_abort(tx);
break;
}
error = dmu_write_uio_dnode(zv->zv_dn, uio, bytes, tx);
if (error == 0) {
zvol_log_write(zv, tx, off, bytes, sync);
}
dmu_tx_commit(tx);
if (error)
break;
}
zfs_rangelock_exit(lr);
int64_t nwritten = start_resid - zfs_uio_resid(uio);
dataset_kstats_update_write_kstats(&zv->zv_kstat, nwritten);
if (sync)
zil_commit(zv->zv_zilog, ZVOL_OBJ);
rw_exit(&zv->zv_suspend_lock);
return (error);
}
int
zvol_os_write_zv(zvol_state_t *zv, zfs_uio_t *uio)
{
/* Start IO - possibly as taskq */
const vm_offset_t r = OSKernelStackRemaining();
if (r < spl_lowest_zvol_stack_remaining)
spl_lowest_zvol_stack_remaining = r;
if (zfs_uio_resid(uio) != 0 &&
r < spl_split_stack_below)
return (zvol_os_spawn_wait(zv, zvol_os_write_zv_impl, uio));
return (zvol_os_write_zv_impl(zv, uio));
}
int
zvol_os_read_zv_impl(zvol_state_t *zv, void *param)
{
zfs_uio_t *uio = (zfs_uio_t *)param;
int error = 0;
ASSERT3P(zv, !=, NULL);
ASSERT3U(zv->zv_open_count, >, 0);
ssize_t start_resid = zfs_uio_resid(uio);
rw_enter(&zv->zv_suspend_lock, RW_READER);
zfs_locked_range_t *lr = zfs_rangelock_enter(&zv->zv_rangelock,
zfs_uio_offset(uio), zfs_uio_resid(uio), RL_READER);
uint64_t volsize = zv->zv_volsize;
while (zfs_uio_resid(uio) > 0 && zfs_uio_offset(uio) < volsize) {
uint64_t bytes = MIN(zfs_uio_resid(uio), DMU_MAX_ACCESS >> 1);
/* don't read past the end */
if (bytes > volsize - zfs_uio_offset(uio))
bytes = volsize - zfs_uio_offset(uio);
error = dmu_read_uio_dnode(zv->zv_dn, uio, bytes);
if (error) {
/* convert checksum errors into IO errors */
if (error == ECKSUM)
error = SET_ERROR(EIO);
break;
}
}
zfs_rangelock_exit(lr);
int64_t nread = start_resid - zfs_uio_resid(uio);
dataset_kstats_update_read_kstats(&zv->zv_kstat, nread);
rw_exit(&zv->zv_suspend_lock);
return (error);
}
int
zvol_os_read_zv(zvol_state_t *zv, zfs_uio_t *uio)
{
/* Start IO - possibly as taskq */
const vm_offset_t r = OSKernelStackRemaining();
if (r < spl_lowest_zvol_stack_remaining)
spl_lowest_zvol_stack_remaining = r;
if (zfs_uio_resid(uio) != 0 &&
r < spl_split_stack_below)
return (zvol_os_spawn_wait(zv, zvol_os_read_zv_impl, uio));
return (zvol_os_read_zv_impl(zv, uio));
}
int
zvol_os_unmap(zvol_state_t *zv, uint64_t off, uint64_t bytes)
{
zfs_locked_range_t *lr = NULL;
dmu_tx_t *tx = NULL;
int error = 0;
uint64_t end = off + bytes;
if (zv == NULL)
return (ENXIO);
/*
* XNU's wipefs_wipe() will issue one giant unmap for the entire
* device;
* zfs create -V 8g BOOM/vol
* zvolIO doDiscard calling zvol_unmap with offset, bytes (0, 858992)
* Which will both take too long, and is uneccessary. We will ignore
* any unmaps deemed "too large".
*/
if ((off == 0ULL) &&
(zv->zv_volsize > (1ULL << 24)) && /* 16Mb slop */
(bytes >= (zv->zv_volsize - (1ULL << 24))))
return (0);
rw_enter(&zv->zv_suspend_lock, RW_READER);
/*
* Open a ZIL if this is the first time we have written to this
* zvol. We protect zv->zv_zilog with zv_suspend_lock rather
* than zv_state_lock so that we don't need to acquire an
* additional lock in this path.
*/
if (zv->zv_zilog == NULL) {
rw_exit(&zv->zv_suspend_lock);
rw_enter(&zv->zv_suspend_lock, RW_WRITER);
if (zv->zv_zilog == NULL) {
zv->zv_zilog = zil_open(zv->zv_objset,
zvol_get_data);
zv->zv_flags |= ZVOL_WRITTEN_TO;
}
rw_downgrade(&zv->zv_suspend_lock);
}
off = P2ROUNDUP(off, zv->zv_volblocksize);
end = P2ALIGN(end, zv->zv_volblocksize);
if (end > zv->zv_volsize) /* don't write past the end */
end = zv->zv_volsize;
if (off >= end) {
/* Return success- caller does not need to know */
goto out;
}
bytes = end - off;
lr = zfs_rangelock_enter(&zv->zv_rangelock, off, bytes, RL_WRITER);
tx = dmu_tx_create(zv->zv_objset);
dmu_tx_mark_netfree(tx);
error = dmu_tx_assign(tx, TXG_WAIT);
if (error) {
dmu_tx_abort(tx);
} else {
zvol_log_truncate(zv, tx, off, bytes, B_TRUE);
dmu_tx_commit(tx);
error = dmu_free_long_range(zv->zv_objset,
ZVOL_OBJ, off, bytes);
}
zfs_rangelock_exit(lr);
if (error == 0) {
/*
* If the 'sync' property is set to 'always' then
* treat this as a synchronous operation
* (i.e. commit to zil).
*/
if (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS) {
zil_commit(zv->zv_zilog, ZVOL_OBJ);
}
}
out:
rw_exit(&zv->zv_suspend_lock);
return (error);
}
int
zvol_os_update_volsize(zvol_state_t *zv, uint64_t volsize)
{
zv->zv_volsize = volsize;
return (0);
}
static void
zvol_os_clear_private_cb(zvol_state_t *zv, void *param)
{
zvolRemoveDeviceTerminate(param);
}
static void
zvol_os_clear_private(zvol_state_t *zv)
{
void *term;
dprintf("%s\n", __func__);
/* We can do all removal work, except call terminate. */
term = zvolRemoveDevice(zv);
if (term == NULL)
return;
zvol_remove_symlink(zv);
zv->zv_zso->zvo_iokitdev = NULL;
/* Call terminate in the background */
zvol_os_spawn(zv, zvol_os_clear_private_cb, term);
}
/*
* Find a zvol_state_t given the full major+minor dev_t. If found,
* return with zv_state_lock taken, otherwise, return (NULL) without
* taking zv_state_lock.
*/
static zvol_state_t *
zvol_os_find_by_dev(dev_t dev)
{
zvol_state_t *zv;
rw_enter(&zvol_state_lock, RW_READER);
for (zv = list_head(&zvol_state_list); zv != NULL;
zv = list_next(&zvol_state_list, zv)) {
mutex_enter(&zv->zv_state_lock);
if (zv->zv_zso->zvo_dev == dev) {
rw_exit(&zvol_state_lock);
return (zv);
}
mutex_exit(&zv->zv_state_lock);
}
rw_exit(&zvol_state_lock);
return (NULL);
}
void
zvol_os_validate_dev(zvol_state_t *zv)
{
}
/*
* Allocate memory for a new zvol_state_t and setup the required
* request queue and generic disk structures for the block device.
*/
static zvol_state_t *
zvol_os_alloc(dev_t dev, const char *name)
{
zvol_state_t *zv;
struct zvol_state_os *zso;
uint64_t volmode;
if (dsl_prop_get_integer(name, "volmode", &volmode, NULL) != 0)
return (NULL);
if (volmode == ZFS_VOLMODE_DEFAULT)
volmode = zvol_volmode;
if (volmode == ZFS_VOLMODE_NONE)
return (NULL);
zv = kmem_zalloc(sizeof (zvol_state_t), KM_SLEEP);
zso = kmem_zalloc(sizeof (struct zvol_state_os), KM_SLEEP);
zv->zv_zso = zso;
list_link_init(&zv->zv_next);
mutex_init(&zv->zv_state_lock, NULL, MUTEX_DEFAULT, NULL);
zv->zv_open_count = 0;
strlcpy(zv->zv_name, name, MAXNAMELEN);
zfs_rangelock_init(&zv->zv_rangelock, NULL, NULL);
rw_init(&zv->zv_suspend_lock, NULL, RW_DEFAULT, NULL);
return (zv);
#if 0
out_kmem:
kmem_free(zso, sizeof (struct zvol_state_os));
kmem_free(zv, sizeof (zvol_state_t));
return (NULL);
#endif
}
/*
* Cleanup then free a zvol_state_t which was created by zvol_alloc().
* At this time, the structure is not opened by anyone, is taken off
* the zvol_state_list, and has its private data set to NULL.
* The zvol_state_lock is dropped.
*
*/
static void
zvol_os_free(zvol_state_t *zv)
{
ASSERT(!RW_LOCK_HELD(&zv->zv_suspend_lock));
ASSERT(!MUTEX_HELD(&zv->zv_state_lock));
ASSERT(zv->zv_open_count == 0);
rw_destroy(&zv->zv_suspend_lock);
zfs_rangelock_fini(&zv->zv_rangelock);
mutex_destroy(&zv->zv_state_lock);
dataset_kstats_destroy(&zv->zv_kstat);
kmem_free(zv->zv_zso, sizeof (struct zvol_state_os));
kmem_free(zv, sizeof (zvol_state_t));
}
void
zvol_wait_close(zvol_state_t *zv)
{
}
/*
* Create a block device minor node and setup the linkage between it
* and the specified volume. Once this function returns the block
* device is live and ready for use.
*/
static int
zvol_os_create_minor(const char *name)
{
zvol_state_t *zv;
objset_t *os;
dmu_object_info_t *doi;
uint64_t volsize;
unsigned minor = 0;
int error = 0;
uint64_t hash = zvol_name_hash(name);
dprintf("%s\n", __func__);
if (zvol_inhibit_dev)
return (0);
// minor?
zv = zvol_find_by_name_hash(name, hash, RW_NONE);
if (zv) {
ASSERT(MUTEX_HELD(&zv->zv_state_lock));
mutex_exit(&zv->zv_state_lock);
return (SET_ERROR(EEXIST));
}
doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP);
error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, B_TRUE, FTAG, &os);
if (error)
goto out_doi;
error = dmu_object_info(os, ZVOL_OBJ, doi);
if (error)
goto out_dmu_objset_disown;
error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
if (error)
goto out_dmu_objset_disown;
zv = zvol_os_alloc(makedevice(zvol_major, minor), name);
if (zv == NULL) {
error = SET_ERROR(EAGAIN);
goto out_dmu_objset_disown;
}
zv->zv_hash = hash;
if (dmu_objset_is_snapshot(os))
zv->zv_flags |= ZVOL_RDONLY;
zv->zv_volblocksize = doi->doi_data_block_size;
zv->zv_volsize = volsize;
zv->zv_objset = os;
// set_capacity(zv->zv_zso->zvo_disk, zv->zv_volsize >> 9);
ASSERT3P(zv->zv_zilog, ==, NULL);
zv->zv_zilog = zil_open(os, zvol_get_data);
if (spa_writeable(dmu_objset_spa(os))) {
if (zil_replay_disable)
zil_destroy(zv->zv_zilog, B_FALSE);
else
zil_replay(os, zv, zvol_replay_vector);
}
zil_close(zv->zv_zilog);
zv->zv_zilog = NULL;
dataset_kstats_create(&zv->zv_kstat, zv->zv_objset);
/* Create the IOKit zvol while owned */
if ((error = zvolCreateNewDevice(zv)) != 0) {
dprintf("%s zvolCreateNewDevice error %d\n",
__func__, error);
}
zv->zv_objset = NULL;
out_dmu_objset_disown:
dmu_objset_disown(os, B_TRUE, FTAG);
out_doi:
kmem_free(doi, sizeof (dmu_object_info_t));
if (error == 0) {
rw_enter(&zvol_state_lock, RW_WRITER);
zvol_insert(zv);
rw_exit(&zvol_state_lock);
/* Register (async) IOKit zvol after disown and unlock */
/* The callback with release the mutex */
zvol_os_spawn(zv, zvol_os_register_device_cb, NULL);
} else {
}
dprintf("%s complete\n", __func__);
return (error);
}
static void zvol_os_rename_device_cb(zvol_state_t *zv, void *param)
{
int locks;
if ((locks = zvol_os_verify_and_lock(zv, zv->zv_open_count == 0)) == 0)
return;
zvol_add_symlink(zv, zv->zv_zso->zvo_bsdname + 1,
zv->zv_zso->zvo_bsdname);
zvol_os_verify_lock_exit(zv, locks);
zvolRenameDevice(zv);
}
static void
zvol_os_rename_minor(zvol_state_t *zv, const char *newname)
{
// int readonly = get_disk_ro(zv->zv_zso->zvo_disk);
ASSERT(RW_LOCK_HELD(&zvol_state_lock));
ASSERT(MUTEX_HELD(&zv->zv_state_lock));
zvol_remove_symlink(zv);
strlcpy(zv->zv_name, newname, sizeof (zv->zv_name));
/* move to new hashtable entry */
zv->zv_hash = zvol_name_hash(zv->zv_name);
hlist_del(&zv->zv_hlink);
hlist_add_head(&zv->zv_hlink, ZVOL_HT_HEAD(zv->zv_hash));
zvol_os_spawn(zv, zvol_os_rename_device_cb, NULL);
/*
* The block device's read-only state is briefly changed causing
* a KOBJ_CHANGE uevent to be issued. This ensures udev detects
* the name change and fixes the symlinks. This does not change
* ZVOL_RDONLY in zv->zv_flags so the actual read-only state never
* changes. This would normally be done using kobject_uevent() but
* that is a GPL-only symbol which is why we need this workaround.
*/
// set_disk_ro(zv->zv_zso->zvo_disk, !readonly);
// set_disk_ro(zv->zv_zso->zvo_disk, readonly);
}
static void
zvol_os_set_disk_ro(zvol_state_t *zv, int flags)
{
// set_disk_ro(zv->zv_zso->zvo_disk, flags);
}
static void
zvol_os_set_capacity(zvol_state_t *zv, uint64_t capacity)
{
// set_capacity(zv->zv_zso->zvo_disk, capacity);
}
int
zvol_os_open_zv(zvol_state_t *zv, int flag, int otyp, struct proc *p)
{
int error = 0;
int locks;
/*
* make sure zvol is not suspended during first open
* (hold zv_suspend_lock) and respect proper lock acquisition
* ordering - zv_suspend_lock before zv_state_lock
*/
if ((locks = zvol_os_verify_and_lock(zv, zv->zv_open_count == 0))
== 0) {
return (SET_ERROR(ENOENT));
}
ASSERT(MUTEX_HELD(&zv->zv_state_lock));
ASSERT(zv->zv_open_count != 0 || RW_READ_HELD(&zv->zv_suspend_lock));
/*
* We often race opens due to DiskArb. So if spa_namespace_lock is
* already held, potentially a zvol_first_open() is already in progress
*/
if (zv->zv_open_count == 0) {
error = zvol_first_open(zv, !(flag & FWRITE));
if (error)
goto out_mutex;
}
if ((flag & FWRITE) && (zv->zv_flags & ZVOL_RDONLY)) {
error = EROFS;
goto out_open_count;
}
zv->zv_open_count++;
zvol_os_verify_lock_exit(zv, locks);
return (0);
out_open_count:
if (zv->zv_open_count == 0)
zvol_last_close(zv);
out_mutex:
zvol_os_verify_lock_exit(zv, locks);
if (error == EINTR) {
error = ERESTART;
schedule();
}
return (SET_ERROR(error));
}
int
zvol_os_open(dev_t devp, int flag, int otyp, struct proc *p)
{
zvol_state_t *zv;
int error = 0;
if (!getminor(devp))
return (0);
zv = zvol_os_find_by_dev(devp);
if (zv == NULL) {
return (SET_ERROR(ENXIO));
}
error = zvol_os_open_zv(zv, flag, otyp, p);
mutex_exit(&zv->zv_state_lock);
return (SET_ERROR(error));
}
int
zvol_os_close_zv(zvol_state_t *zv, int flag, int otyp, struct proc *p)
{
int locks;
if ((locks = zvol_os_verify_and_lock(zv, TRUE)) == 0)
return (SET_ERROR(ENOENT));
ASSERT(MUTEX_HELD(&zv->zv_state_lock));
ASSERT(zv->zv_open_count != 1 || RW_READ_HELD(&zv->zv_suspend_lock));
zv->zv_open_count--;
if (zv->zv_open_count == 0)
zvol_last_close(zv);
zvol_os_verify_lock_exit(zv, locks);
return (0);
}
int
zvol_os_close(dev_t dev, int flag, int otyp, struct proc *p)
{
zvol_state_t *zv;
int error = 0;
if (!getminor(dev))
return (0);
zv = zvol_os_find_by_dev(dev);
if (zv == NULL) {
return (SET_ERROR(-ENXIO));
}
error = zvol_os_close_zv(zv, flag, otyp, p);
mutex_exit(&zv->zv_state_lock);
return (0);
}
void
zvol_os_strategy(struct buf *bp)
{
}
int
zvol_os_get_volume_blocksize(dev_t dev)
{
/* XNU can only handle two sizes. */
return (DEV_BSIZE);
}
int
zvol_os_ioctl(dev_t dev, unsigned long cmd, caddr_t data, int isblk,
cred_t *cr, int *rvalp)
{
int error = 0;
u_int32_t *f;
u_int64_t *o;
zvol_state_t *zv = NULL;
dprintf("%s\n", __func__);
if (!getminor(dev))
return (ENXIO);
zv = zvol_os_find_by_dev(dev);
if (zv == NULL) {
dprintf("zv is NULL\n");
return (ENXIO);
}
f = (u_int32_t *)data;
o = (u_int64_t *)data;
switch (cmd) {
case DKIOCGETMAXBLOCKCOUNTREAD:
dprintf("DKIOCGETMAXBLOCKCOUNTREAD\n");
*o = 32;
break;
case DKIOCGETMAXBLOCKCOUNTWRITE:
dprintf("DKIOCGETMAXBLOCKCOUNTWRITE\n");
*o = 32;
break;
case DKIOCGETMAXSEGMENTCOUNTREAD:
dprintf("DKIOCGETMAXSEGMENTCOUNTREAD\n");
*o = 32;
break;
case DKIOCGETMAXSEGMENTCOUNTWRITE:
dprintf("DKIOCGETMAXSEGMENTCOUNTWRITE\n");
*o = 32;
break;
case DKIOCGETBLOCKSIZE:
dprintf("DKIOCGETBLOCKSIZE: %llu\n",
zv->zv_volblocksize);