forked from open-iscsi/tcmu-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_zbc.c
2332 lines (1939 loc) · 51.5 KB
/
file_zbc.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 2017, Western Digital Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
/*
* ZBC device emulation with a file backstore.
*/
#define _GNU_SOURCE
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <endian.h>
#include <errno.h>
#include <scsi/scsi.h>
#include <linux/types.h>
#include "scsi_defs.h"
#include "libtcmu.h"
#include "tcmu-runner.h"
/*
* SCSI commands.
*/
#define ZBC_OUT 0x94
#define ZBC_IN 0x95
/*
* ZBC IN/OUT Sevice Actions
*/
#define ZBC_SA_REPORT_ZONES 0x00
#define ZBC_SA_CLOSE_ZONE 0x01
#define ZBC_SA_FINISH_ZONE 0x02
#define ZBC_SA_OPEN_ZONE 0x03
#define ZBC_SA_RESET_WP 0x04
/*
* ZBC related additional sense codes.
*/
#define ASC_UNALIGNED_WRITE_COMMAND 0x2104
#define ASC_WRITE_BOUNDARY_VIOLATION 0x2105
#define ASC_ATTEMPT_TO_READ_INVALID_DATA 0x2106
#define ASC_READ_BOUNDARY_VIOLATION 0x2107
#define ASC_INSUFFICIENT_ZONE_RESOURCES 0x550E
/*
* Device zone model.
*/
enum zbc_dev_model {
ZBC_HA = 0x00,
ZBC_HM = 0x14,
};
/*
* Zone types.
*/
enum zbc_zone_type {
ZBC_ZONE_TYPE_CONVENTIONAL = 0x1,
ZBC_ZONE_TYPE_SEQWRITE_REQ = 0x2,
ZBC_ZONE_TYPE_SEQWRITE_PREF = 0x3,
};
/*
* Zone conditions.
*/
enum zbc_zone_cond {
ZBC_ZONE_COND_NOT_WP = 0x0,
ZBC_ZONE_COND_EMPTY = 0x1,
ZBC_ZONE_COND_IMP_OPEN = 0x2,
ZBC_ZONE_COND_EXP_OPEN = 0x3,
ZBC_ZONE_COND_CLOSED = 0x4,
ZBC_ZONE_COND_READONLY = 0xD,
ZBC_ZONE_COND_FULL = 0xE,
ZBC_ZONE_COND_OFFLINE = 0xF,
};
/*
* Metadata zone descriptor.
*/
struct zbc_zone {
__u64 start; /* Zone start sector */
__u64 len; /* Zone length in number of sectors */
__u64 wp; /* Zone write pointer position */
__u8 type; /* Zone type */
__u8 cond; /* Zone condition */
__u8 non_seq; /* Non-sequential write resources active */
__u8 reset; /* Reset write pointer recommended */
__u8 reserved[36];
};
/*
* Test zone type.
*/
#define zbc_zone_conv(z) ((z)->type == ZBC_ZONE_TYPE_CONVENTIONAL)
#define zbc_zone_seq_req(z) ((z)->type == ZBC_ZONE_TYPE_SEQWRITE_REQ)
#define zbc_zone_seq_pref(z) ((z)->type == ZBC_ZONE_TYPE_SEQWRITE_PREF)
#define zbc_zone_seq(z) (!zbc_zone_conv(z))
/*
* Test zone conditions.
*/
#define zbc_zone_empty(z) ((z)->cond == ZBC_ZONE_COND_EMPTY)
#define zbc_zone_full(z) ((z)->cond == ZBC_ZONE_COND_FULL)
#define zbc_zone_imp_open(z) ((z)->cond == ZBC_ZONE_COND_IMP_OPEN)
#define zbc_zone_exp_open(z) ((z)->cond == ZBC_ZONE_COND_EXP_OPEN)
#define zbc_zone_is_open(z) (zbc_zone_imp_open(z) || zbc_zone_exp_open(z))
#define zbc_zone_closed(z) ((z)->cond == ZBC_ZONE_COND_CLOSED)
#define zbc_zone_not_wp(z) ((z)->cond == ZBC_ZONE_COND_NOT_WP)
#define zbc_zone_closed(z) ((z)->cond == ZBC_ZONE_COND_CLOSED)
#define zbc_zone_offline(z) ((z)->cond == ZBC_ZONE_COND_OFFLINE)
#define zbc_zone_rdonly(z) ((z)->cond == ZBC_ZONE_COND_READONLY)
#define zbc_zone_rwp(z) ((z)->reset)
#define zbc_zone_non_seq(z) ((z)->non_seq)
/*
* Reporting options.
*/
enum zbc_reporting_options {
/* List all of the zones in the device */
ZBC_RO_ALL = 0x00,
/* List the zones with a Zone Condition of EMPTY */
ZBC_RO_EMPTY = 0x01,
/* List the zones with a Zone Condition of IMPLICIT OPEN */
ZBC_RO_IMP_OPEN = 0x02,
/* List the zones with a Zone Condition of EXPLICIT OPEN */
ZBC_RO_EXP_OPEN = 0x03,
/* List the zones with a Zone Condition of CLOSED */
ZBC_RO_CLOSED = 0x04,
/* List the zones with a Zone Condition of FULL */
ZBC_RO_FULL = 0x05,
/* List the zones with a Zone Condition of READ ONLY */
ZBC_RO_READONLY = 0x06,
/* List the zones with a Zone Condition of OFFLINE */
ZBC_RO_OFFLINE = 0x07,
/* 08h to 0Fh Reserved */
/* List the zones with a zone attribute RESET WP RECOMMENDED set */
ZBC_RO_RWP_RECOMMENDED = 0x10,
/* List the zones with a zone attribute NON_SEQ set */
ZBC_RO_NON_SEQ = 0x11,
/* 12h to 3Eh Reserved */
/* List of the zones with a Zone Condition of NOT WP */
ZBC_RO_NOT_WP = 0x3f,
/* Partial report flag */
ZBC_RO_PARTIAL = 0x80,
};
/*
* Metadata magic.
*/
#define ZBC_MAGIC ((__u32)'U' << 24 | \
(__u32)'Z' << 16 | \
(__u32)'B' << 8 | \
(__u32)'C')
/*
* Disk parameters (metadata).
*/
struct zbc_meta {
/* Magic */
__u32 magic;
/* Device zone model */
__u32 model;
/* Device size (LBAs) */
__u64 capacity;
/* LBA size (B) */
__u32 lba_size;
/* Zone size in (LBAs) */
__u32 zone_size;
/* Number of zones */
__u32 nr_zones;
/* Number of conventional zones */
__u32 nr_conv_zones;
/* Maximum/optimal number of open zones */
__u32 nr_open_zones;
/* Number of implicitly open zones */
__u32 nr_imp_open;
/* Number of explicitly open zones */
__u32 nr_exp_open;
};
/*
* Emulated device configuration.
* Values come from parsing the configuration string, except for the device size
* which is obtained using tcmu_get_device_size().
*/
struct zbc_dev_config {
/* Backstore file path */
char *path;
/* Device size in bytes */
long long dev_size;
/* Configuration options */
bool need_format;
enum zbc_dev_model model;
size_t lba_size;
size_t zone_size;
unsigned int conv_num;
unsigned int open_num;
};
#define ZBC_CONF_DEFAULT_MODEL ZBC_HM
#define ZBC_CONF_DEFAULT_ZSIZE (256 * 1024 * 1024)
#define ZBC_CONF_DEFAULT_LBA_SIZE 512
#define ZBC_CONF_DEFAULT_CONV_NUM (unsigned int)(-1)
#define ZBC_CONF_DEFAULT_OPEN_NUM 128
/*
* Emulated device descriptor private data.
*/
struct zbc_dev {
struct tcmu_device *dev;
struct zbc_dev_config cfg;
int fd;
size_t meta_size;
struct zbc_meta *meta;
enum zbc_dev_model model;
unsigned long long capacity;
size_t lba_size;
size_t zone_size;
struct zbc_zone *zones;
unsigned int nr_zones;
unsigned int nr_conv_zones;
unsigned int nr_open_zones;
unsigned int nr_imp_open;
unsigned int nr_exp_open;
};
static char *zbc_parse_model(char *val, struct zbc_dev_config *cfg, char **msg)
{
/* Device model */
if (strncmp(val, "HA", 2) == 0) {
cfg->model = ZBC_HA;
return val + 2;
}
if (strncmp(val, "HM", 2) == 0) {
cfg->model = ZBC_HM;
return val + 2;
}
*msg = "Invalid device model";
return NULL;
}
static char *zbc_parse_lba(char *val, struct zbc_dev_config *cfg, char **msg)
{
char *end;
cfg->lba_size = strtoul(val, &end, 10);
if (cfg->lba_size != 512 && cfg->lba_size != 4096) {
*msg = "Invalid LBA size";
return NULL;
}
return end;
}
static char *zbc_parse_zsize(char *val, struct zbc_dev_config *cfg, char **msg)
{
char *end;
cfg->zone_size = strtoul(val, &end, 10) * 1024 * 1024;
if (!cfg->zone_size ||
(cfg->zone_size & (cfg->zone_size - 1))) {
*msg = "Invalid zone size";
return NULL;
}
return end;
}
static char *zbc_parse_conv(char *val, struct zbc_dev_config *cfg, char **msg)
{
char *end;
cfg->conv_num = strtoul(val, &end, 10);
return end;
}
static char *zbc_parse_open(char *val, struct zbc_dev_config *cfg, char **msg)
{
char *end;
cfg->open_num = strtoul(val, &end, 10);
if (!cfg->open_num) {
*msg = "Invalid number of open zones";
return NULL;
}
return end;
}
#define ZBC_PARAMS 5
struct zbc_dev_config_param {
char *name;
char *(*parse)(char *, struct zbc_dev_config *, char **);
} zbc_params[ZBC_PARAMS] = {
{ "model-", zbc_parse_model },
{ "lba-", zbc_parse_lba },
{ "zsize-", zbc_parse_zsize },
{ "conv-", zbc_parse_conv },
{ "open-", zbc_parse_open },
};
/*
* Get emulated device parameters form the backstore file name
* in the configuration string.
*/
static bool zbc_parse_config(const char *cfgstring, struct zbc_dev_config *cfg,
char **reason)
{
struct stat st;
char *str, *msg = NULL;
int i, ret;
/*
* Set default config: 256 MB zone size host managed device,
* 128 maximum open zones.
*/
memset(cfg, 0, sizeof(struct zbc_dev_config));
cfg->model = ZBC_CONF_DEFAULT_MODEL;
cfg->lba_size = ZBC_CONF_DEFAULT_LBA_SIZE;
cfg->zone_size = ZBC_CONF_DEFAULT_ZSIZE;
cfg->conv_num = ZBC_CONF_DEFAULT_CONV_NUM;
cfg->open_num = ZBC_CONF_DEFAULT_OPEN_NUM;
if (strncmp(cfgstring, "zbc/", 4) != 0)
goto err;
str = (char *)cfgstring + 4;
if (*str != '/') {
/* Parse option parameters */
while (*str && *str != '@') {
for (i = 0; i < ZBC_PARAMS; i++) {
if (strncmp(str, zbc_params[i].name,
strlen(zbc_params[i].name)) == 0)
break;
}
if (i >= ZBC_PARAMS) {
msg = "Invalid option name";
goto failed;
}
str += strlen(zbc_params[i].name);
str = (zbc_params[i].parse)(str, cfg, &msg);
if (!str)
goto failed;
if (*str != '/')
break;
str++;
}
if (*str != '@')
goto err;
str++;
/* Options were specified */
cfg->need_format = true;
}
cfg->path = strdup(str);
if (!cfg->path) {
msg = "Failed to get path";
goto failed;
}
/* Get stats */
ret = stat(cfg->path, &st);
if (ret && errno == ENOENT)
/* New file: reformat */
cfg->need_format = true;
return true;
err:
msg = "Invalid configuration string format";
failed:
if (!msg || asprintf(reason, "%s", msg) == -1)
*reason = NULL;
return false;
}
/*
* Return metadata size;
*/
static size_t zbc_meta_size(unsigned int nr_zones)
{
return sizeof(struct zbc_meta) + nr_zones * sizeof(struct zbc_zone);
}
/*
* Same, aligned up on the system page size.
*/
static size_t zbc_meta_size_aligned(unsigned int nr_zones)
{
size_t meta_size = zbc_meta_size(nr_zones);
size_t pg_size = sysconf(_SC_PAGESIZE) - 1;
return (meta_size + pg_size - 1) & (~(pg_size - 1));
}
/*
* Mmap the metadata portion of the backstore file.
*/
static int zbc_map_meta(struct zbc_dev *zdev)
{
int ret;
/* Mmap metadata */
zdev->meta = mmap(NULL, zdev->meta_size, PROT_READ | PROT_WRITE,
MAP_SHARED, zdev->fd, 0);
if (zdev->meta == MAP_FAILED) {
ret = -errno;
tcmu_dev_err(zdev->dev, "mmap %s failed (%m)\n",
zdev->cfg.path);
zdev->meta = NULL;
return ret;
}
zdev->zones = (struct zbc_zone *)(zdev->meta + 1);
tcmu_dev_dbg(zdev->dev, "Mapped %zu B of metadata at %p\n",
zdev->meta_size, zdev->meta);
return 0;
}
/*
* Unmap the metadata portion of the backstore file.
*/
static void zbc_unmap_meta(struct zbc_dev *zdev)
{
if (zdev->meta) {
munmap(zdev->meta, zdev->meta_size);
zdev->meta = NULL;
}
}
/*
* Flush metadata.
*/
static int zbc_flush_meta(struct zbc_dev *zdev)
{
int ret;
ret = msync(zdev->meta, zdev->meta_size, MS_SYNC | MS_INVALIDATE);
if (ret) {
ret = -errno;
tcmu_dev_err(zdev->dev, "msync metadata failed (%m)\n");
return ret;
}
return 0;
}
/*
* Check a zone metadata.
*/
static bool zbc_check_zone(struct zbc_dev *zdev,
struct zbc_meta *meta,
unsigned int zno)
{
struct zbc_zone zone;
ssize_t ret;
ret = pread(zdev->fd, &zone, sizeof(struct zbc_zone),
sizeof(struct zbc_meta) + zno * sizeof(struct zbc_zone));
if (ret != sizeof(struct zbc_zone))
return false;
switch (zone.type) {
case ZBC_ZONE_TYPE_CONVENTIONAL:
case ZBC_ZONE_TYPE_SEQWRITE_PREF:
case ZBC_ZONE_TYPE_SEQWRITE_REQ:
break;
default:
return false;
}
if (zbc_zone_seq_pref(&zone) && meta->model != ZBC_HA)
return false;
if (zbc_zone_seq_req(&zone) && meta->model != ZBC_HM)
return false;
if (zbc_zone_conv(&zone) && zone.cond != ZBC_ZONE_COND_NOT_WP)
return false;
if (zone.start % meta->zone_size ||
zone.len > meta->zone_size)
return false;
return true;
}
/*
* Check metadata.
* Return true if the metadata is correct and can be used without reformatting.
*/
static bool zbc_check_meta(struct zbc_dev *zdev, struct stat *st)
{
struct zbc_meta meta;
unsigned int i, nr_zones;
ssize_t ret;
ret = pread(zdev->fd, &meta, sizeof(struct zbc_meta), 0);
if (ret != sizeof(struct zbc_meta))
return false;
if (meta.magic != ZBC_MAGIC)
return false;
if (meta.model != ZBC_HM && meta.model != ZBC_HA)
return false;
if (meta.lba_size != 512 && meta.lba_size != 4096)
return false;
if (meta.capacity * meta.lba_size != zdev->cfg.dev_size)
return false;
if (!meta.zone_size ||
meta.zone_size & (meta.zone_size - 1))
return false;
nr_zones = (meta.capacity + meta.zone_size - 1) / meta.zone_size;
if (meta.nr_zones != nr_zones ||
meta.nr_conv_zones >= nr_zones ||
meta.nr_open_zones > nr_zones)
return false;
zdev->meta_size = zbc_meta_size_aligned(nr_zones);
if (st->st_size != zdev->meta_size + zdev->cfg.dev_size)
return false;
/* Check all zones */
for (i = 0; i < nr_zones; i++) {
if (!zbc_check_zone(zdev, &meta, i)) {
tcmu_dev_err(zdev->dev, "Invalid zone %u\n", i);
return false;
}
}
zdev->model = meta.model;
zdev->capacity = meta.capacity;
zdev->lba_size = meta.lba_size;
zdev->zone_size = meta.zone_size;
zdev->nr_zones = meta.nr_zones;
zdev->nr_conv_zones = meta.nr_conv_zones;
zdev->nr_open_zones = meta.nr_open_zones;
return true;
}
/*
* Format metadata.
*/
static int zbc_format_meta(struct zbc_dev *zdev)
{
struct zbc_dev_config *cfg = &zdev->cfg;
struct zbc_meta *meta;
struct zbc_zone *zone;
unsigned int nr_seq_zones;
__u64 lba = 0;
unsigned int i;
int ret;
zdev->model = cfg->model;
zdev->lba_size = cfg->lba_size;
zdev->capacity = cfg->dev_size / zdev->lba_size;
zdev->zone_size = cfg->zone_size / zdev->lba_size;
zdev->nr_zones = (zdev->capacity + zdev->zone_size - 1) /
zdev->zone_size;
if (cfg->conv_num == ZBC_CONF_DEFAULT_CONV_NUM) {
/* Default: 1% of the capacity as conventional zones */
zdev->nr_conv_zones = zdev->nr_zones / 100;
if (!zdev->nr_conv_zones)
zdev->nr_conv_zones = 1;
} else {
zdev->nr_conv_zones = cfg->conv_num;
if (zdev->nr_conv_zones >= zdev->nr_zones) {
tcmu_dev_err(zdev->dev,
"Too many conventional zones\n");
return -ENOSPC;
}
}
zdev->nr_open_zones = cfg->open_num;
nr_seq_zones = zdev->nr_zones - zdev->nr_conv_zones;
if (zdev->nr_open_zones >= nr_seq_zones / 2) {
zdev->nr_open_zones = nr_seq_zones / 2;
if (!zdev->nr_open_zones)
zdev->nr_open_zones = 1;
}
tcmu_dev_dbg(zdev->dev, "Formatting...\n");
tcmu_dev_dbg(zdev->dev, " Model: %s\n",
cfg->model == ZBC_HM ? "HM" : "HA");
tcmu_dev_dbg(zdev->dev, " LBA size: %zu B\n",
cfg->lba_size);
tcmu_dev_dbg(zdev->dev, " Zone size: %zu MiB\n",
cfg->zone_size);
tcmu_dev_dbg(zdev->dev, " Number of conventional zones: %u\n",
zdev->nr_conv_zones);
tcmu_dev_dbg(zdev->dev, " Number of open zones: %u\n",
cfg->open_num);
/* Truncate file */
zdev->meta_size = zbc_meta_size_aligned(zdev->nr_zones);
ret = ftruncate(zdev->fd, zdev->meta_size + cfg->dev_size);
if (ret < 0) {
ret = -errno;
tcmu_dev_err(zdev->dev, "Truncate %s failed (%m)\n",
cfg->path);
return ret;
}
/* Mmap metadata */
ret = zbc_map_meta(zdev);
if (ret)
return ret;
/* Write metadata */
meta = zdev->meta;
memset(meta, 0, zdev->meta_size);
meta->magic = ZBC_MAGIC;
meta->model = zdev->model;
meta->capacity = zdev->capacity;
meta->lba_size = zdev->lba_size;
meta->zone_size = zdev->zone_size;
meta->nr_zones = zdev->nr_zones;
meta->nr_conv_zones = zdev->nr_conv_zones;
meta->nr_open_zones = zdev->nr_open_zones;
/* Initialize zones */
zone = zdev->zones;
for (i = 0; i < zdev->nr_zones; i++) {
zone->start = lba;
if (zone->start + meta->zone_size > meta->capacity)
zone->len = meta->capacity - zone->start;
else
zone->len = meta->zone_size;
if (i < zdev->nr_conv_zones) {
zone->wp = ULLONG_MAX;
zone->type = ZBC_ZONE_TYPE_CONVENTIONAL;
zone->cond = ZBC_ZONE_COND_NOT_WP;
} else {
zone->wp = zone->start;
if (meta->model == ZBC_HA)
zone->type = ZBC_ZONE_TYPE_SEQWRITE_PREF;
else
zone->type = ZBC_ZONE_TYPE_SEQWRITE_REQ;
zone->cond = ZBC_ZONE_COND_EMPTY;
}
lba += zone->len;
zone++;
}
ret = zbc_flush_meta(zdev);
if (ret) {
zbc_unmap_meta(zdev);
return ret;
}
return 0;
}
static void __zbc_close_zone(struct zbc_dev *zdev, struct zbc_zone *zone);
/*
* Initialize metadata.
*/
static int zbc_init_meta(struct zbc_dev *zdev)
{
struct zbc_zone *zone;
unsigned int i;
int ret;
/* Mmap metadata */
ret = zbc_map_meta(zdev);
if (ret)
return ret;
/* Close all zones */
zone = zdev->zones;
for (i = 0; i < zdev->nr_zones; i++) {
__zbc_close_zone(zdev, zone);
zone++;
}
zdev->nr_imp_open = 0;
zdev->nr_exp_open = 0;
return 0;
}
/*
* Open the emulated backstore file.
* If the file does not exist, it is created and metadata formatted.
*/
static int zbc_open_backstore(struct tcmu_device *dev)
{
struct zbc_dev *zdev = tcmu_get_dev_private(dev);
struct zbc_dev_config *cfg = &zdev->cfg;
struct stat st;
int ret;
/* Get stats */
ret = stat(cfg->path, &st);
if (ret && errno == ENOENT) {
zdev->cfg.need_format = true;
tcmu_dev_dbg(dev, "New backstore file %s\n", cfg->path);
} else {
tcmu_dev_dbg(dev, "Using backstore file %s\n", cfg->path);
if (!S_ISREG(st.st_mode)) {
tcmu_dev_err(dev, "%s is not a regular file\n",
cfg->path);
return -EINVAL;
}
}
/* Open the file */
zdev->fd = open(cfg->path, O_CREAT | O_RDWR | O_LARGEFILE,
S_IRUSR | S_IWUSR);
if (zdev->fd == -1) {
ret = -errno;
tcmu_dev_err(dev, "Open %s failed (%m)\n", cfg->path);
return ret;
}
if (!zbc_check_meta(zdev, &st))
cfg->need_format = true;
if (cfg->need_format)
ret = zbc_format_meta(zdev);
else
ret = zbc_init_meta(zdev);
if (ret)
goto err;
tcmu_set_dev_block_size(dev, zdev->lba_size);
tcmu_set_dev_num_lbas(dev, zdev->capacity);
tcmu_dev_dbg(dev,
"%s: Host %s zone model\n",
cfg->path,
zdev->model == ZBC_HA ? "aware" : "managed");
tcmu_dev_dbg(dev,
"%llu 512-bytes sectors\n",
(zdev->capacity * zdev->lba_size) >> 9);
tcmu_dev_dbg(dev,
"%llu logical blocks of %u B\n",
(unsigned long long) zdev->capacity,
(unsigned int) zdev->lba_size);
tcmu_dev_dbg(dev,
"%.03F GB capacity\n",
(double)(zdev->capacity * zdev->lba_size) / 1000000000);
tcmu_dev_dbg(dev,
"%u zones of %zu 512-bytes sectors (%zu LBAs)\n",
zdev->nr_zones, (zdev->zone_size * zdev->lba_size) >> 9,
zdev->zone_size);
tcmu_dev_dbg(dev,
"%u conventional zones\n",
zdev->nr_conv_zones);
if (zdev->model == ZBC_HM) {
tcmu_dev_dbg(dev,
"Maximum number of open sequential write required zones: %u\n",
zdev->nr_open_zones);
} else {
tcmu_dev_dbg(dev,
"Optimal number of open sequential write preferred zones: %u\n",
zdev->nr_open_zones);
tcmu_dev_dbg(dev,
"Optimal number of non-sequentially written sequential write preferred zones: %u\n",
zdev->nr_open_zones);
}
return 0;
err:
close(zdev->fd);
return ret;
}
/*
* Ready the emulated device.
*/
static int zbc_open(struct tcmu_device *dev, bool reopen)
{
struct zbc_dev *zdev;
char *err = NULL;
int ret;
tcmu_dev_dbg(dev, "Configuration string: %s\n",
tcmu_get_dev_cfgstring(dev));
zdev = calloc(1, sizeof(*zdev));
if (!zdev)
return -ENOMEM;
tcmu_set_dev_private(dev, zdev);
zdev->dev = dev;
/* Parse config */
if (!zbc_parse_config(tcmu_get_dev_cfgstring(dev), &zdev->cfg, &err)) {
if (err) {
tcmu_dev_err(dev, "%s\n", err);
free(err);
}
ret = -EINVAL;
goto err;
}
/* Get device capacity */
zdev->cfg.dev_size = tcmu_get_dev_size(dev);
if (zdev->cfg.dev_size == -1) {
tcmu_dev_err(dev, "Could not get device size\n");
ret = -ENODEV;
goto err;
}
/* Open the backstore file */
ret = zbc_open_backstore(dev);
if (ret)
goto err;
return 0;
err:
free(zdev->cfg.path);
free(zdev);
return ret;
}
/*
* Cleanup resources used by the emulated device.
*/
static void zbc_close(struct tcmu_device *dev)
{
struct zbc_dev *zdev = tcmu_get_dev_private(dev);
zbc_unmap_meta(zdev);
close(zdev->fd);
free(zdev->cfg.path);
free(zdev);
}
/*
* VPD page inquiry.
*/
static int zbc_evpd_inquiry(struct tcmu_device *dev, struct tcmulib_cmd *cmd)
{
struct zbc_dev *zdev = tcmu_get_dev_private(dev);
uint8_t *cdb = cmd->cdb;
struct iovec *iovec = cmd->iovec;
size_t iov_cnt = cmd->iov_cnt;
size_t len;
uint8_t data[512];
char *ptr;
size_t used = 0;
char *wwn, *p;
bool next = true;
int block_size;
int max_xfer_len;
uint16_t val16;
uint32_t val32;
uint64_t val64;
int i;
memset(data, 0, sizeof(data));
data[0] = zdev->meta->model;
data[1] = cdb[2];
switch (cdb[2]) {
case 0x00:
/* Supported VPD pages */
data[3] = 5;
data[4] = 0x83;
data[5] = 0xb0;
data[6] = 0xb1;
data[7] = 0xb6;
tcmu_memcpy_into_iovec(iovec, iov_cnt, data, 8);
break;
case 0x83:
/* Device identification */
wwn = tcmu_get_wwn(dev);
if (!wwn)
return tcmu_set_sense_data(cmd->sense_buf,
HARDWARE_ERROR,
ASC_INTERNAL_TARGET_FAILURE,
NULL);
ptr = (char *)&data[4];
/* 1/5: T10 Vendor id */
ptr[0] = 2; /* code set: ASCII */
ptr[1] = 1; /* identifier: T10 vendor id */
memcpy(&ptr[4], "LIO-ORG ", 8);
len = snprintf(&ptr[12], sizeof(data) - 16, "%s", wwn);
ptr[3] = 8 + len + 1;
used += (uint8_t)ptr[3] + 4;
ptr += used;
/* 2/5: NAA binary */
ptr[0] = 1; /* code set: binary */
ptr[1] = 3; /* identifier: NAA */
ptr[3] = 16; /* body length for naa registered extended format */
/*
* Set type 6 and use OpenFabrics IEEE Company ID: 00 14 05
*/
ptr[4] = 0x60;
ptr[5] = 0x01;
ptr[6] = 0x40;
ptr[7] = 0x50;
/*
* Fill in the rest with a binary representation of WWN
*
* This implementation only uses a nibble out of every byte of
* WWN, but this is what the kernel does, and it's nice for our
* values to match.