]> git.ipfire.org Git - thirdparty/util-linux.git/blame - sys-utils/blkzone.c
blkzone: remove never read value
[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>
27#include <fcntl.h>
28#include <limits.h>
29#include <getopt.h>
30#include <time.h>
31
32#include <sys/ioctl.h>
33#include <sys/stat.h>
34#include <sys/time.h>
35#include <linux/fs.h>
36#include <linux/blkzoned.h>
37
38#include "nls.h"
39#include "strutils.h"
40#include "xalloc.h"
41#include "c.h"
42#include "closestream.h"
43#include "blkdev.h"
44#include "sysfs.h"
c16970f7 45#include "optutils.h"
1ad8ef91
KZ
46
47struct blkzone_control;
48
49static int blkzone_report(struct blkzone_control *ctl);
50static int blkzone_reset(struct blkzone_control *ctl);
51
52struct blkzone_command {
53 const char *name;
54 int (*handler)(struct blkzone_control *);
55 const char *help;
56};
57
58struct blkzone_control {
59 const char *devname;
60 const struct blkzone_command *command;
61
62 uint64_t total_sectors;
63 int secsize;
64
65 uint64_t offset;
66 uint64_t length;
f1b8b84d 67 uint32_t count;
1ad8ef91
KZ
68
69 unsigned int verbose : 1;
70};
71
72static const struct blkzone_command commands[] = {
73 { "report", blkzone_report, N_("Report zone information about the given device") },
74 { "reset", blkzone_reset, N_("Reset a range of zones.") }
75};
76
77static const struct blkzone_command *name_to_command(const char *name)
78{
79 size_t i;
80
81 for (i = 0; i < ARRAY_SIZE(commands); i++) {
82 if (strcmp(commands[i].name, name) == 0)
83 return &commands[i];
84 }
85
86 return NULL;
87}
88
89static int init_device(struct blkzone_control *ctl, int mode)
90{
91 struct stat sb;
92 int fd;
93
94 fd = open(ctl->devname, mode);
95 if (fd < 0)
96 err(EXIT_FAILURE, _("cannot open %s"), ctl->devname);
97
98 if (fstat(fd, &sb) == -1)
99 err(EXIT_FAILURE, _("stat of %s failed"), ctl->devname);
100 if (!S_ISBLK(sb.st_mode))
101 errx(EXIT_FAILURE, _("%s: not a block device"), ctl->devname);
102
103 if (blkdev_get_sectors(fd, (unsigned long long *) &ctl->total_sectors))
104 err(EXIT_FAILURE, _("%s: blkdev_get_sectors ioctl failed"), ctl->devname);
105
106 if (blkdev_get_sector_size(fd, &ctl->secsize))
107 err(EXIT_FAILURE, _("%s: BLKSSZGET ioctl failed"), ctl->devname);
108
109 return fd;
110}
111
6e1958d8
DLM
112/*
113 * Get the device zone size indicated by chunk sectors).
114 */
115static unsigned long blkdev_chunk_sectors(const char *dname)
116{
4448b3ab
KZ
117 struct path_cxt *pc = NULL;
118 dev_t devno = sysfs_devname_to_devno(dname);
ddf287b4 119 dev_t disk;
4448b3ab 120 uint64_t sz = 0;
6e1958d8
DLM
121 int rc;
122
123 /*
124 * Mapping /dev/sdXn -> /sys/block/sdX to read the chunk_size entry.
125 * This method masks off the partition specified by the minor device
126 * component.
127 */
4448b3ab
KZ
128 pc = ul_new_sysfs_path(devno, NULL, NULL);
129 if (!pc)
ddf287b4
KZ
130 return 0;
131
4448b3ab
KZ
132 rc = sysfs_blkdev_get_wholedisk(pc, NULL, 0, &disk);
133 if (rc != 0)
134 goto done;
6e1958d8 135
4448b3ab
KZ
136 /* if @pc is not while-disk device, switch to disk */
137 if (devno != disk) {
138 rc = sysfs_blkdev_init_path(pc, disk, NULL);
139 if (rc != 0)
140 goto done;
141 }
6e1958d8 142
4448b3ab
KZ
143 rc = ul_path_read_u64(pc, &sz, "queue/chunk_sectors");
144done:
145 ul_unref_path(pc);
6e1958d8
DLM
146 return rc == 0 ? sz : 0;
147}
148
1ad8ef91
KZ
149/*
150 * blkzone report
151 */
f1b8b84d 152#define DEF_REPORT_LEN (1U << 12) /* 4k zones per report (256k kzalloc) */
1ad8ef91
KZ
153
154static const char *type_text[] = {
155 "RESERVED",
156 "CONVENTIONAL",
157 "SEQ_WRITE_REQUIRED",
158 "SEQ_WRITE_PREFERRED",
159};
160
161static const char *condition_str[] = {
a7f74206
DLM
162 "nw", /* Not write pointer */
163 "em", /* Empty */
b1484d8d
DLM
164 "oi", /* Implicitly opened */
165 "oe", /* Explicitly opened */
a7f74206 166 "cl", /* Closed */
b1484d8d 167 "x5", "x6", "x7", "x8", "x9", "xA", "xB", "xC", /* xN: reserved */
a7f74206
DLM
168 "ro", /* Read only */
169 "fu", /* Full */
170 "of" /* Offline */
1ad8ef91
KZ
171};
172
173static int blkzone_report(struct blkzone_control *ctl)
174{
175 struct blk_zone_report *zi;
6e1958d8 176 unsigned long zonesize;
f1b8b84d 177 uint32_t i, nr_zones;
1ad8ef91
KZ
178 int fd;
179
180 fd = init_device(ctl, O_RDONLY);
181
8a7f4b5b
MS
182 if (ctl->offset >= ctl->total_sectors)
183 errx(EXIT_FAILURE,
184 _("%s: offset is greater than or equal to device size"), ctl->devname);
f1b8b84d
DLM
185
186 zonesize = blkdev_chunk_sectors(ctl->devname);
187 if (!zonesize)
188 errx(EXIT_FAILURE, _("%s: unable to determine zone size"), ctl->devname);
189
190 if (ctl->count)
191 nr_zones = ctl->count;
192 else if (ctl->length)
193 nr_zones = (ctl->length + zonesize - 1) / zonesize;
194 else
195 nr_zones = 1 + (ctl->total_sectors - ctl->offset) / zonesize;
1ad8ef91 196
6e1958d8
DLM
197 zi = xmalloc(sizeof(struct blk_zone_report) +
198 (DEF_REPORT_LEN * sizeof(struct blk_zone)));
1ad8ef91 199
f1b8b84d 200 while (nr_zones && ctl->offset < ctl->total_sectors) {
1ad8ef91 201
f1b8b84d 202 zi->nr_zones = min(nr_zones, DEF_REPORT_LEN);
6e1958d8 203 zi->sector = ctl->offset;
1ad8ef91 204
6e1958d8
DLM
205 if (ioctl(fd, BLKREPORTZONE, zi) == -1)
206 err(EXIT_FAILURE, _("%s: BLKREPORTZONE ioctl failed"), ctl->devname);
1ad8ef91 207
6e1958d8 208 if (ctl->verbose)
c8df4b17 209 printf(_("Found %d zones from 0x%"PRIx64"\n"),
6e1958d8 210 zi->nr_zones, ctl->offset);
1ad8ef91 211
106bd306 212 if (!zi->nr_zones)
1ad8ef91 213 break;
6e1958d8
DLM
214
215 for (i = 0; i < zi->nr_zones; i++) {
216 const struct blk_zone *entry = &zi->zones[i];
217 unsigned int type = entry->type;
218 uint64_t start = entry->start;
219 uint64_t wp = entry->wp;
220 uint8_t cond = entry->cond;
221 uint64_t len = entry->len;
222
223 if (!len) {
f1b8b84d 224 nr_zones = 0;
6e1958d8
DLM
225 break;
226 }
227
c8df4b17 228 printf(_(" start: 0x%09"PRIx64", len 0x%06"PRIx64", wptr 0x%06"PRIx64
6e1958d8 229 " reset:%u non-seq:%u, zcond:%2u(%s) [type: %u(%s)]\n"),
a7f74206 230 start, len, (type == 0x1) ? 0 : wp - start,
6e1958d8 231 entry->reset, entry->non_seq,
b1484d8d 232 cond, condition_str[cond & (ARRAY_SIZE(condition_str) - 1)],
6e1958d8
DLM
233 type, type_text[type]);
234
f1b8b84d 235 nr_zones--;
6e1958d8
DLM
236 ctl->offset = start + len;
237
238 }
1ad8ef91 239
1ad8ef91
KZ
240 }
241
242 free(zi);
243 close(fd);
244
245 return 0;
246}
247
248/*
249 * blkzone reset
250 */
1ad8ef91
KZ
251static int blkzone_reset(struct blkzone_control *ctl)
252{
253 struct blk_zone_range za = { .sector = 0 };
254 unsigned long zonesize;
255 uint64_t zlen;
256 int fd;
257
258 zonesize = blkdev_chunk_sectors(ctl->devname);
259 if (!zonesize)
260 errx(EXIT_FAILURE, _("%s: unable to determine zone size"), ctl->devname);
261
262 fd = init_device(ctl, O_WRONLY);
263
264 if (ctl->offset & (zonesize - 1))
f1b8b84d 265 errx(EXIT_FAILURE, _("%s: offset %" PRIu64 " is not aligned "
c8df4b17 266 "to zone size %lu"),
1ad8ef91
KZ
267 ctl->devname, ctl->offset, zonesize);
268
269 if (ctl->offset > ctl->total_sectors)
270 errx(EXIT_FAILURE, _("%s: offset is greater than device size"), ctl->devname);
271
f1b8b84d
DLM
272 if (ctl->count)
273 zlen = ctl->count * zonesize;
274 else if (ctl->length)
275 zlen = ctl->length;
72a73102 276 else
f1b8b84d 277 zlen = ctl->total_sectors;
1ad8ef91 278 if (ctl->offset + zlen > ctl->total_sectors)
72a73102 279 zlen = ctl->total_sectors - ctl->offset;
1ad8ef91 280
f1b8b84d
DLM
281 if (ctl->length &&
282 (zlen & (zonesize - 1)) &&
283 ctl->offset + zlen != ctl->total_sectors)
284 errx(EXIT_FAILURE, _("%s: number of sectors %" PRIu64 " is not aligned "
c8df4b17 285 "to zone size %lu"),
f1b8b84d
DLM
286 ctl->devname, ctl->length, zonesize);
287
1ad8ef91
KZ
288 za.sector = ctl->offset;
289 za.nr_sectors = zlen;
290
291 if (ioctl(fd, BLKRESETZONE, &za) == -1)
292 err(EXIT_FAILURE, _("%s: BLKRESETZONE ioctl failed"), ctl->devname);
1ad8ef91
KZ
293 else if (ctl->verbose)
294 printf(_("%s: successfully reset in range from %" PRIu64 ", to %" PRIu64),
295 ctl->devname,
296 ctl->offset,
f1b8b84d 297 ctl->offset + zlen);
1ad8ef91
KZ
298 close(fd);
299 return 0;
300}
301
86be6a32 302static void __attribute__((__noreturn__)) usage(void)
1ad8ef91 303{
86be6a32 304 FILE *out = stdout;
1ad8ef91
KZ
305 size_t i;
306
307 fputs(USAGE_HEADER, out);
308 fprintf(out, _(" %s <command> [options] <device>\n"), program_invocation_short_name);
309
310 fputs(USAGE_SEPARATOR, out);
311 fputs(_("Run zone command on the given block device.\n"), out);
312
6e2d5a44 313 fputs(USAGE_COMMANDS, out);
1ad8ef91
KZ
314 for (i = 0; i < ARRAY_SIZE(commands); i++)
315 fprintf(out, " %-11s %s\n", commands[i].name, _(commands[i].help));
316
317 fputs(USAGE_OPTIONS, out);
318 fputs(_(" -o, --offset <sector> start sector of zone to act (in 512-byte sectors)\n"), out);
c16970f7
KZ
319 fputs(_(" -l, --length <sectors> maximum sectors to act (in 512-byte sectors)\n"), out);
320 fputs(_(" -c, --count <number> maximum number of zones\n"), out);
1ad8ef91
KZ
321 fputs(_(" -v, --verbose display more details\n"), out);
322 fputs(USAGE_SEPARATOR, out);
f45f3ec3 323 printf(USAGE_HELP_OPTIONS(24));
1ad8ef91 324
f45f3ec3 325 printf(USAGE_MAN_TAIL("blkzone(8)"));
86be6a32 326 exit(EXIT_SUCCESS);
1ad8ef91
KZ
327}
328
329int main(int argc, char **argv)
330{
331 int c;
f1b8b84d
DLM
332 struct blkzone_control ctl = {
333 .devname = NULL,
334 .offset = 0,
335 .count = 0,
336 .length = 0
337 };
1ad8ef91
KZ
338
339 static const struct option longopts[] = {
340 { "help", no_argument, NULL, 'h' },
c16970f7 341 { "count", required_argument, NULL, 'c' }, /* max #of zones to operate on */
f1b8b84d 342 { "length", required_argument, NULL, 'l' }, /* max of sectors to operate on */
1ad8ef91
KZ
343 { "offset", required_argument, NULL, 'o' }, /* starting LBA */
344 { "verbose", no_argument, NULL, 'v' },
345 { "version", no_argument, NULL, 'V' },
346 { NULL, 0, NULL, 0 }
347 };
c16970f7
KZ
348 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
349 { 'c', 'l' },
350 { 0 }
351 };
352 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
353
1ad8ef91
KZ
354
355 setlocale(LC_ALL, "");
356 bindtextdomain(PACKAGE, LOCALEDIR);
357 textdomain(PACKAGE);
2c308875 358 close_stdout_atexit();
1ad8ef91
KZ
359
360 if (argc >= 2 && *argv[1] != '-') {
361 ctl.command = name_to_command(argv[1]);
362 if (!ctl.command)
363 errx(EXIT_FAILURE, _("%s is not valid command name"), argv[1]);
364 argv++;
365 argc--;
366 }
367
f1b8b84d 368 while ((c = getopt_long(argc, argv, "hc:l:o:vV", longopts, NULL)) != -1) {
c16970f7
KZ
369
370 err_exclusive_options(c, longopts, excl, excl_st);
371
1ad8ef91 372 switch (c) {
f1b8b84d
DLM
373 case 'c':
374 ctl.count = strtou32_or_err(optarg,
375 _("failed to parse number of zones"));
376 break;
1ad8ef91
KZ
377 case 'l':
378 ctl.length = strtosize_or_err(optarg,
f1b8b84d 379 _("failed to parse number of sectors"));
1ad8ef91
KZ
380 break;
381 case 'o':
382 ctl.offset = strtosize_or_err(optarg,
383 _("failed to parse zone offset"));
384 break;
385 case 'v':
386 ctl.verbose = 1;
387 break;
2c308875
KZ
388
389 case 'h':
390 usage();
1ad8ef91 391 case 'V':
2c308875 392 print_version(EXIT_SUCCESS);
1ad8ef91
KZ
393 default:
394 errtryhelp(EXIT_FAILURE);
395 }
396 }
397
398 if (!ctl.command)
399 errx(EXIT_FAILURE, _("no command specified"));
400
401 if (optind == argc)
402 errx(EXIT_FAILURE, _("no device specified"));
403 ctl.devname = argv[optind++];
404
405 if (optind != argc)
406 errx(EXIT_FAILURE,_("unexpected number of arguments"));
407
408 if (ctl.command->handler(&ctl) < 0)
409 return EXIT_FAILURE;
410
411 return EXIT_SUCCESS;
412
413}