]> git.ipfire.org Git - thirdparty/util-linux.git/blame - sys-utils/blkzone.c
Merge branch 'fix-unnecessary-remake' of https://github.com/ChenQi1989/util-linux
[thirdparty/util-linux.git] / sys-utils / blkzone.c
CommitLineData
1ad8ef91
KZ
1/*
2 * blkzone.c -- the block device zone commands
3 *
4 * Copyright (C) 2015,2016 Seagate Technology PLC
5 * Written by Shaun Tancheff <shaun.tancheff@seagate.com>
6 *
7 * Copyright (C) 2017 Karel Zak <kzak@redhat.com>
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22#include <string.h>
23#include <unistd.h>
24#include <stdlib.h>
25#include <stdio.h>
26#include <stdint.h>
e5ad3a6a 27#include <stdbool.h>
1ad8ef91
KZ
28#include <fcntl.h>
29#include <limits.h>
30#include <getopt.h>
31#include <time.h>
32
33#include <sys/ioctl.h>
34#include <sys/stat.h>
35#include <sys/time.h>
36#include <linux/fs.h>
37#include <linux/blkzoned.h>
38
39#include "nls.h"
40#include "strutils.h"
41#include "xalloc.h"
42#include "c.h"
43#include "closestream.h"
44#include "blkdev.h"
45#include "sysfs.h"
c16970f7 46#include "optutils.h"
1ad8ef91 47
0cecae9c
AR
48/*
49 * These ioctls are defined in linux/blkzoned.h starting with kernel 5.5.
50 */
51#ifndef BLKOPENZONE
52#define BLKOPENZONE _IOW(0x12, 134, struct blk_zone_range)
53#endif
54#ifndef BLKCLOSEZONE
55#define BLKCLOSEZONE _IOW(0x12, 135, struct blk_zone_range)
56#endif
57#ifndef BLKFINISHZONE
58#define BLKFINISHZONE _IOW(0x12, 136, struct blk_zone_range)
59#endif
60
1ad8ef91
KZ
61struct blkzone_control;
62
63static int blkzone_report(struct blkzone_control *ctl);
0cecae9c 64static int blkzone_action(struct blkzone_control *ctl);
1ad8ef91
KZ
65
66struct blkzone_command {
67 const char *name;
68 int (*handler)(struct blkzone_control *);
46cf6625
DLM
69 unsigned long ioctl_cmd;
70 const char *ioctl_name;
1ad8ef91
KZ
71 const char *help;
72};
73
74struct blkzone_control {
75 const char *devname;
76 const struct blkzone_command *command;
77
78 uint64_t total_sectors;
79 int secsize;
80
81 uint64_t offset;
82 uint64_t length;
f1b8b84d 83 uint32_t count;
1ad8ef91 84
6e103c76 85 unsigned int force : 1;
1ad8ef91
KZ
86 unsigned int verbose : 1;
87};
88
89static const struct blkzone_command commands[] = {
46cf6625
DLM
90 {
91 .name = "report",
92 .handler = blkzone_report,
93 .help = N_("Report zone information about the given device")
049859a6
HH
94 },{
95 .name = "capacity",
96 .handler = blkzone_report,
97 .help = N_("Report sum of zone capacities for the given device")
46cf6625
DLM
98 },{
99 .name = "reset",
100 .handler = blkzone_action,
101 .ioctl_cmd = BLKRESETZONE,
102 .ioctl_name = "BLKRESETZONE",
103 .help = N_("Reset a range of zones.")
104 },{
105 .name = "open",
106 .handler = blkzone_action,
107 .ioctl_cmd = BLKOPENZONE,
108 .ioctl_name = "BLKOPENZONE",
109 .help = N_("Open a range of zones.")
110 },{
111 .name = "close",
112 .handler = blkzone_action,
113 .ioctl_cmd = BLKCLOSEZONE,
114 .ioctl_name = "BLKCLOSEZONE",
115 .help = N_("Close a range of zones.")
116 },{
117 .name = "finish",
118 .handler = blkzone_action,
119 .ioctl_cmd = BLKFINISHZONE,
120 .ioctl_name = "BLKFINISHZONE",
121 .help = N_("Set a range of zones to Full.")
122 }
1ad8ef91
KZ
123};
124
125static const struct blkzone_command *name_to_command(const char *name)
126{
127 size_t i;
128
129 for (i = 0; i < ARRAY_SIZE(commands); i++) {
130 if (strcmp(commands[i].name, name) == 0)
131 return &commands[i];
132 }
133
134 return NULL;
135}
136
137static int init_device(struct blkzone_control *ctl, int mode)
138{
139 struct stat sb;
140 int fd;
141
142 fd = open(ctl->devname, mode);
143 if (fd < 0)
144 err(EXIT_FAILURE, _("cannot open %s"), ctl->devname);
145
146 if (fstat(fd, &sb) == -1)
147 err(EXIT_FAILURE, _("stat of %s failed"), ctl->devname);
148 if (!S_ISBLK(sb.st_mode))
149 errx(EXIT_FAILURE, _("%s: not a block device"), ctl->devname);
150
151 if (blkdev_get_sectors(fd, (unsigned long long *) &ctl->total_sectors))
152 err(EXIT_FAILURE, _("%s: blkdev_get_sectors ioctl failed"), ctl->devname);
153
154 if (blkdev_get_sector_size(fd, &ctl->secsize))
155 err(EXIT_FAILURE, _("%s: BLKSSZGET ioctl failed"), ctl->devname);
156
157 return fd;
158}
159
6e1958d8
DLM
160/*
161 * Get the device zone size indicated by chunk sectors).
162 */
163static unsigned long blkdev_chunk_sectors(const char *dname)
164{
4448b3ab
KZ
165 struct path_cxt *pc = NULL;
166 dev_t devno = sysfs_devname_to_devno(dname);
ddf287b4 167 dev_t disk;
4448b3ab 168 uint64_t sz = 0;
6e1958d8
DLM
169 int rc;
170
171 /*
172 * Mapping /dev/sdXn -> /sys/block/sdX to read the chunk_size entry.
173 * This method masks off the partition specified by the minor device
174 * component.
175 */
4448b3ab
KZ
176 pc = ul_new_sysfs_path(devno, NULL, NULL);
177 if (!pc)
ddf287b4
KZ
178 return 0;
179
4448b3ab
KZ
180 rc = sysfs_blkdev_get_wholedisk(pc, NULL, 0, &disk);
181 if (rc != 0)
182 goto done;
6e1958d8 183
4448b3ab
KZ
184 /* if @pc is not while-disk device, switch to disk */
185 if (devno != disk) {
186 rc = sysfs_blkdev_init_path(pc, disk, NULL);
187 if (rc != 0)
188 goto done;
189 }
6e1958d8 190
4448b3ab
KZ
191 rc = ul_path_read_u64(pc, &sz, "queue/chunk_sectors");
192done:
193 ul_unref_path(pc);
6e1958d8
DLM
194 return rc == 0 ? sz : 0;
195}
196
e5ad3a6a
SK
197#if HAVE_DECL_BLK_ZONE_REP_CAPACITY
198#define has_zone_capacity(zi) ((zi)->flags & BLK_ZONE_REP_CAPACITY)
199#define zone_capacity(z) (z)->capacity
200#else
201#define has_zone_capacity(zi) (false)
202#define zone_capacity(z) (z)->len
203#endif
204
1ad8ef91
KZ
205/*
206 * blkzone report
207 */
f1b8b84d 208#define DEF_REPORT_LEN (1U << 12) /* 4k zones per report (256k kzalloc) */
1ad8ef91
KZ
209
210static const char *type_text[] = {
211 "RESERVED",
212 "CONVENTIONAL",
213 "SEQ_WRITE_REQUIRED",
214 "SEQ_WRITE_PREFERRED",
215};
216
217static const char *condition_str[] = {
a7f74206
DLM
218 "nw", /* Not write pointer */
219 "em", /* Empty */
b1484d8d
DLM
220 "oi", /* Implicitly opened */
221 "oe", /* Explicitly opened */
a7f74206 222 "cl", /* Closed */
b1484d8d 223 "x5", "x6", "x7", "x8", "x9", "xA", "xB", "xC", /* xN: reserved */
a7f74206
DLM
224 "ro", /* Read only */
225 "fu", /* Full */
226 "of" /* Offline */
1ad8ef91
KZ
227};
228
229static int blkzone_report(struct blkzone_control *ctl)
230{
049859a6
HH
231 bool only_capacity_sum = !strcmp(ctl->command->name, "capacity");
232 uint64_t capacity_sum = 0;
1ad8ef91 233 struct blk_zone_report *zi;
6e1958d8 234 unsigned long zonesize;
f1b8b84d 235 uint32_t i, nr_zones;
1ad8ef91
KZ
236 int fd;
237
238 fd = init_device(ctl, O_RDONLY);
239
8a7f4b5b
MS
240 if (ctl->offset >= ctl->total_sectors)
241 errx(EXIT_FAILURE,
242 _("%s: offset is greater than or equal to device size"), ctl->devname);
f1b8b84d
DLM
243
244 zonesize = blkdev_chunk_sectors(ctl->devname);
245 if (!zonesize)
246 errx(EXIT_FAILURE, _("%s: unable to determine zone size"), ctl->devname);
247
248 if (ctl->count)
249 nr_zones = ctl->count;
250 else if (ctl->length)
251 nr_zones = (ctl->length + zonesize - 1) / zonesize;
252 else
253 nr_zones = 1 + (ctl->total_sectors - ctl->offset) / zonesize;
1ad8ef91 254
6e1958d8
DLM
255 zi = xmalloc(sizeof(struct blk_zone_report) +
256 (DEF_REPORT_LEN * sizeof(struct blk_zone)));
1ad8ef91 257
f1b8b84d 258 while (nr_zones && ctl->offset < ctl->total_sectors) {
1ad8ef91 259
f1b8b84d 260 zi->nr_zones = min(nr_zones, DEF_REPORT_LEN);
6e1958d8 261 zi->sector = ctl->offset;
1ad8ef91 262
6e1958d8
DLM
263 if (ioctl(fd, BLKREPORTZONE, zi) == -1)
264 err(EXIT_FAILURE, _("%s: BLKREPORTZONE ioctl failed"), ctl->devname);
1ad8ef91 265
6e1958d8 266 if (ctl->verbose)
c8df4b17 267 printf(_("Found %d zones from 0x%"PRIx64"\n"),
6e1958d8 268 zi->nr_zones, ctl->offset);
1ad8ef91 269
106bd306 270 if (!zi->nr_zones)
1ad8ef91 271 break;
6e1958d8
DLM
272
273 for (i = 0; i < zi->nr_zones; i++) {
754c030f
TW
274 const struct blk_zone entry = zi->zones[i];
275 unsigned int type = entry.type;
276 uint64_t start = entry.start;
277 uint64_t wp = entry.wp;
278 uint8_t cond = entry.cond;
279 uint64_t len = entry.len;
e5ad3a6a 280 uint64_t cap;
6e1958d8
DLM
281
282 if (!len) {
f1b8b84d 283 nr_zones = 0;
6e1958d8
DLM
284 break;
285 }
286
e5ad3a6a 287 if (has_zone_capacity(zi))
754c030f 288 cap = zone_capacity(&entry);
e5ad3a6a 289 else
754c030f 290 cap = entry.len;
e5ad3a6a 291
049859a6
HH
292 if (only_capacity_sum) {
293 capacity_sum += cap;
efea8677 294 } else if (has_zone_capacity(zi)) {
049859a6
HH
295 printf(_(" start: 0x%09"PRIx64", len 0x%06"PRIx64
296 ", cap 0x%06"PRIx64", wptr 0x%06"PRIx64
297 " reset:%u non-seq:%u, zcond:%2u(%s) [type: %u(%s)]\n"),
298 start, len, cap, (type == 0x1) ? 0 : wp - start,
754c030f 299 entry.reset, entry.non_seq,
049859a6
HH
300 cond, condition_str[cond & (ARRAY_SIZE(condition_str) - 1)],
301 type, type_text[type]);
efea8677
AH
302 } else {
303 printf(_(" start: 0x%09"PRIx64", len 0x%06"PRIx64
304 ", wptr 0x%06"PRIx64
305 " reset:%u non-seq:%u, zcond:%2u(%s) [type: %u(%s)]\n"),
306 start, len, (type == 0x1) ? 0 : wp - start,
754c030f 307 entry.reset, entry.non_seq,
efea8677
AH
308 cond, condition_str[cond & (ARRAY_SIZE(condition_str) - 1)],
309 type, type_text[type]);
049859a6 310 }
6e1958d8 311
f1b8b84d 312 nr_zones--;
6e1958d8 313 ctl->offset = start + len;
6e1958d8 314 }
1ad8ef91 315
1ad8ef91
KZ
316 }
317
049859a6
HH
318 if (only_capacity_sum)
319 printf(_("0x%09"PRIx64"\n"), capacity_sum);
320
1ad8ef91
KZ
321 free(zi);
322 close(fd);
323
324 return 0;
325}
326
327/*
0cecae9c 328 * blkzone reset, open, close, and finish.
1ad8ef91 329 */
0cecae9c 330static int blkzone_action(struct blkzone_control *ctl)
1ad8ef91
KZ
331{
332 struct blk_zone_range za = { .sector = 0 };
333 unsigned long zonesize;
334 uint64_t zlen;
335 int fd;
336
337 zonesize = blkdev_chunk_sectors(ctl->devname);
338 if (!zonesize)
339 errx(EXIT_FAILURE, _("%s: unable to determine zone size"), ctl->devname);
340
6e103c76 341 fd = init_device(ctl, O_WRONLY | (ctl->force ? 0 : O_EXCL));
1ad8ef91 342
ba676a27 343 if (ctl->offset % zonesize )
f1b8b84d 344 errx(EXIT_FAILURE, _("%s: offset %" PRIu64 " is not aligned "
c8df4b17 345 "to zone size %lu"),
1ad8ef91
KZ
346 ctl->devname, ctl->offset, zonesize);
347
348 if (ctl->offset > ctl->total_sectors)
349 errx(EXIT_FAILURE, _("%s: offset is greater than device size"), ctl->devname);
350
f1b8b84d
DLM
351 if (ctl->count)
352 zlen = ctl->count * zonesize;
353 else if (ctl->length)
354 zlen = ctl->length;
72a73102 355 else
f1b8b84d 356 zlen = ctl->total_sectors;
1ad8ef91 357 if (ctl->offset + zlen > ctl->total_sectors)
72a73102 358 zlen = ctl->total_sectors - ctl->offset;
1ad8ef91 359
f1b8b84d 360 if (ctl->length &&
ba676a27 361 (zlen % zonesize) &&
f1b8b84d
DLM
362 ctl->offset + zlen != ctl->total_sectors)
363 errx(EXIT_FAILURE, _("%s: number of sectors %" PRIu64 " is not aligned "
c8df4b17 364 "to zone size %lu"),
f1b8b84d
DLM
365 ctl->devname, ctl->length, zonesize);
366
1ad8ef91
KZ
367 za.sector = ctl->offset;
368 za.nr_sectors = zlen;
369
46cf6625 370 if (ioctl(fd, ctl->command->ioctl_cmd, &za) == -1)
0cecae9c 371 err(EXIT_FAILURE, _("%s: %s ioctl failed"),
46cf6625 372 ctl->devname, ctl->command->ioctl_name);
1ad8ef91 373 else if (ctl->verbose)
311e33af 374 printf(_("%s: successful %s of zones in range from %" PRIu64 ", to %" PRIu64),
1ad8ef91 375 ctl->devname,
0cecae9c 376 ctl->command->name,
1ad8ef91 377 ctl->offset,
f1b8b84d 378 ctl->offset + zlen);
1ad8ef91
KZ
379 close(fd);
380 return 0;
381}
382
86be6a32 383static void __attribute__((__noreturn__)) usage(void)
1ad8ef91 384{
86be6a32 385 FILE *out = stdout;
1ad8ef91
KZ
386 size_t i;
387
388 fputs(USAGE_HEADER, out);
389 fprintf(out, _(" %s <command> [options] <device>\n"), program_invocation_short_name);
390
391 fputs(USAGE_SEPARATOR, out);
392 fputs(_("Run zone command on the given block device.\n"), out);
393
6e2d5a44 394 fputs(USAGE_COMMANDS, out);
1ad8ef91
KZ
395 for (i = 0; i < ARRAY_SIZE(commands); i++)
396 fprintf(out, " %-11s %s\n", commands[i].name, _(commands[i].help));
397
398 fputs(USAGE_OPTIONS, out);
399 fputs(_(" -o, --offset <sector> start sector of zone to act (in 512-byte sectors)\n"), out);
c16970f7
KZ
400 fputs(_(" -l, --length <sectors> maximum sectors to act (in 512-byte sectors)\n"), out);
401 fputs(_(" -c, --count <number> maximum number of zones\n"), out);
6e103c76 402 fputs(_(" -f, --force enforce on block devices used by the system\n"), out);
1ad8ef91
KZ
403 fputs(_(" -v, --verbose display more details\n"), out);
404 fputs(USAGE_SEPARATOR, out);
bad4c729 405 fprintf(out, USAGE_HELP_OPTIONS(24));
1ad8ef91 406
f1970cc5 407 fputs(USAGE_ARGUMENTS, out);
bad4c729 408 fprintf(out, USAGE_ARG_SIZE(_("<sector> and <sectors>")));
f1970cc5 409
bad4c729 410 fprintf(out, USAGE_MAN_TAIL("blkzone(8)"));
86be6a32 411 exit(EXIT_SUCCESS);
1ad8ef91
KZ
412}
413
414int main(int argc, char **argv)
415{
416 int c;
f1b8b84d 417 struct blkzone_control ctl = {
43d2ce3b 418 .devname = NULL
f1b8b84d 419 };
1ad8ef91
KZ
420
421 static const struct option longopts[] = {
422 { "help", no_argument, NULL, 'h' },
c16970f7 423 { "count", required_argument, NULL, 'c' }, /* max #of zones to operate on */
f1b8b84d 424 { "length", required_argument, NULL, 'l' }, /* max of sectors to operate on */
1ad8ef91 425 { "offset", required_argument, NULL, 'o' }, /* starting LBA */
6e103c76 426 { "force", no_argument, NULL, 'f' },
1ad8ef91
KZ
427 { "verbose", no_argument, NULL, 'v' },
428 { "version", no_argument, NULL, 'V' },
429 { NULL, 0, NULL, 0 }
430 };
c16970f7
KZ
431 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
432 { 'c', 'l' },
433 { 0 }
434 };
435 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
436
1ad8ef91
KZ
437
438 setlocale(LC_ALL, "");
439 bindtextdomain(PACKAGE, LOCALEDIR);
440 textdomain(PACKAGE);
2c308875 441 close_stdout_atexit();
1ad8ef91
KZ
442
443 if (argc >= 2 && *argv[1] != '-') {
444 ctl.command = name_to_command(argv[1]);
445 if (!ctl.command)
446 errx(EXIT_FAILURE, _("%s is not valid command name"), argv[1]);
447 argv++;
448 argc--;
449 }
450
6e103c76 451 while ((c = getopt_long(argc, argv, "hc:l:o:fvV", longopts, NULL)) != -1) {
c16970f7
KZ
452
453 err_exclusive_options(c, longopts, excl, excl_st);
454
1ad8ef91 455 switch (c) {
f1b8b84d
DLM
456 case 'c':
457 ctl.count = strtou32_or_err(optarg,
458 _("failed to parse number of zones"));
459 break;
1ad8ef91
KZ
460 case 'l':
461 ctl.length = strtosize_or_err(optarg,
f1b8b84d 462 _("failed to parse number of sectors"));
1ad8ef91
KZ
463 break;
464 case 'o':
465 ctl.offset = strtosize_or_err(optarg,
466 _("failed to parse zone offset"));
467 break;
6e103c76
SK
468 case 'f':
469 ctl.force = 1;
470 break;
1ad8ef91
KZ
471 case 'v':
472 ctl.verbose = 1;
473 break;
2c308875
KZ
474
475 case 'h':
476 usage();
1ad8ef91 477 case 'V':
2c308875 478 print_version(EXIT_SUCCESS);
1ad8ef91
KZ
479 default:
480 errtryhelp(EXIT_FAILURE);
481 }
482 }
483
484 if (!ctl.command)
485 errx(EXIT_FAILURE, _("no command specified"));
486
487 if (optind == argc)
488 errx(EXIT_FAILURE, _("no device specified"));
489 ctl.devname = argv[optind++];
490
491 if (optind != argc)
492 errx(EXIT_FAILURE,_("unexpected number of arguments"));
493
494 if (ctl.command->handler(&ctl) < 0)
495 return EXIT_FAILURE;
496
497 return EXIT_SUCCESS;
498
499}