]> git.ipfire.org Git - thirdparty/util-linux.git/blame - sys-utils/blkzone.c
misc: consolidate macro style USAGE_HELP_OPTIONS
[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{
117 struct sysfs_cxt cxt = UL_SYSFSCXT_EMPTY;
118 dev_t devno = sysfs_devname_to_devno(dname, NULL);
119 int major_no = major(devno);
120 int block_no = minor(devno) & ~0x0f;
121 uint64_t sz;
122 int rc;
123
124 /*
125 * Mapping /dev/sdXn -> /sys/block/sdX to read the chunk_size entry.
126 * This method masks off the partition specified by the minor device
127 * component.
128 */
129 devno = makedev(major_no, block_no);
130 if (sysfs_init(&cxt, devno, NULL))
131 return 0;
132
133 rc = sysfs_read_u64(&cxt, "queue/chunk_sectors", &sz);
134
135 sysfs_deinit(&cxt);
136 return rc == 0 ? sz : 0;
137}
138
1ad8ef91
KZ
139/*
140 * blkzone report
141 */
f1b8b84d 142#define DEF_REPORT_LEN (1U << 12) /* 4k zones per report (256k kzalloc) */
1ad8ef91
KZ
143
144static const char *type_text[] = {
145 "RESERVED",
146 "CONVENTIONAL",
147 "SEQ_WRITE_REQUIRED",
148 "SEQ_WRITE_PREFERRED",
149};
150
151static const char *condition_str[] = {
a7f74206
DLM
152 "nw", /* Not write pointer */
153 "em", /* Empty */
b1484d8d
DLM
154 "oi", /* Implicitly opened */
155 "oe", /* Explicitly opened */
a7f74206 156 "cl", /* Closed */
b1484d8d 157 "x5", "x6", "x7", "x8", "x9", "xA", "xB", "xC", /* xN: reserved */
a7f74206
DLM
158 "ro", /* Read only */
159 "fu", /* Full */
160 "of" /* Offline */
1ad8ef91
KZ
161};
162
163static int blkzone_report(struct blkzone_control *ctl)
164{
165 struct blk_zone_report *zi;
6e1958d8 166 unsigned long zonesize;
f1b8b84d 167 uint32_t i, nr_zones;
1ad8ef91
KZ
168 int fd;
169
170 fd = init_device(ctl, O_RDONLY);
171
172 if (ctl->offset > ctl->total_sectors)
173 errx(EXIT_FAILURE, _("%s: offset is greater than device size"), ctl->devname);
f1b8b84d
DLM
174
175 zonesize = blkdev_chunk_sectors(ctl->devname);
176 if (!zonesize)
177 errx(EXIT_FAILURE, _("%s: unable to determine zone size"), ctl->devname);
178
179 if (ctl->count)
180 nr_zones = ctl->count;
181 else if (ctl->length)
182 nr_zones = (ctl->length + zonesize - 1) / zonesize;
183 else
184 nr_zones = 1 + (ctl->total_sectors - ctl->offset) / zonesize;
1ad8ef91 185
6e1958d8
DLM
186 zi = xmalloc(sizeof(struct blk_zone_report) +
187 (DEF_REPORT_LEN * sizeof(struct blk_zone)));
1ad8ef91 188
f1b8b84d 189 while (nr_zones && ctl->offset < ctl->total_sectors) {
1ad8ef91 190
f1b8b84d 191 zi->nr_zones = min(nr_zones, DEF_REPORT_LEN);
6e1958d8 192 zi->sector = ctl->offset;
1ad8ef91 193
6e1958d8
DLM
194 if (ioctl(fd, BLKREPORTZONE, zi) == -1)
195 err(EXIT_FAILURE, _("%s: BLKREPORTZONE ioctl failed"), ctl->devname);
1ad8ef91 196
6e1958d8 197 if (ctl->verbose)
c8df4b17 198 printf(_("Found %d zones from 0x%"PRIx64"\n"),
6e1958d8 199 zi->nr_zones, ctl->offset);
1ad8ef91 200
6e1958d8 201 if (!zi->nr_zones) {
f1b8b84d 202 nr_zones = 0;
1ad8ef91 203 break;
6e1958d8
DLM
204 }
205
206 for (i = 0; i < zi->nr_zones; i++) {
207 const struct blk_zone *entry = &zi->zones[i];
208 unsigned int type = entry->type;
209 uint64_t start = entry->start;
210 uint64_t wp = entry->wp;
211 uint8_t cond = entry->cond;
212 uint64_t len = entry->len;
213
214 if (!len) {
f1b8b84d 215 nr_zones = 0;
6e1958d8
DLM
216 break;
217 }
218
c8df4b17 219 printf(_(" start: 0x%09"PRIx64", len 0x%06"PRIx64", wptr 0x%06"PRIx64
6e1958d8 220 " reset:%u non-seq:%u, zcond:%2u(%s) [type: %u(%s)]\n"),
a7f74206 221 start, len, (type == 0x1) ? 0 : wp - start,
6e1958d8 222 entry->reset, entry->non_seq,
b1484d8d 223 cond, condition_str[cond & (ARRAY_SIZE(condition_str) - 1)],
6e1958d8
DLM
224 type, type_text[type]);
225
f1b8b84d 226 nr_zones--;
6e1958d8
DLM
227 ctl->offset = start + len;
228
229 }
1ad8ef91 230
1ad8ef91
KZ
231 }
232
233 free(zi);
234 close(fd);
235
236 return 0;
237}
238
239/*
240 * blkzone reset
241 */
1ad8ef91
KZ
242static int blkzone_reset(struct blkzone_control *ctl)
243{
244 struct blk_zone_range za = { .sector = 0 };
245 unsigned long zonesize;
246 uint64_t zlen;
247 int fd;
248
249 zonesize = blkdev_chunk_sectors(ctl->devname);
250 if (!zonesize)
251 errx(EXIT_FAILURE, _("%s: unable to determine zone size"), ctl->devname);
252
253 fd = init_device(ctl, O_WRONLY);
254
255 if (ctl->offset & (zonesize - 1))
f1b8b84d 256 errx(EXIT_FAILURE, _("%s: offset %" PRIu64 " is not aligned "
c8df4b17 257 "to zone size %lu"),
1ad8ef91
KZ
258 ctl->devname, ctl->offset, zonesize);
259
260 if (ctl->offset > ctl->total_sectors)
261 errx(EXIT_FAILURE, _("%s: offset is greater than device size"), ctl->devname);
262
f1b8b84d
DLM
263 if (ctl->count)
264 zlen = ctl->count * zonesize;
265 else if (ctl->length)
266 zlen = ctl->length;
72a73102 267 else
f1b8b84d 268 zlen = ctl->total_sectors;
1ad8ef91 269 if (ctl->offset + zlen > ctl->total_sectors)
72a73102 270 zlen = ctl->total_sectors - ctl->offset;
1ad8ef91 271
f1b8b84d
DLM
272 if (ctl->length &&
273 (zlen & (zonesize - 1)) &&
274 ctl->offset + zlen != ctl->total_sectors)
275 errx(EXIT_FAILURE, _("%s: number of sectors %" PRIu64 " is not aligned "
c8df4b17 276 "to zone size %lu"),
f1b8b84d
DLM
277 ctl->devname, ctl->length, zonesize);
278
1ad8ef91
KZ
279 za.sector = ctl->offset;
280 za.nr_sectors = zlen;
281
282 if (ioctl(fd, BLKRESETZONE, &za) == -1)
283 err(EXIT_FAILURE, _("%s: BLKRESETZONE ioctl failed"), ctl->devname);
1ad8ef91
KZ
284 else if (ctl->verbose)
285 printf(_("%s: successfully reset in range from %" PRIu64 ", to %" PRIu64),
286 ctl->devname,
287 ctl->offset,
f1b8b84d 288 ctl->offset + zlen);
1ad8ef91
KZ
289 close(fd);
290 return 0;
291}
292
86be6a32 293static void __attribute__((__noreturn__)) usage(void)
1ad8ef91 294{
86be6a32 295 FILE *out = stdout;
1ad8ef91
KZ
296 size_t i;
297
298 fputs(USAGE_HEADER, out);
299 fprintf(out, _(" %s <command> [options] <device>\n"), program_invocation_short_name);
300
301 fputs(USAGE_SEPARATOR, out);
302 fputs(_("Run zone command on the given block device.\n"), out);
303
6e2d5a44 304 fputs(USAGE_COMMANDS, out);
1ad8ef91
KZ
305 for (i = 0; i < ARRAY_SIZE(commands); i++)
306 fprintf(out, " %-11s %s\n", commands[i].name, _(commands[i].help));
307
308 fputs(USAGE_OPTIONS, out);
309 fputs(_(" -o, --offset <sector> start sector of zone to act (in 512-byte sectors)\n"), out);
c16970f7
KZ
310 fputs(_(" -l, --length <sectors> maximum sectors to act (in 512-byte sectors)\n"), out);
311 fputs(_(" -c, --count <number> maximum number of zones\n"), out);
1ad8ef91
KZ
312 fputs(_(" -v, --verbose display more details\n"), out);
313 fputs(USAGE_SEPARATOR, out);
f45f3ec3 314 printf(USAGE_HELP_OPTIONS(24));
1ad8ef91 315
f45f3ec3 316 printf(USAGE_MAN_TAIL("blkzone(8)"));
86be6a32 317 exit(EXIT_SUCCESS);
1ad8ef91
KZ
318}
319
320int main(int argc, char **argv)
321{
322 int c;
f1b8b84d
DLM
323 struct blkzone_control ctl = {
324 .devname = NULL,
325 .offset = 0,
326 .count = 0,
327 .length = 0
328 };
1ad8ef91
KZ
329
330 static const struct option longopts[] = {
331 { "help", no_argument, NULL, 'h' },
c16970f7 332 { "count", required_argument, NULL, 'c' }, /* max #of zones to operate on */
f1b8b84d 333 { "length", required_argument, NULL, 'l' }, /* max of sectors to operate on */
1ad8ef91
KZ
334 { "offset", required_argument, NULL, 'o' }, /* starting LBA */
335 { "verbose", no_argument, NULL, 'v' },
336 { "version", no_argument, NULL, 'V' },
337 { NULL, 0, NULL, 0 }
338 };
c16970f7
KZ
339 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
340 { 'c', 'l' },
341 { 0 }
342 };
343 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
344
1ad8ef91
KZ
345
346 setlocale(LC_ALL, "");
347 bindtextdomain(PACKAGE, LOCALEDIR);
348 textdomain(PACKAGE);
349 atexit(close_stdout);
350
351 if (argc >= 2 && *argv[1] != '-') {
352 ctl.command = name_to_command(argv[1]);
353 if (!ctl.command)
354 errx(EXIT_FAILURE, _("%s is not valid command name"), argv[1]);
355 argv++;
356 argc--;
357 }
358
f1b8b84d 359 while ((c = getopt_long(argc, argv, "hc:l:o:vV", longopts, NULL)) != -1) {
c16970f7
KZ
360
361 err_exclusive_options(c, longopts, excl, excl_st);
362
1ad8ef91
KZ
363 switch (c) {
364 case 'h':
86be6a32 365 usage();
1ad8ef91 366 break;
f1b8b84d
DLM
367 case 'c':
368 ctl.count = strtou32_or_err(optarg,
369 _("failed to parse number of zones"));
370 break;
1ad8ef91
KZ
371 case 'l':
372 ctl.length = strtosize_or_err(optarg,
f1b8b84d 373 _("failed to parse number of sectors"));
1ad8ef91
KZ
374 break;
375 case 'o':
376 ctl.offset = strtosize_or_err(optarg,
377 _("failed to parse zone offset"));
378 break;
379 case 'v':
380 ctl.verbose = 1;
381 break;
382 case 'V':
383 printf(UTIL_LINUX_VERSION);
384 return EXIT_SUCCESS;
385 default:
386 errtryhelp(EXIT_FAILURE);
387 }
388 }
389
390 if (!ctl.command)
391 errx(EXIT_FAILURE, _("no command specified"));
392
393 if (optind == argc)
394 errx(EXIT_FAILURE, _("no device specified"));
395 ctl.devname = argv[optind++];
396
397 if (optind != argc)
398 errx(EXIT_FAILURE,_("unexpected number of arguments"));
399
400 if (ctl.command->handler(&ctl) < 0)
401 return EXIT_FAILURE;
402
403 return EXIT_SUCCESS;
404
405}