/
bs_sheepdog.c
1455 lines (1233 loc) · 33.6 KB
/
bs_sheepdog.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) 2010 FUJITA Tomonori <tomof@acm.org>
* Copyright (C) 2013 Nippon Telegraph and Telephone Corporation.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, version 2 of the
* License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <pthread.h>
#include <limits.h>
#include <ctype.h>
#include <sys/un.h>
#include "list.h"
#include "tgtd.h"
#include "util.h"
#include "log.h"
#include "scsi.h"
#include "bs_thread.h"
#define SD_PROTO_VER 0x01
#define SD_DEFAULT_ADDR "localhost"
#define SD_DEFAULT_PORT 7000
#define SD_OP_CREATE_AND_WRITE_OBJ 0x01
#define SD_OP_READ_OBJ 0x02
#define SD_OP_WRITE_OBJ 0x03
/* 0x04 is used internally by Sheepdog */
#define SD_OP_DISCARD_OBJ 0x05
#define SD_OP_NEW_VDI 0x11
#define SD_OP_LOCK_VDI 0x12
#define SD_OP_RELEASE_VDI 0x13
#define SD_OP_GET_VDI_INFO 0x14
#define SD_OP_READ_VDIS 0x15
#define SD_OP_FLUSH_VDI 0x16
#define SD_OP_DEL_VDI 0x17
#define SD_FLAG_CMD_WRITE 0x01
#define SD_FLAG_CMD_COW 0x02
#define SD_FLAG_CMD_CACHE 0x04 /* Writeback mode for cache */
#define SD_FLAG_CMD_DIRECT 0x08 /* Don't use cache */
/* return something back while sending something to sheep */
#define SD_FLAG_CMD_PIGGYBACK 0x10
#define SD_FLAG_CMD_TGT 0x20
#define SD_RES_SUCCESS 0x00 /* Success */
#define SD_RES_UNKNOWN 0x01 /* Unknown error */
#define SD_RES_NO_OBJ 0x02 /* No object found */
#define SD_RES_EIO 0x03 /* I/O error */
#define SD_RES_VDI_EXIST 0x04 /* Vdi exists already */
#define SD_RES_INVALID_PARMS 0x05 /* Invalid parameters */
#define SD_RES_SYSTEM_ERROR 0x06 /* System error */
#define SD_RES_VDI_LOCKED 0x07 /* Vdi is locked */
#define SD_RES_NO_VDI 0x08 /* No vdi found */
#define SD_RES_NO_BASE_VDI 0x09 /* No base vdi found */
#define SD_RES_VDI_READ 0x0A /* Cannot read requested vdi */
#define SD_RES_VDI_WRITE 0x0B /* Cannot write requested vdi */
#define SD_RES_BASE_VDI_READ 0x0C /* Cannot read base vdi */
#define SD_RES_BASE_VDI_WRITE 0x0D /* Cannot write base vdi */
#define SD_RES_NO_TAG 0x0E /* Requested tag is not found */
#define SD_RES_STARTUP 0x0F /* Sheepdog is on starting up */
#define SD_RES_VDI_NOT_LOCKED 0x10 /* Vdi is not locked */
#define SD_RES_SHUTDOWN 0x11 /* Sheepdog is shutting down */
#define SD_RES_NO_MEM 0x12 /* Cannot allocate memory */
#define SD_RES_FULL_VDI 0x13 /* we already have the maximum vdis */
#define SD_RES_VER_MISMATCH 0x14 /* Protocol version mismatch */
#define SD_RES_NO_SPACE 0x15 /* Server has no room for new objects */
#define SD_RES_WAIT_FOR_FORMAT 0x16 /* Waiting for a format operation */
#define SD_RES_WAIT_FOR_JOIN 0x17 /* Waiting for other nodes joining */
#define SD_RES_JOIN_FAILED 0x18 /* Target node had failed to join sheepdog */
#define SD_RES_HALT 0x19 /* Sheepdog is stopped serving IO request */
#define SD_RES_READONLY 0x1A /* Object is read-only */
#define SD_RES_INCOMPLETE 0x1B /* Object (in kv) is incomplete uploading */
/* sheep is collecting cluster wide status, not ready for operation */
#define SD_RES_COLLECTING_CINFO 0x1C
/* inode object in client is invalidated, refreshing is required */
#define SD_RES_INODE_INVALIDATED 0x1D
/*
* Object ID rules
*
* 0 - 19 (20 bits): data object space
* 20 - 31 (12 bits): reserved data object space
* 32 - 55 (24 bits): vdi object space
* 56 - 59 ( 4 bits): reserved vdi object space
* 60 - 63 ( 4 bits): object type identifier space
*/
#define VDI_SPACE_SHIFT 32
#define VDI_BIT (UINT64_C(1) << 63)
#define VMSTATE_BIT (UINT64_C(1) << 62)
#define MAX_DATA_OBJS (UINT64_C(1) << 20)
#define MAX_CHILDREN 1024
#define SD_MAX_VDI_LEN 256
#define SD_MAX_VDI_TAG_LEN 256
#define SD_NR_VDIS (1U << 24)
#define SD_DATA_OBJ_SIZE (UINT64_C(1) << 22)
#define SD_MAX_VDI_SIZE (SD_DATA_OBJ_SIZE * MAX_DATA_OBJS)
#define SECTOR_SIZE 512
#define CURRENT_VDI_ID 0
struct sheepdog_req {
uint8_t proto_ver;
uint8_t opcode;
uint16_t flags;
uint32_t epoch;
uint32_t id;
uint32_t data_length;
uint32_t opcode_specific[8];
};
struct sheepdog_rsp {
uint8_t proto_ver;
uint8_t opcode;
uint16_t flags;
uint32_t epoch;
uint32_t id;
uint32_t data_length;
uint32_t result;
uint32_t opcode_specific[7];
};
struct sheepdog_obj_req {
uint8_t proto_ver;
uint8_t opcode;
uint16_t flags;
uint32_t epoch;
uint32_t id;
uint32_t data_length;
uint64_t oid;
uint64_t cow_oid;
uint32_t copies;
uint32_t rsvd;
uint64_t offset;
};
struct sheepdog_obj_rsp {
uint8_t proto_ver;
uint8_t opcode;
uint16_t flags;
uint32_t epoch;
uint32_t id;
uint32_t data_length;
uint32_t result;
uint32_t copies;
uint32_t pad[6];
};
#define LOCK_TYPE_NORMAL 0
#define LOCK_TYPE_SHARED 1 /* for iSCSI multipath */
struct sheepdog_vdi_req {
uint8_t proto_ver;
uint8_t opcode;
uint16_t flags;
uint32_t epoch;
uint32_t id;
uint32_t data_length;
uint64_t vdi_size;
uint32_t vdi_id;
uint32_t copies;
uint32_t snapid;
uint32_t type;
uint32_t pad[2];
};
struct sheepdog_vdi_rsp {
uint8_t proto_ver;
uint8_t opcode;
uint16_t flags;
uint32_t epoch;
uint32_t id;
uint32_t data_length;
uint32_t result;
uint32_t rsvd;
uint32_t vdi_id;
uint32_t pad[5];
};
/*
* Historical notes: previous version of sheepdog (< v0.9.0) has a limit of
* maximum number of children which can be created from single VDI. So the inode
* object has an array for storing the IDs of the child VDIs. The constant
* OLD_MAX_CHILDREN represents it. Current sheepdog doesn't have the limitation,
* so we are recycling the area (4 * OLD_MAX_CHILDREN = 4KB) for storing new
* metadata.
*
* users of the released area:
* - uint32_t btree_counter
*/
#define OLD_MAX_CHILDREN 1024U
struct sheepdog_inode {
char name[SD_MAX_VDI_LEN];
char tag[SD_MAX_VDI_TAG_LEN];
uint64_t create_time;
uint64_t snap_ctime;
uint64_t vm_clock_nsec;
uint64_t vdi_size;
uint64_t vm_state_size;
uint8_t copy_policy;
uint8_t store_policy;
uint8_t nr_copies;
uint8_t block_size_shift;
uint32_t snap_id;
uint32_t vdi_id;
uint32_t parent_vdi_id;
uint32_t btree_counter;
uint32_t __unused[OLD_MAX_CHILDREN - 1];
uint32_t data_vdi_id[MAX_DATA_OBJS];
};
#define SD_INODE_SIZE (sizeof(struct sheepdog_inode))
struct sheepdog_fd_list {
int fd;
pthread_t id;
struct list_head list;
};
#define UNIX_PATH_MAX 108
struct sheepdog_access_info {
int is_unix;
/* tcp */
char hostname[HOST_NAME_MAX + 1];
int port;
/* unix domain socket */
char uds_path[UNIX_PATH_MAX];
/* if the opened VDI is a snapshot, write commands cannot be issued */
int is_snapshot;
/*
* maximum length of fd_list_head: nr_iothreads + 1
* (+ 1 is for main thread)
*
* TODO: more effective data structure for handling massive parallel
* access
*/
struct list_head fd_list_head;
pthread_rwlock_t fd_list_lock;
uint32_t min_dirty_data_idx;
uint32_t max_dirty_data_idx;
struct sheepdog_inode inode;
pthread_rwlock_t inode_lock;
};
static inline int is_data_obj_writeable(struct sheepdog_inode *inode,
unsigned int idx)
{
return inode->vdi_id == inode->data_vdi_id[idx];
}
static inline int is_data_obj(uint64_t oid)
{
return !(VDI_BIT & oid);
}
static inline uint64_t data_oid_to_idx(uint64_t oid)
{
return oid & (MAX_DATA_OBJS - 1);
}
static inline uint64_t vid_to_vdi_oid(uint32_t vid)
{
return VDI_BIT | ((uint64_t)vid << VDI_SPACE_SHIFT);
}
static inline uint64_t vid_to_vmstate_oid(uint32_t vid, uint32_t idx)
{
return VMSTATE_BIT | ((uint64_t)vid << VDI_SPACE_SHIFT) | idx;
}
static inline uint64_t vid_to_data_oid(uint32_t vid, uint32_t idx)
{
return ((uint64_t)vid << VDI_SPACE_SHIFT) | idx;
}
static const char *sd_strerror(int err)
{
int i;
static const struct {
int err;
const char *desc;
} errors[] = {
{SD_RES_SUCCESS,
"Success"},
{SD_RES_UNKNOWN,
"Unknown error"},
{SD_RES_NO_OBJ, "No object found"},
{SD_RES_EIO, "I/O error"},
{SD_RES_VDI_EXIST, "VDI exists already"},
{SD_RES_INVALID_PARMS, "Invalid parameters"},
{SD_RES_SYSTEM_ERROR, "System error"},
{SD_RES_VDI_LOCKED, "VDI is already locked"},
{SD_RES_NO_VDI, "No vdi found"},
{SD_RES_NO_BASE_VDI, "No base VDI found"},
{SD_RES_VDI_READ, "Failed read the requested VDI"},
{SD_RES_VDI_WRITE, "Failed to write the requested VDI"},
{SD_RES_BASE_VDI_READ, "Failed to read the base VDI"},
{SD_RES_BASE_VDI_WRITE, "Failed to write the base VDI"},
{SD_RES_NO_TAG, "Failed to find the requested tag"},
{SD_RES_STARTUP, "The system is still booting"},
{SD_RES_VDI_NOT_LOCKED, "VDI isn't locked"},
{SD_RES_SHUTDOWN, "The system is shutting down"},
{SD_RES_NO_MEM, "Out of memory on the server"},
{SD_RES_FULL_VDI, "We already have the maximum vdis"},
{SD_RES_VER_MISMATCH, "Protocol version mismatch"},
{SD_RES_NO_SPACE, "Server has no space for new objects"},
{SD_RES_WAIT_FOR_FORMAT, "Sheepdog is waiting for a format operation"},
{SD_RES_WAIT_FOR_JOIN, "Sheepdog is waiting for other nodes joining"},
{SD_RES_JOIN_FAILED, "Target node had failed to join sheepdog"},
{SD_RES_HALT, "Sheepdog is stopped serving IO request"},
{SD_RES_READONLY, "Object is read-only"},
{SD_RES_INODE_INVALIDATED, "Inode object is invalidated"},
};
for (i = 0; i < ARRAY_SIZE(errors); ++i) {
if (errors[i].err == err)
return errors[i].desc;
}
return "Invalid error code";
}
static int connect_to_sdog_tcp(const char *addr, int port)
{
char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
int fd, ret;
struct addrinfo hints, *res, *res0;
char port_s[6];
if (!addr) {
addr = SD_DEFAULT_ADDR;
port = SD_DEFAULT_PORT;
}
memset(port_s, 0, 6);
snprintf(port_s, 5, "%d", port);
memset(&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_STREAM;
ret = getaddrinfo(addr, port_s, &hints, &res0);
if (ret) {
eprintf("unable to get address info %s, %s\n",
addr, strerror(errno));
return -1;
}
for (res = res0; res; res = res->ai_next) {
ret = getnameinfo(res->ai_addr, res->ai_addrlen, hbuf,
sizeof(hbuf), sbuf, sizeof(sbuf),
NI_NUMERICHOST | NI_NUMERICSERV);
if (ret)
continue;
fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (fd < 0)
continue;
reconnect:
ret = connect(fd, res->ai_addr, res->ai_addrlen);
if (ret < 0) {
if (errno == EINTR)
goto reconnect;
close(fd);
break;
}
dprintf("connected to %s:%d\n", addr, port);
goto success;
}
fd = -1;
eprintf("failed connect to %s:%d\n", addr, port);
success:
freeaddrinfo(res0);
return fd;
}
static int connect_to_sdog_unix(const char *path)
{
int fd, ret;
struct sockaddr_un un;
fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd < 0) {
eprintf("socket() failed: %m\n");
return -1;
}
memset(&un, 0, sizeof(un));
un.sun_family = AF_UNIX;
strncpy(un.sun_path, path, sizeof(un.sun_path) - 1);
ret = connect(fd, (const struct sockaddr *)&un, (socklen_t)sizeof(un));
if (ret < 0) {
eprintf("connect() failed: %m\n");
close(fd);
return -1;
}
return fd;
}
static int get_my_fd(struct sheepdog_access_info *ai)
{
pthread_t self_id = pthread_self();
struct sheepdog_fd_list *p;
int fd;
pthread_rwlock_rdlock(&ai->fd_list_lock);
list_for_each_entry(p, &ai->fd_list_head, list) {
if (p->id == self_id) {
pthread_rwlock_unlock(&ai->fd_list_lock);
return p->fd;
}
}
pthread_rwlock_unlock(&ai->fd_list_lock);
if (ai->is_unix)
fd = connect_to_sdog_unix(ai->uds_path);
else
fd = connect_to_sdog_tcp(ai->hostname, ai->port);
if (fd < 0)
return -1;
p = zalloc(sizeof(*p));
if (!p) {
close(fd);
return -1;
}
p->id = self_id;
p->fd = fd;
INIT_LIST_HEAD(&p->list);
pthread_rwlock_wrlock(&ai->fd_list_lock);
list_add_tail(&p->list, &ai->fd_list_head);
pthread_rwlock_unlock(&ai->fd_list_lock);
return p->fd;
}
static void close_my_fd(struct sheepdog_access_info *ai, int fd)
{
struct sheepdog_fd_list *p;
int closed = 0;
pthread_rwlock_wrlock(&ai->fd_list_lock);
list_for_each_entry(p, &ai->fd_list_head, list) {
if (p->fd == fd) {
close(fd);
list_del(&p->list);
free(p);
closed = 1;
break;
}
}
pthread_rwlock_unlock(&ai->fd_list_lock);
if (!closed)
eprintf("unknown fd to close: %d\n", fd);
}
static int do_read(int sockfd, void *buf, int len)
{
int ret;
reread:
ret = read(sockfd, buf, len);
if (!ret) {
eprintf("connection is closed (%d bytes left)\n", len);
return 1;
}
if (ret < 0) {
if (errno == EINTR || errno == EAGAIN)
goto reread;
eprintf("failed to read from socket: %d, %s\n",
ret, strerror(errno));
return 1;
}
len -= ret;
buf = (char *)buf + ret;
if (len)
goto reread;
return 0;
}
static void forward_iov(struct msghdr *msg, int len)
{
while (msg->msg_iov->iov_len <= len) {
len -= msg->msg_iov->iov_len;
msg->msg_iov++;
msg->msg_iovlen--;
}
msg->msg_iov->iov_base = (char *) msg->msg_iov->iov_base + len;
msg->msg_iov->iov_len -= len;
}
static int do_write(int sockfd, struct msghdr *msg, int len)
{
int ret;
rewrite:
ret = sendmsg(sockfd, msg, 0);
if (ret < 0) {
if (errno == EINTR || errno == EAGAIN)
goto rewrite;
eprintf("failed to write to socket: %d, %s\n",
ret, strerror(errno));
return 1;
}
len -= ret;
if (len) {
forward_iov(msg, ret);
goto rewrite;
}
return 0;
}
static int send_req(int sockfd, struct sheepdog_req *hdr, void *data,
unsigned int *wlen)
{
int ret;
struct iovec iov[2];
struct msghdr msg;
memset(&msg, 0, sizeof(msg));
msg.msg_iov = iov;
msg.msg_iovlen = 1;
iov[0].iov_base = hdr;
iov[0].iov_len = sizeof(*hdr);
if (*wlen) {
msg.msg_iovlen++;
iov[1].iov_base = data;
iov[1].iov_len = *wlen;
}
ret = do_write(sockfd, &msg, sizeof(*hdr) + *wlen);
if (ret) {
eprintf("failed to send a req, %s\n", strerror(errno));
ret = -1;
}
return ret;
}
static int do_req(struct sheepdog_access_info *ai, struct sheepdog_req *hdr,
void *data, unsigned int *wlen, unsigned int *rlen)
{
int ret, sockfd, count = 0;
retry:
if (count++) {
eprintf("retrying to reconnect (%d)\n", count);
if (0 <= sockfd)
close_my_fd(ai, sockfd);
sleep(1);
}
sockfd = get_my_fd(ai);
if (sockfd < 0)
goto retry;
ret = send_req(sockfd, hdr, data, wlen);
if (ret)
goto retry;
/* FIXME: retrying COW request should be handled in graceful way */
ret = do_read(sockfd, hdr, sizeof(*hdr));
if (ret)
goto retry;
if (hdr->data_length < *rlen)
*rlen = hdr->data_length;
if (*rlen) {
ret = do_read(sockfd, data, *rlen);
if (ret)
goto retry;
}
return 0;
}
static int find_vdi_name(struct sheepdog_access_info *ai, char *filename,
uint32_t snapid, char *tag, uint32_t *vid,
int for_snapshot);
static int read_object(struct sheepdog_access_info *ai, char *buf, uint64_t oid,
int copies, unsigned int datalen, uint64_t offset,
int *need_reload);
static int reload_inode(struct sheepdog_access_info *ai)
{
int ret, need_reload = 0;
char tag[SD_MAX_VDI_TAG_LEN];
uint32_t vid;
memset(tag, 0, sizeof(tag));
ret = find_vdi_name(ai, ai->inode.name, CURRENT_VDI_ID, tag, &vid, 0);
if (ret)
return -1;
ret = read_object(ai, (char *)&ai->inode, vid_to_vdi_oid(vid),
ai->inode.nr_copies, SD_INODE_SIZE, 0, &need_reload);
if (ret)
return -1;
return 0;
}
static int read_write_object(struct sheepdog_access_info *ai, char *buf,
uint64_t oid, int copies,
unsigned int datalen, uint64_t offset,
int write, int create, uint64_t old_oid,
uint16_t flags, int *need_reload)
{
struct sheepdog_obj_req hdr;
struct sheepdog_obj_rsp *rsp = (struct sheepdog_obj_rsp *)&hdr;
unsigned int wlen, rlen;
int ret;
memset(&hdr, 0, sizeof(hdr));
hdr.proto_ver = SD_PROTO_VER;
hdr.flags = flags;
if (write) {
wlen = datalen;
rlen = 0;
hdr.flags |= SD_FLAG_CMD_WRITE;
if (create) {
hdr.opcode = SD_OP_CREATE_AND_WRITE_OBJ;
hdr.cow_oid = old_oid;
} else {
hdr.opcode = SD_OP_WRITE_OBJ;
}
} else {
wlen = 0;
rlen = datalen;
hdr.opcode = SD_OP_READ_OBJ;
}
hdr.oid = oid;
hdr.data_length = datalen;
hdr.offset = offset;
hdr.copies = copies;
hdr.flags |= SD_FLAG_CMD_TGT;
ret = do_req(ai, (struct sheepdog_req *)&hdr, buf, &wlen, &rlen);
if (ret) {
eprintf("failed to send a request to the sheep\n");
return -1;
}
switch (rsp->result) {
case SD_RES_SUCCESS:
return 0;
case SD_RES_INODE_INVALIDATED:
dprintf("inode object is invalidated\n");
case SD_RES_READONLY:
*need_reload = 1;
return 0;
default:
eprintf("%s (oid: %" PRIx64 ", old_oid: %" PRIx64 ")\n",
sd_strerror(rsp->result), oid, old_oid);
return -1;
}
}
static int read_object(struct sheepdog_access_info *ai, char *buf,
uint64_t oid, int copies,
unsigned int datalen, uint64_t offset, int *need_reload)
{
return read_write_object(ai, buf, oid, copies, datalen, offset,
0, 0, 0, 0, need_reload);
}
static int write_object(struct sheepdog_access_info *ai, char *buf,
uint64_t oid, int copies,
unsigned int datalen, uint64_t offset, int create,
uint64_t old_oid, uint16_t flags, int *need_reload)
{
return read_write_object(ai, buf, oid, copies, datalen, offset, 1,
create, old_oid, flags, need_reload);
}
static int sd_sync(struct sheepdog_access_info *ai)
{
int ret;
struct sheepdog_obj_req hdr;
struct sheepdog_obj_rsp *rsp = (struct sheepdog_obj_rsp *)&hdr;
unsigned int wlen = 0, rlen = 0;
memset(&hdr, 0, sizeof(hdr));
hdr.proto_ver = SD_PROTO_VER;
hdr.opcode = SD_OP_FLUSH_VDI;
hdr.oid = vid_to_vdi_oid(ai->inode.vdi_id);
ret = do_req(ai, (struct sheepdog_req *)&hdr, NULL, &wlen, &rlen);
if (ret) {
eprintf("failed to send a request to the sheep\n");
return -1;
}
switch (rsp->result) {
case SD_RES_SUCCESS:
case SD_RES_INVALID_PARMS:
/*
* SD_RES_INVALID_PARMS means the sheep daemon doesn't use
* object caches
*/
return 0;
default:
eprintf("%s\n", sd_strerror(rsp->result));
return -1;
}
}
static int update_inode(struct sheepdog_access_info *ai)
{
int ret = 0, need_reload_inode = 0;
uint64_t oid = vid_to_vdi_oid(ai->inode.vdi_id);
uint32_t min, max, offset, data_len;
min = ai->min_dirty_data_idx;
max = ai->max_dirty_data_idx;
if (max < min)
goto end;
goto update;
reload:
reload_inode(ai);
need_reload_inode = 0;
update:
offset = sizeof(ai->inode) - sizeof(ai->inode.data_vdi_id) +
min * sizeof(ai->inode.data_vdi_id[0]);
data_len = (max - min + 1) * sizeof(ai->inode.data_vdi_id[0]);
ret = write_object(ai, (char *)&ai->inode + offset, oid,
ai->inode.nr_copies, data_len, offset,
0, 0, 0, &need_reload_inode);
if (ret < 0)
eprintf("sync inode failed\n");
if (need_reload_inode) {
dprintf("reloading inode is required in the path"
" of update_inode()\n");
goto reload;
}
end:
ai->min_dirty_data_idx = UINT32_MAX;
ai->max_dirty_data_idx = 0;
return ret;
}
static int is_refresh_required(struct sheepdog_access_info *ai)
/*
* 0: refresh isn't required
* 1: refresh is required
*/
{
uint64_t inode_oid = vid_to_vdi_oid(ai->inode.vdi_id);
char dummy;
int need_reload_inode = 0;
/*
* Check inode of this tgtd is invaldiated or not.
* The inode object is the only one object which always exists.
*/
read_object(ai, &dummy, inode_oid, ai->inode.nr_copies, sizeof(dummy),
0, &need_reload_inode);
return need_reload_inode;
}
static int sd_io(struct sheepdog_access_info *ai, int write, char *buf, int len,
uint64_t offset)
{
uint32_t vid;
unsigned long idx = offset / SD_DATA_OBJ_SIZE;
unsigned long max =
(offset + len + (SD_DATA_OBJ_SIZE - 1)) / SD_DATA_OBJ_SIZE;
unsigned obj_offset = offset % SD_DATA_OBJ_SIZE;
size_t orig_size, size, rest = len;
int ret = 0, create = 0;
uint64_t oid, old_oid;
uint16_t flags = 0;
int need_update_inode = 0, need_reload_inode;
int nr_copies = ai->inode.nr_copies;
goto do_req;
reload_in_read_path:
pthread_rwlock_unlock(&ai->inode_lock); /* unlock current read lock */
pthread_rwlock_wrlock(&ai->inode_lock);
ret = reload_inode(ai);
if (ret) {
eprintf("failed to reload in read path\n");
goto out;
}
pthread_rwlock_unlock(&ai->inode_lock);
do_req:
if (write)
pthread_rwlock_wrlock(&ai->inode_lock);
else
pthread_rwlock_rdlock(&ai->inode_lock);
for (; idx < max; idx++) {
orig_size = size;
size = SD_DATA_OBJ_SIZE - obj_offset;
size = min_t(size_t, size, rest);
retry:
vid = ai->inode.vdi_id;
oid = vid_to_data_oid(ai->inode.data_vdi_id[idx], idx);
old_oid = 0;
if (write) {
/*
* tgt doesn't affect semantics of caching, so we can
* always turn on cache of sheep layer
*/
flags = SD_FLAG_CMD_CACHE;
if (ai->inode.data_vdi_id[idx] != vid) {
create = 1;
if (ai->inode.data_vdi_id[idx]) {
/* COW */
old_oid = oid;
flags |= SD_FLAG_CMD_COW;
}
oid = vid_to_data_oid(ai->inode.vdi_id, idx);
ai->min_dirty_data_idx =
min_t(uint32_t,
idx, ai->min_dirty_data_idx);
ai->max_dirty_data_idx =
max_t(uint32_t,
idx, ai->max_dirty_data_idx);
ai->inode.data_vdi_id[idx] = vid;
}
need_reload_inode = 0;
ret = write_object(ai, buf + (len - rest),
oid, nr_copies, size,
obj_offset, create,
old_oid, flags, &need_reload_inode);
if (!ret) {
if (need_reload_inode) {
ret = reload_inode(ai);
if (!ret)
goto retry;
}
if (create) {
need_update_inode = 1;
create = 0;
}
}
} else {
if (!ai->inode.data_vdi_id[idx]) {
int check = is_refresh_required(ai);
if (!check) {
memset(buf, 0, size);
goto done;
} else {
dprintf("reload in read path for not"\
" written area\n");
size = orig_size;
goto reload_in_read_path;
}
}
need_reload_inode = 0;
ret = read_object(ai, buf + (len - rest),
oid, nr_copies, size,
obj_offset, &need_reload_inode);
if (need_reload_inode) {
dprintf("reload in ordinal read path\n");
size = orig_size;
goto reload_in_read_path;
}
}
if (ret) {
eprintf("%lu %d\n", idx, ret);
goto out;
}
done:
rest -= size;
obj_offset = 0;
}
if (need_update_inode)
ret = update_inode(ai);
out:
pthread_rwlock_unlock(&ai->inode_lock);
return ret;
}
static int find_vdi_name(struct sheepdog_access_info *ai, char *filename,
uint32_t snapid, char *tag, uint32_t *vid,
int for_snapshot)
{
int ret;
struct sheepdog_vdi_req hdr;
struct sheepdog_vdi_rsp *rsp = (struct sheepdog_vdi_rsp *)&hdr;
unsigned int wlen, rlen = 0;
char buf[SD_MAX_VDI_LEN + SD_MAX_VDI_TAG_LEN];
memset(buf, 0, sizeof(buf));
strncpy(buf, filename, SD_MAX_VDI_LEN - 1);
strncpy(buf + SD_MAX_VDI_LEN, tag, SD_MAX_VDI_TAG_LEN - 1);
memset(&hdr, 0, sizeof(hdr));
if (for_snapshot)
hdr.opcode = SD_OP_GET_VDI_INFO;
else
hdr.opcode = SD_OP_LOCK_VDI;
hdr.type = LOCK_TYPE_SHARED;
wlen = SD_MAX_VDI_LEN + SD_MAX_VDI_TAG_LEN;
hdr.proto_ver = SD_PROTO_VER;
hdr.data_length = wlen;
hdr.snapid = snapid;
hdr.flags = SD_FLAG_CMD_WRITE;
ret = do_req(ai, (struct sheepdog_req *)&hdr, buf, &wlen, &rlen);
if (ret) {
ret = -1;
goto out;
}