Skip to content

Commit 821d46c

Browse files
tools: Add -nz option to zbc_write_zone
Add a command line option to specify the number of consecutive zones to process to zbc_write_zone tool. This option is useful, for example, for filling multiple SOBR zones of XMR drives - these zones don't support FINISH zone operation. Signed-off-by: Dmitry Fomichev <[email protected]>
1 parent bf05f56 commit 821d46c

File tree

1 file changed

+98
-72
lines changed

1 file changed

+98
-72
lines changed

tools/write_zone/zbc_write_zone.c

Lines changed: 98 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,22 @@
2626

2727
#include <libzbc/zbc.h>
2828

29-
static int zbc_write_zone_abort = 0;
29+
static int zbc_write_zone_abort;
30+
31+
static ssize_t zbc_read_pattern_file(char *file, int fd, void *iobuf,
32+
size_t iosize, size_t ios)
33+
{
34+
int ret;
35+
36+
ret = read(fd, iobuf + ios, iosize - ios);
37+
if (ret < 0) {
38+
fprintf(stderr, "Read file \"%s\" failed %d (%s)\n",
39+
file,
40+
errno, strerror(errno));
41+
}
42+
43+
return ret;
44+
}
3045

3146
static inline unsigned long long zbc_write_zone_usec(void)
3247
{
@@ -95,13 +110,13 @@ int main(int argc, char **argv)
95110
char *path, *file = NULL, *end;
96111
long long sector_ofst = 0;
97112
long long sector_max = 0;
98-
long long zone_ofst = 0;
113+
long long zone_ofst = 0, zofst;
99114
bool flush = false, floop = false, vio = false;
100115
unsigned long pattern = 0;
101116
int flags = O_WRONLY;
102117
int oflags = 0;
103118
struct iovec *iov = NULL;
104-
int iovcnt = 1, n;
119+
int iovcnt = 1, n, z, nz = 1;
105120

106121
/* Check command line */
107122
if (argc < 4)
@@ -204,6 +219,18 @@ int main(int argc, char **argv)
204219
return 1;
205220
}
206221

222+
} else if (strcmp(argv[i], "-nz") == 0) {
223+
224+
if (i >= (argc - 1))
225+
goto err;
226+
i++;
227+
228+
nz = atoi(argv[i]);
229+
if (nz < 0) {
230+
fprintf(stderr, "Invalid number of zones\n");
231+
return 1;
232+
}
233+
207234
} else if (argv[i][0] == '-') {
208235

209236
fprintf(stderr, "Unknown option \"%s\"\n", argv[i]);
@@ -273,12 +300,17 @@ int main(int argc, char **argv)
273300
goto out;
274301
}
275302

276-
/* Get target zone */
303+
/* Get the first target zone */
277304
if (zidx >= (int)nr_zones) {
278305
fprintf(stderr, "Target zone not found\n");
279306
ret = 1;
280307
goto out;
281308
}
309+
if (zidx + nz > (int)nr_zones) {
310+
fprintf(stderr, "-nz value is too large\n");
311+
ret = 1;
312+
goto out;
313+
}
282314
iozone = &zones[zidx];
283315

284316
if (zbc_zone_conventional(iozone))
@@ -390,44 +422,38 @@ int main(int argc, char **argv)
390422

391423
}
392424

393-
sector_max = zbc_zone_length(iozone);
394-
if (zbc_zone_sequential(iozone)) {
395-
if (zbc_zone_full(iozone))
396-
sector_max = 0;
397-
else if (zbc_zone_wp(iozone) > zbc_zone_start(iozone))
398-
sector_max =
399-
zbc_zone_wp(iozone) - zbc_zone_start(iozone);
400-
}
401-
402425
elapsed = zbc_write_zone_usec();
403426

404-
while (!zbc_write_zone_abort) {
427+
for (z = 0; z < nz && !zbc_write_zone_abort; z++, iozone++) {
428+
sector_max = zbc_zone_length(iozone);
429+
if (zbc_zone_sequential(iozone)) {
430+
if (zbc_zone_full(iozone))
431+
sector_max = 0;
432+
else if (zbc_zone_wp(iozone) > zbc_zone_start(iozone))
433+
sector_max =
434+
zbc_zone_wp(iozone) - zbc_zone_start(iozone);
435+
}
436+
zofst = zone_ofst;
405437

406-
if (file) {
438+
while (!zbc_write_zone_abort) {
407439

408-
size_t ios;
440+
if (file) {
409441

410-
/* Read file */
411-
ret = read(fd, iobuf, iosize);
412-
if (ret < 0) {
413-
fprintf(stderr, "Read file \"%s\" failed %d (%s)\n",
414-
file,
415-
errno,
416-
strerror(errno));
417-
ret = 1;
418-
break;
419-
}
442+
size_t ios;
443+
444+
/* Read file */
445+
ret = zbc_read_pattern_file(file, fd, iobuf, iosize, 0);
446+
if (ret < 0) {
447+
ret = 1;
448+
break;
449+
}
420450

421-
ios = ret;
422-
if (ios < iosize) {
423-
if (floop) {
451+
ios = ret;
452+
if (ios < iosize && floop) {
424453
/* Rewind and read remaining of buffer */
425454
lseek(fd, 0, SEEK_SET);
426-
ret = read(fd, iobuf + ios, iosize - ios);
455+
ret = zbc_read_pattern_file(file, fd, iobuf, iosize, 0);
427456
if (ret < 0) {
428-
fprintf(stderr, "Read file \"%s\" failed %d (%s)\n",
429-
file,
430-
errno, strerror(errno));
431457
ret = 1;
432458
break;
433459
}
@@ -436,53 +462,53 @@ int main(int argc, char **argv)
436462
/* Clear end of buffer */
437463
memset(iobuf + ios, 0, iosize - ios);
438464
}
439-
}
440465

441-
if (!ios)
442-
/* EOF */
443-
break;
466+
if (!ios)
467+
/* EOF */
468+
break;
444469

445-
}
470+
}
446471

447-
/* Do not exceed the end of the zone */
448-
if (zbc_zone_sequential(iozone) && zbc_zone_full(iozone))
449-
sector_count = 0;
450-
else
451-
sector_count = iosize >> 9;
452-
if (zone_ofst + sector_count > sector_max)
453-
sector_count = sector_max - zone_ofst;
454-
if (!sector_count)
455-
break;
456-
sector_ofst = zbc_zone_start(iozone) + zone_ofst;
457-
458-
/* Write to zone */
459-
if (vio) {
460-
n = zbc_map_iov(iobuf, sector_count,
461-
iov, iovcnt, bufsize >> 9);
462-
if (n < 0) {
463-
fprintf(stderr, "iov map failed %d (%s)\n",
464-
-n, strerror(-n));
472+
/* Do not exceed the end of the zone */
473+
if (zbc_zone_sequential(iozone) && zbc_zone_full(iozone))
474+
sector_count = 0;
475+
else
476+
sector_count = iosize >> 9;
477+
if (zofst + sector_count > sector_max)
478+
sector_count = sector_max - zofst;
479+
if (!sector_count)
480+
break;
481+
sector_ofst = zbc_zone_start(iozone) + zofst;
482+
483+
/* Write to zone */
484+
if (vio) {
485+
n = zbc_map_iov(iobuf, sector_count,
486+
iov, iovcnt, bufsize >> 9);
487+
if (n < 0) {
488+
fprintf(stderr, "iov map failed %d (%s)\n",
489+
-n, strerror(-n));
490+
ret = 1;
491+
goto out;
492+
}
493+
ret = zbc_pwritev(dev, iov, n, sector_ofst);
494+
} else {
495+
ret = zbc_pwrite(dev, iobuf, sector_count, sector_ofst);
496+
}
497+
if (ret <= 0) {
498+
fprintf(stderr, "%s failed %zd (%s)\n",
499+
vio ? "zbc_pwritev" : "zbc_pwrite",
500+
-ret, strerror(-ret));
465501
ret = 1;
466502
goto out;
467503
}
468-
ret = zbc_pwritev(dev, iov, n, sector_ofst);
469-
} else {
470-
ret = zbc_pwrite(dev, iobuf, sector_count, sector_ofst);
471-
}
472-
if (ret <= 0) {
473-
fprintf(stderr, "%s failed %zd (%s)\n",
474-
vio ? "zbc_pwritev" : "zbc_pwrite",
475-
-ret, strerror(-ret));
476-
ret = 1;
477-
goto out;
478-
}
479504

480-
zone_ofst += ret;
481-
bcount += ret << 9;
482-
iocount++;
505+
zofst += ret;
506+
bcount += ret << 9;
507+
iocount++;
483508

484-
if (ionum > 0 && iocount >= ionum)
485-
break;
509+
if (ionum > 0 && iocount >= ionum)
510+
break;
511+
}
486512
}
487513

488514
if (flush) {

0 commit comments

Comments
 (0)