forked from mayadata-io/cstor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mgmt_conn.c
1792 lines (1622 loc) · 49.3 KB
/
mgmt_conn.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) 2018 Cloudbyte. All rights reserved.
*/
#include <time.h>
#include <ifaddrs.h>
#include <netdb.h>
#include <sys/epoll.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/eventfd.h>
#include <sys/prctl.h>
#include <sys/dsl_dataset.h>
#include <sys/dsl_destroy.h>
#include <sys/dsl_dir.h>
#include <sys/dmu_objset.h>
#include <sys/dsl_prop.h>
#include <string.h>
#include <zrepl_prot.h>
#include <uzfs_mgmt.h>
#include <json-c/json_object.h>
#include <libnvpair.h>
#include <mgmt_conn.h>
#include "data_conn.h"
#include "uzfs_rebuilding.h"
/*
* This file contains implementation of event loop (uzfs_zvol_mgmt_thread).
* Event loop is run by a single thread and it has exclusive access to
* file descriptors which simplifies locking. The only synchronization
* problem which needs to be taken care of is adding new connections and
* removing/closing existing ones, which is done by other threads.
* For that purpose there is:
*
* list of connections
* eventfd file descriptor for signaling changes in connection list
* connection list mutex which protects both entities mentioned above
*
* zinfo_create_cb - uzfs callback which adds entry to connection list
* (connect is async - it does not block creation)
* zinfo_destroy_cb - uzfs callback which removes entry from connection list
* (it blocks until the connection FD is really closed
* to guarantee no activity related to zinfo after it
* is destroyed)
* event loop thread never adds or removes list entries but only updates
* their state.
*/
/* log wrappers which prefix log message by iscsi target address */
#define DBGCONN(c, fmt, ...) LOG_DEBUG("[tgt %s:%u:%d]: " fmt, \
(c)->conn_host, (c)->conn_port, \
c->conn_fd, ##__VA_ARGS__)
#define LOGCONN(c, fmt, ...) LOG_INFO("[tgt %s:%u:%d]: " fmt, \
(c)->conn_host, (c)->conn_port, \
c->conn_fd, ##__VA_ARGS__)
#define LOGERRCONN(c, fmt, ...) LOG_ERR("[tgt %s:%u:%d]: " fmt, \
(c)->conn_host, (c)->conn_port, \
c->conn_fd, ##__VA_ARGS__)
/* Max # of events from epoll processed at once */
#define MAX_EVENTS 10
#define MGMT_PORT "12000"
#define RECONNECT_DELAY 4 // 4 seconds
/* conn list can be traversed or changed only when holding the mutex */
kmutex_t conn_list_mtx;
struct uzfs_mgmt_conn_list uzfs_mgmt_conns;
/*
* Blocking or lengthy operations must be executed asynchronously not to block
* the main event loop. Following structure describes asynchronous task.
*/
kmutex_t async_tasks_mtx;
/* event FD for waking up event loop thread blocked in epoll_wait */
int mgmt_eventfd = -1;
int epollfd = -1;
/* default iSCSI target IP address */
char *target_addr;
static int move_to_next_state(uzfs_mgmt_conn_t *conn);
/*
* Remove connection FD from poll set and close the FD.
*/
static int
close_conn(uzfs_mgmt_conn_t *conn)
{
async_task_t *async_task;
if (conn->conn_state != CS_CONNECT)
LOGCONN(conn, "Closing the connection");
/* Release resources tight to the conn */
if (conn->conn_buf != NULL) {
kmem_free(conn->conn_buf, conn->conn_bufsiz);
conn->conn_buf = NULL;
}
conn->conn_bufsiz = 0;
conn->conn_procn = 0;
if (conn->conn_hdr != NULL) {
kmem_free(conn->conn_hdr, sizeof (zvol_io_hdr_t));
conn->conn_hdr = NULL;
}
if (epoll_ctl(epollfd, EPOLL_CTL_DEL, conn->conn_fd, NULL) == -1) {
perror("epoll_ctl del");
return (-1);
}
(void) close(conn->conn_fd);
conn->conn_fd = -1;
mutex_enter(&async_tasks_mtx);
SLIST_FOREACH(async_task, &async_tasks, task_next) {
if (async_task->conn == conn) {
async_task->conn_closed = B_TRUE;
}
}
mutex_exit(&async_tasks_mtx);
return (0);
}
/*
* Complete destruction of conn struct. conn list mtx must be held when calling
* this function.
* Close connection if still open, remove conn from list of conns and free it.
*/
static int
destroy_conn(uzfs_mgmt_conn_t *conn)
{
ASSERT(MUTEX_HELD(&conn_list_mtx));
if (conn->conn_fd >= 0) {
if (close_conn(conn) != 0)
return (-1);
}
DBGCONN(conn, "Destroying the connection");
SLIST_REMOVE(&uzfs_mgmt_conns, conn, uzfs_mgmt_conn, conn_next);
kmem_free(conn, sizeof (*conn));
return (0);
}
/*
* Create non-blocking socket and initiate connection to the target.
* Returns the new FD or -1.
*/
static int
connect_to_tgt(uzfs_mgmt_conn_t *conn)
{
struct sockaddr_in istgt_addr;
int sfd, rc;
conn->conn_last_connect = time(NULL);
bzero((char *)&istgt_addr, sizeof (istgt_addr));
istgt_addr.sin_family = AF_INET;
istgt_addr.sin_addr.s_addr = inet_addr(conn->conn_host);
istgt_addr.sin_port = htons(conn->conn_port);
sfd = create_and_bind(MGMT_PORT, B_FALSE, B_TRUE);
if (sfd < 0)
return (-1);
rc = connect(sfd, (struct sockaddr *)&istgt_addr, sizeof (istgt_addr));
/* EINPROGRESS means that EPOLLOUT will tell us when connect is done */
if (rc != 0 && errno != EINPROGRESS) {
close(sfd);
LOG_ERRNO("Failed to connect to %s:%d", conn->conn_host,
conn->conn_port);
return (-1);
}
return (sfd);
}
/*
* Scan mgmt connection list and create new connections or close unused ones
* as needed.
*/
static int
scan_conn_list(void)
{
uzfs_mgmt_conn_t *conn, *conn_tmp;
struct epoll_event ev;
int rc = 0;
mutex_enter(&conn_list_mtx);
/* iterate safely because entries can be destroyed while iterating */
conn = SLIST_FIRST(&uzfs_mgmt_conns);
while (conn != NULL) {
conn_tmp = SLIST_NEXT(conn, conn_next);
/* we need to create new connection */
if (conn->conn_refcount > 0 && conn->conn_fd < 0 &&
time(NULL) - conn->conn_last_connect >= RECONNECT_DELAY) {
conn->conn_fd = connect_to_tgt(conn);
if (conn->conn_fd >= 0) {
conn->conn_state = CS_CONNECT;
ev.events = EPOLLOUT;
ev.data.ptr = conn;
if (epoll_ctl(epollfd, EPOLL_CTL_ADD,
conn->conn_fd, &ev) == -1) {
perror("epoll_ctl add");
close(conn->conn_fd);
conn->conn_fd = -1;
rc = -1;
break;
}
}
/* we need to close unused connection */
} else if (conn->conn_refcount == 0) {
if (destroy_conn(conn) != 0) {
rc = -1;
break;
}
}
conn = conn_tmp;
}
mutex_exit(&conn_list_mtx);
return (rc);
}
/*
* This gets called whenever a new zinfo is created. We might need to create
* a new mgmt connection to iscsi target in response to this event.
*/
void
zinfo_create_cb(zvol_info_t *zinfo, nvlist_t *create_props)
{
char target_host[MAXNAMELEN];
uint16_t target_port;
uzfs_mgmt_conn_t *conn, *new_mgmt_conn;
zvol_state_t *zv = zinfo->main_zv;
char *delim, *ip;
uint64_t val = 1;
int rc;
/* if zvol is being created the zvol property does not exist yet */
if (create_props != NULL &&
nvlist_lookup_string(create_props, ZFS_PROP_TARGET_IP, &ip) == 0) {
strlcpy(target_host, ip, sizeof (target_host));
} else {
/* get it from zvol properties */
if (zv->zv_target_host[0] == 0) {
/* in case of missing property take the default IP */
strlcpy(target_host, "127.0.0.1", sizeof ("127.0.0.1"));
target_port = TARGET_PORT;
}
else
strlcpy(target_host, zv->zv_target_host, MAXNAMELEN);
}
delim = strchr(target_host, ':');
if (delim == NULL) {
target_port = TARGET_PORT;
} else {
*delim = '\0';
target_port = atoi(++delim);
}
/*
* It is allocated before we enter the mutex even if it might not be
* used because, because in 99% of cases it will be needed (normally
* each zvol has a different iSCSI target).
*/
new_mgmt_conn = kmem_zalloc(sizeof (*new_mgmt_conn), KM_SLEEP);
mutex_enter(&conn_list_mtx);
SLIST_FOREACH(conn, &uzfs_mgmt_conns, conn_next) {
if (strcmp(conn->conn_host, target_host) == 0 &&
conn->conn_port == target_port) {
/* we already have conn for this target */
conn->conn_refcount++;
zinfo->mgmt_conn = conn;
mutex_exit(&conn_list_mtx);
kmem_free(new_mgmt_conn, sizeof (*new_mgmt_conn));
return;
}
}
new_mgmt_conn->conn_fd = -1;
new_mgmt_conn->conn_refcount = 1;
new_mgmt_conn->conn_port = target_port;
strlcpy(new_mgmt_conn->conn_host, target_host,
sizeof (new_mgmt_conn->conn_host));
zinfo->mgmt_conn = new_mgmt_conn;
SLIST_INSERT_HEAD(&uzfs_mgmt_conns, new_mgmt_conn, conn_next);
/* signal the event loop thread */
if (mgmt_eventfd >= 0) {
rc = write(mgmt_eventfd, &val, sizeof (val));
ASSERT3S(rc, ==, sizeof (val));
}
mutex_exit(&conn_list_mtx);
}
/*
* This gets called whenever a zinfo is destroyed. We might need to close
* the mgmt connection to iscsi target if this was the last zinfo using it.
*/
void
zinfo_destroy_cb(zvol_info_t *zinfo)
{
uzfs_mgmt_conn_t *conn;
uint64_t val = 1;
int rc;
mutex_enter(&conn_list_mtx);
SLIST_FOREACH(conn, &uzfs_mgmt_conns, conn_next) {
if (conn == (uzfs_mgmt_conn_t *)zinfo->mgmt_conn)
break;
}
ASSERT3P(conn, !=, NULL);
zinfo->mgmt_conn = NULL;
if (--conn->conn_refcount == 0) {
/* signal the event loop thread to close FD and destroy conn */
ASSERT3S(mgmt_eventfd, >=, 0);
rc = write(mgmt_eventfd, &val, sizeof (val));
ASSERT3S(rc, ==, sizeof (val));
}
mutex_exit(&conn_list_mtx);
}
/*
* Send simple reply without any payload to the client.
*/
static int
reply_nodata(uzfs_mgmt_conn_t *conn, zvol_op_status_t status,
int opcode, uint64_t io_seq)
{
zvol_io_hdr_t *hdrp;
struct epoll_event ev;
if (status != ZVOL_OP_STATUS_OK) {
LOGERRCONN(conn, "Error reply with status %d for OP %d",
status, opcode);
} else {
DBGCONN(conn, "Reply without payload for OP %d", opcode);
}
hdrp = kmem_zalloc(sizeof (*hdrp), KM_SLEEP);
hdrp->version = REPLICA_VERSION;
hdrp->opcode = opcode;
hdrp->io_seq = io_seq;
hdrp->status = status;
hdrp->len = 0;
ASSERT3P(conn->conn_buf, ==, NULL);
conn->conn_buf = hdrp;
conn->conn_bufsiz = sizeof (*hdrp);
conn->conn_procn = 0;
conn->conn_state = CS_INIT;
ev.events = EPOLLOUT;
ev.data.ptr = conn;
return (epoll_ctl(epollfd, EPOLL_CTL_MOD, conn->conn_fd, &ev));
}
/*
* Send reply to client which consists of a header and opaque payload.
*/
static int
reply_data(uzfs_mgmt_conn_t *conn, zvol_io_hdr_t *hdrp, void *buf, int size)
{
struct epoll_event ev;
DBGCONN(conn, "Data reply");
conn->conn_procn = 0;
conn->conn_state = CS_INIT;
ASSERT3P(conn->conn_buf, ==, NULL);
conn->conn_bufsiz = sizeof (*hdrp) + size;
conn->conn_buf = kmem_zalloc(conn->conn_bufsiz, KM_SLEEP);
memcpy(conn->conn_buf, hdrp, sizeof (*hdrp));
memcpy((char *)conn->conn_buf + sizeof (*hdrp), buf, size);
ev.events = EPOLLOUT;
ev.data.ptr = conn;
return (epoll_ctl(epollfd, EPOLL_CTL_MOD, conn->conn_fd, &ev));
}
/*
* Get IP address of first external network interface we encounter.
*/
int
uzfs_zvol_get_ip(char *host, size_t host_len)
{
struct ifaddrs *ifaddr, *ifa;
int family, n;
int rc = -1;
if (getifaddrs(&ifaddr) == -1) {
perror("getifaddrs");
return (-1);
}
/*
* Walk through linked list, maintaining head
* pointer so we can free list later
*/
for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) {
if (ifa->ifa_addr == NULL)
continue;
family = ifa->ifa_addr->sa_family;
if (family == AF_INET || family == AF_INET6) {
rc = getnameinfo(ifa->ifa_addr, (family == AF_INET) ?
sizeof (struct sockaddr_in) :
sizeof (struct sockaddr_in6),
host, host_len,
NULL, 0, NI_NUMERICHOST);
if (rc != 0) {
perror("getnameinfo");
break;
}
if (family == AF_INET) {
if (strcmp(host, "127.0.0.1") == 0)
continue;
break;
}
}
}
freeifaddrs(ifaddr);
return (rc);
}
int
uzfs_zvol_mgmt_get_handshake_info(zvol_io_hdr_t *in_hdr, const char *name,
zvol_info_t *zinfo, zvol_io_hdr_t *out_hdr, mgmt_ack_t *mgmt_ack)
{
zvol_state_t *zv = zinfo->main_zv;
int error1, error2;
bzero(mgmt_ack, sizeof (*mgmt_ack));
if (uzfs_zvol_get_ip(mgmt_ack->ip, MAX_IP_LEN) == -1) {
LOG_ERRNO("Unable to get IP");
return (-1);
}
strlcpy(mgmt_ack->volname, name, sizeof (mgmt_ack->volname));
mgmt_ack->port = (in_hdr->opcode == ZVOL_OPCODE_PREPARE_FOR_REBUILD) ?
REBUILD_IO_SERVER_PORT : IO_SERVER_PORT;
mgmt_ack->pool_guid = spa_guid(zv->zv_spa);
/*
* hold dataset during handshake if objset is NULL
* no critical section here as rebuild & handshake won't come at a time
*/
if (zv->zv_objset == NULL) {
if (uzfs_hold_dataset(zv) != 0) {
LOG_ERR("Failed to hold zvol %s", zinfo->name);
return (-1);
}
}
error1 = uzfs_zvol_get_last_committed_io_no(zv, HEALTHY_IO_SEQNUM,
&zinfo->checkpointed_ionum);
error2 = uzfs_zvol_get_last_committed_io_no(zv, DEGRADED_IO_SEQNUM,
&zinfo->degraded_checkpointed_ionum);
if ((error1 != 0) || (error2 != 0)) {
LOG_ERR("Failed to read io_seqnum %s", zinfo->name);
return (-1);
}
/*
* We don't use fsid_guid because that one is not guaranteed
* to stay the same (it is changed in case of conflicts).
*/
mgmt_ack->zvol_guid = dsl_dataset_phys(
zv->zv_objset->os_dsl_dataset)->ds_guid;
if (zinfo->zvol_guid == 0)
zinfo->zvol_guid = mgmt_ack->zvol_guid;
LOG_INFO("Volume:%s has zvol_guid:%lu", zinfo->name, zinfo->zvol_guid);
bzero(out_hdr, sizeof (*out_hdr));
out_hdr->version = REPLICA_VERSION;
out_hdr->opcode = in_hdr->opcode; // HANDSHAKE or PREPARE_FOR_REBUILD
out_hdr->io_seq = in_hdr->io_seq;
out_hdr->len = sizeof (*mgmt_ack);
out_hdr->status = ZVOL_OP_STATUS_OK;
zinfo->stored_healthy_ionum = zinfo->checkpointed_ionum;
zinfo->running_ionum = zinfo->degraded_checkpointed_ionum;
LOG_INFO("IO sequence number:%lu Degraded IO sequence number:%lu",
zinfo->checkpointed_ionum, zinfo->degraded_checkpointed_ionum);
mgmt_ack->checkpointed_io_seq = zinfo->checkpointed_ionum;
mgmt_ack->checkpointed_degraded_io_seq =
zinfo->degraded_checkpointed_ionum;
mgmt_ack->quorum = uzfs_zinfo_get_quorum(zinfo);
return (0);
}
/*
* This function suppose to lookup into zvol list to find if LUN presented for
* identification is available/online or not. This function also need to send
* back IP address of replica along with port so that ISTGT controller can open
* a connection for IOs.
*/
static int
uzfs_zvol_mgmt_do_handshake(uzfs_mgmt_conn_t *conn, zvol_io_hdr_t *hdrp,
const char *name, zvol_info_t *zinfo)
{
mgmt_ack_t mgmt_ack;
zvol_io_hdr_t hdr;
if (uzfs_zvol_mgmt_get_handshake_info(hdrp, name, zinfo, &hdr,
&mgmt_ack) != 0)
return (reply_nodata(conn, ZVOL_OP_STATUS_FAILED, hdrp->opcode,
hdrp->io_seq));
return (reply_data(conn, &hdr, &mgmt_ack, sizeof (mgmt_ack)));
}
static int
uzfs_zvol_rebuild_status(uzfs_mgmt_conn_t *conn, zvol_io_hdr_t *hdrp,
const char *name, zvol_info_t *zinfo)
{
zrepl_status_ack_t status_ack;
zvol_io_hdr_t hdr;
status_ack.state = uzfs_zvol_get_status(zinfo->main_zv);
bzero(&hdr, sizeof (hdr));
hdr.version = REPLICA_VERSION;
hdr.opcode = hdrp->opcode;
hdr.io_seq = hdrp->io_seq;
hdr.len = sizeof (status_ack);
hdr.status = ZVOL_OP_STATUS_OK;
mutex_enter(&zinfo->main_zv->rebuild_mtx);
status_ack.rebuild_status = uzfs_zvol_get_rebuild_status(
zinfo->main_zv);
/*
* Once the REBUILD_FAILED status is sent to target, rebuild status
* need to be set to INIT so that rebuild can be retriggered
*/
if (uzfs_zvol_get_rebuild_status(zinfo->main_zv) ==
ZVOL_REBUILDING_FAILED) {
memset(&zinfo->main_zv->rebuild_info, 0,
sizeof (zvol_rebuild_info_t));
uzfs_zvol_set_rebuild_status(zinfo->main_zv,
ZVOL_REBUILDING_INIT);
}
mutex_exit(&zinfo->main_zv->rebuild_mtx);
return (reply_data(conn, &hdr, &status_ack, sizeof (status_ack)));
}
static int
uzfs_zvol_stats(uzfs_mgmt_conn_t *conn, zvol_io_hdr_t *hdrp, zvol_info_t *zinfo)
{
zvol_io_hdr_t hdr;
zvol_op_stat_t stat;
objset_t *zv_objset = zinfo->main_zv->zv_objset;
strlcpy(stat.label, "used", sizeof (stat.label));
stat.value = dsl_dir_phys(
zv_objset->os_dsl_dataset->ds_dir)->dd_used_bytes;
bzero(&hdr, sizeof (hdr));
hdr.version = REPLICA_VERSION;
hdr.opcode = hdrp->opcode;
hdr.io_seq = hdrp->io_seq;
hdr.len = sizeof (zvol_op_stat_t);
hdr.status = ZVOL_OP_STATUS_OK;
return (reply_data(conn, &hdr, &stat, sizeof (stat)));
}
static void
uzfs_append_snapshot_properties(nvlist_t *nv, struct json_object *robj,
char *prop_name)
{
nvpair_t *elem = NULL;
nvlist_t *nvlist_value;
uint64_t value = 0;
char *str_value;
int len;
if (nv == NULL) {
return;
}
while ((elem = nvlist_next_nvpair(nv, elem)) != NULL) {
switch (nvpair_type(elem)) {
case DATA_TYPE_UINT64:
nvpair_value_uint64(elem, &value);
if (!prop_name) {
if (strcmp(nvpair_name(elem), "source"))
LOG_ERR("property name not set.. "
"elem:%s val:%lu",
nvpair_name(elem), value);
} else {
len = snprintf(NULL, 0, "%lu", value) + 1;
str_value = kmem_zalloc(len, KM_SLEEP);
snprintf(str_value, len, "%lu", value);
json_object_object_add(robj, prop_name,
json_object_new_string(str_value));
kmem_free(str_value, len);
}
break;
case DATA_TYPE_STRING:
nvpair_value_string(elem, &str_value);
if (!prop_name) {
if (strcmp(nvpair_name(elem), "source"))
LOG_ERR("property name not set.. "
"elem:%s val:%lu",
nvpair_name(elem), value);
} else
json_object_object_add(robj, prop_name,
json_object_new_string(str_value));
break;
case DATA_TYPE_NVLIST:
(void) nvpair_value_nvlist(elem, &nvlist_value);
uzfs_append_snapshot_properties(nvlist_value, robj,
nvpair_name(elem));
break;
default:
LOG_ERR("nvpair type : %d name:%s\n",
nvpair_type(elem), nvpair_name(elem));
}
prop_name = NULL;
}
}
/* Returns TRUE if given is name of internally created snapshot */
static boolean_t
internal_snapshot(char *snap)
{
if ((strcmp(snap, REBUILD_SNAPSHOT_SNAPNAME) == 0) ||
(strncmp(snap, IO_DIFF_SNAPNAME, sizeof (IO_DIFF_SNAPNAME) - 1)
== 0))
return (B_TRUE);
return (B_FALSE);
}
static int
uzfs_zvol_fetch_snapshot_list(zvol_info_t *zinfo, void **buf,
size_t *buflen)
{
char *snapname;
boolean_t case_conflict, prop_error;
uint64_t id, pos = 0;
int error = 0;
zvol_state_t *zv = (zvol_state_t *)zinfo->main_zv;
objset_t *os = zv->zv_objset;
struct zvol_snapshot_list *snap_list;
dsl_dataset_t *ds;
dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
objset_t *snap_os;
nvlist_t *nv;
struct json_object *jobj, *jarray, *jprop;
const char *json_string;
uint64_t total_len;
char err_msg[128];
snapname = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP);
jarray = json_object_new_array();
while (error == 0) {
prop_error = TRUE;
dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
error = dmu_snapshot_list_next(os,
ZFS_MAX_DATASET_NAME_LEN, snapname, &id, &pos,
&case_conflict);
if (error) {
dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
goto out;
}
if (internal_snapshot(snapname)) {
dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
continue;
}
error = dsl_dataset_hold_obj(dp, id, FTAG, &ds);
if (error == 0) {
error = dmu_objset_from_ds(ds, &snap_os);
if (error == 0 &&
!dsl_prop_get_all(snap_os, &nv)) {
dmu_objset_stats(snap_os, nv);
if (zvol_get_stats(snap_os, nv))
LOG_ERR("Failed to get zvol "
"stats");
prop_error = FALSE;
} else
prop_error = TRUE;
dsl_dataset_rele(ds, FTAG);
}
dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
jobj = json_object_new_object();
json_object_object_add(jobj, "name",
json_object_new_string(snapname));
jprop = json_object_new_object();
if (error == 0 && !prop_error) {
uzfs_append_snapshot_properties(nv, jprop, NULL);
nvlist_free(nv);
} else {
snprintf(err_msg, sizeof (err_msg),
"Failed to fetch snapshot details.. err(%d)",
error);
json_object_object_add(jprop, "error",
json_object_new_string(err_msg));
}
json_object_object_add(jobj, "properties", jprop);
json_object_array_add(jarray, jobj);
}
out:
jobj = json_object_new_object();
json_object_object_add(jobj, "snapshot", jarray);
kmem_free(snapname, ZFS_MAX_DATASET_NAME_LEN);
json_string = json_object_to_json_string_ext(jobj,
JSON_C_TO_STRING_PLAIN);
total_len = strlen(json_string);
snap_list = malloc(total_len + sizeof (*snap_list));
memset(snap_list, 0, total_len + sizeof (*snap_list));
snap_list->zvol_guid = zinfo->zvol_guid;
snap_list->error = (error == ENOENT) ? 0 : error;
snap_list->data_len = total_len;
strncpy(snap_list->data, json_string, total_len);
json_object_put(jobj);
*buf = snap_list;
*buflen = total_len + sizeof (*snap_list);
return (0);
}
static void
free_async_task(async_task_t *async_task)
{
ASSERT(MUTEX_HELD(&async_tasks_mtx));
uzfs_zinfo_drop_refcnt(async_task->zinfo);
if (async_task->payload)
kmem_free(async_task->payload, async_task->payload_length);
if (async_task->response)
kmem_free(async_task->response, async_task->response_length);
kmem_free(async_task, sizeof (*async_task));
}
/*
* Iterate through all finished async tasks and send replies to clients.
*/
int
finish_async_tasks(void)
{
async_task_t *async_task, *async_task_tmp;
int rc = 0;
mutex_enter(&async_tasks_mtx);
for (async_task = SLIST_FIRST(&async_tasks);
async_task != NULL;
async_task = async_task_tmp) {
async_task_tmp = SLIST_NEXT(async_task, task_next);
if (!async_task->finished)
continue;
/* connection could have been closed in the meantime */
if (!async_task->conn_closed) {
if (async_task->response) {
async_task->hdr.status = async_task->status;
async_task->hdr.len =
async_task->response_length;
rc = reply_data(async_task->conn,
&async_task->hdr, async_task->response,
async_task->response_length);
} else
rc = reply_nodata(async_task->conn,
async_task->status, async_task->hdr.opcode,
async_task->hdr.io_seq);
}
SLIST_REMOVE(&async_tasks, async_task, async_task, task_next);
free_async_task(async_task);
if (rc != 0)
break;
}
mutex_exit(&async_tasks_mtx);
return (rc);
}
/*
* Checks that running io_num is not greater than snapshot_io_num
* Update snapshot IO to ZAP and take snapshot.
*/
int
uzfs_zvol_create_snapshot_update_zap(zvol_info_t *zinfo,
char *snapname, uint64_t snapshot_io_num)
{
int ret = 0;
if ((uzfs_zvol_get_status(zinfo->main_zv) == ZVOL_STATUS_HEALTHY) &&
(zinfo->running_ionum > snapshot_io_num -1)) {
LOG_ERR("Failed to create snapshot as running_ionum %lu"
" is greater than snapshot_io_num %lu",
zinfo->running_ionum, snapshot_io_num);
return (ret = -1);
}
uzfs_zinfo_store_last_committed_healthy_io_no(zinfo,
snapshot_io_num - 1);
ret = dmu_objset_snapshot_one(zinfo->name, snapname);
return (ret);
}
/*
* Returns zv of snap that is just higher than given ionum
* If no such snap exists, it returns NULL
* Note: caller should do uzfs_close_dataset of zv, and,
* caller need to take care of any ongoing snap requests
*/
int
uzfs_get_snap_zv_ionum(zvol_info_t *zinfo, uint64_t ionum,
zvol_state_t **psnapzv)
{
if ((zinfo == NULL) || (zinfo->main_zv == NULL) ||
(zinfo->main_zv->zv_objset == NULL))
return (-1);
uint64_t obj = 0, cookie = 0;
zvol_state_t *zv = zinfo->main_zv;
zvol_state_t *snap_zv = NULL;
zvol_state_t *smallest_higher_snapzv = NULL;
objset_t *os = zv->zv_objset;
char snapname[MAXNAMELEN];
uint64_t healthy_ionum, smallest_higher_ionum = 0;
int error;
while (1) {
dsl_pool_config_enter(spa_get_dsl(zv->zv_spa), FTAG);
error = dmu_snapshot_list_next(os, sizeof (snapname) - 1,
snapname, &obj, &cookie, NULL);
dsl_pool_config_exit(spa_get_dsl(zv->zv_spa), FTAG);
if (error) {
if (error == ENOENT)
error = 0;
break;
}
if (internal_snapshot(snapname))
continue;
error = get_snapshot_zv(zv, snapname, &snap_zv, B_FALSE,
B_TRUE);
if (error) {
LOG_ERR("err %d in getting snap %s", error, snapname);
break;
}
error = uzfs_zvol_get_last_committed_io_no(snap_zv,
HEALTHY_IO_SEQNUM, &healthy_ionum);
if (error) {
LOG_ERR("err %d in getting last commited for snap %s",
error, snap_zv->zv_name);
uzfs_close_dataset(snap_zv);
snap_zv = NULL;
break;
}
if ((healthy_ionum > ionum) &&
((smallest_higher_snapzv == NULL) ||
(smallest_higher_ionum > healthy_ionum))) {
smallest_higher_ionum = healthy_ionum;
if (smallest_higher_snapzv != NULL)
uzfs_close_dataset(smallest_higher_snapzv);
smallest_higher_snapzv = snap_zv;
} else
uzfs_close_dataset(snap_zv);
}
if (error) {
if (smallest_higher_snapzv != NULL) {
uzfs_close_dataset(smallest_higher_snapzv);
smallest_higher_snapzv = NULL;
}
} else if (psnapzv != NULL)
*psnapzv = smallest_higher_snapzv;
return (error);
}
/*
* For a given snap name, get snap dataset and IO number stored in ZAP
* Input: zinfo, snap
* Output: snapshot_io_num, snap_zv
*/
int
uzfs_zvol_get_snap_dataset_with_io(zvol_info_t *zinfo,
char *snapname, uint64_t *snapshot_io_num, zvol_state_t **snap_zv)
{
int ret = 0;
char *longsnap = kmem_asprintf("%s@%s",
strchr(zinfo->name, '/') + 1, snapname);
ret = uzfs_open_dataset(zinfo->main_zv->zv_spa, longsnap, snap_zv);
if (ret != 0) {
LOG_ERR("Failed to get info about %s", longsnap);
strfree(longsnap);
return (ret);
}
strfree(longsnap);
ret = uzfs_hold_dataset(*snap_zv);
if (ret != 0) {
LOG_ERR("Failed to hold snapshot: %d", ret);
uzfs_close_dataset(*snap_zv);
*snap_zv = NULL;
return (ret);
}
ret = uzfs_zvol_get_last_committed_io_no(*snap_zv,
HEALTHY_IO_SEQNUM, snapshot_io_num);
return (ret);
}
/*
* Perform the command (in async context).
*
* Currently we have only snapshot commands which are async. We might need to
* make the code & structures more generic if we add more commands.
*/
static void
uzfs_zvol_execute_async_command(void *arg)
{
async_task_t *async_task = arg;
zvol_info_t *zinfo = async_task->zinfo;
char *dataset;
char *snap;
uint64_t volsize;
int rc;
switch (async_task->hdr.opcode) {
case ZVOL_OPCODE_SNAP_CREATE:
snap = async_task->payload;
rc = uzfs_zvol_create_snapshot_update_zap(zinfo, snap,
async_task->hdr.io_seq);
if (rc != 0) {
/*
* snap create command failed, close the io
* connection so that it can start the rebuilding
*/
if (zinfo->io_fd >= 0)
VERIFY0(shutdown(zinfo->io_fd, SHUT_RDWR));
LOG_ERR("Failed to create %s@%s: %d",
zinfo->name, snap, rc);
async_task->status = ZVOL_OP_STATUS_FAILED;
} else {
async_task->status = ZVOL_OP_STATUS_OK;
}
break;
case ZVOL_OPCODE_SNAP_DESTROY:
snap = async_task->payload;
dataset = kmem_asprintf("%s@%s", zinfo->name, snap);
rc = dsl_destroy_snapshot(dataset, B_FALSE);
strfree(dataset);
if (rc != 0) {
LOG_ERR("Failed to destroy %s@%s: %d",
zinfo->name, snap, rc);
async_task->status = ZVOL_OP_STATUS_FAILED;
} else {
async_task->status = ZVOL_OP_STATUS_OK;
}
break;
case ZVOL_OPCODE_RESIZE:
volsize = *(uint64_t *)async_task->payload;
rc = zvol_check_volsize(volsize,
zinfo->main_zv->zv_volblocksize);
if (rc == 0) {
rc = zvol_update_volsize(volsize,
zinfo->main_zv->zv_objset);
if (rc == 0)