]> git.ipfire.org Git - thirdparty/util-linux.git/blame - sys-utils/blkzone.c
agetty: don't put the VC into canonical mode
[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
182 if (ctl->offset > ctl->total_sectors)
183 errx(EXIT_FAILURE, _("%s: offset is greater than device size"), ctl->devname);
f1b8b84d
DLM
184
185 zonesize = blkdev_chunk_sectors(ctl->devname);
186 if (!zonesize)
187 errx(EXIT_FAILURE, _("%s: unable to determine zone size"), ctl->devname);
188
189 if (ctl->count)
190 nr_zones = ctl->count;
191 else if (ctl->length)
192 nr_zones = (ctl->length + zonesize - 1) / zonesize;
193 else
194 nr_zones = 1 + (ctl->total_sectors - ctl->offset) / zonesize;
1ad8ef91 195
6e1958d8
DLM
196 zi = xmalloc(sizeof(struct blk_zone_report) +
197 (DEF_REPORT_LEN * sizeof(struct blk_zone)));
1ad8ef91 198
f1b8b84d 199 while (nr_zones && ctl->offset < ctl->total_sectors) {
1ad8ef91 200
f1b8b84d 201 zi->nr_zones = min(nr_zones, DEF_REPORT_LEN);
6e1958d8 202 zi->sector = ctl->offset;
1ad8ef91 203
6e1958d8
DLM
204 if (ioctl(fd, BLKREPORTZONE, zi) == -1)
205 err(EXIT_FAILURE, _("%s: BLKREPORTZONE ioctl failed"), ctl->devname);
1ad8ef91 206
6e1958d8 207 if (ctl->verbose)
c8df4b17 208 printf(_("Found %d zones from 0x%"PRIx64"\n"),
6e1958d8 209 zi->nr_zones, ctl->offset);
1ad8ef91 210
6e1958d8 211 if (!zi->nr_zones) {
f1b8b84d 212 nr_zones = 0;
1ad8ef91 213 break;
6e1958d8
DLM
214 }
215
216 for (i = 0; i < zi->nr_zones; i++) {
217 const struct blk_zone *entry = &zi->zones[i];
218 unsigned int type = entry->type;
219 uint64_t start = entry->start;
220 uint64_t wp = entry->wp;
221 uint8_t cond = entry->cond;
222 uint64_t len = entry->len;
223
224 if (!len) {
f1b8b84d 225 nr_zones = 0;
6e1958d8
DLM
226 break;
227 }
228
c8df4b17 229 printf(_(" start: 0x%09"PRIx64", len 0x%06"PRIx64", wptr 0x%06"PRIx64
6e1958d8 230 " reset:%u non-seq:%u, zcond:%2u(%s) [type: %u(%s)]\n"),
a7f74206 231 start, len, (type == 0x1) ? 0 : wp - start,
6e1958d8 232 entry->reset, entry->non_seq,
b1484d8d 233 cond, condition_str[cond & (ARRAY_SIZE(condition_str) - 1)],
6e1958d8
DLM
234 type, type_text[type]);
235
f1b8b84d 236 nr_zones--;
6e1958d8
DLM
237 ctl->offset = start + len;
238
239 }
1ad8ef91 240
1ad8ef91
KZ
241 }
242
243 free(zi);
244 close(fd);
245
246 return 0;
247}
248
249/*
250 * blkzone reset
251 */
1ad8ef91
KZ
252static int blkzone_reset(struct blkzone_control *ctl)
253{
254 struct blk_zone_range za = { .sector = 0 };
255 unsigned long zonesize;
256 uint64_t zlen;
257 int fd;
258
259 zonesize = blkdev_chunk_sectors(ctl->devname);
260 if (!zonesize)
261 errx(EXIT_FAILURE, _("%s: unable to determine zone size"), ctl->devname);
262
263 fd = init_device(ctl, O_WRONLY);
264
265 if (ctl->offset & (zonesize - 1))
f1b8b84d 266 errx(EXIT_FAILURE, _("%s: offset %" PRIu64 " is not aligned "
c8df4b17 267 "to zone size %lu"),
1ad8ef91
KZ
268 ctl->devname, ctl->offset, zonesize);
269
270 if (ctl->offset > ctl->total_sectors)
271 errx(EXIT_FAILURE, _("%s: offset is greater than device size"), ctl->devname);
272
f1b8b84d
DLM
273 if (ctl->count)
274 zlen = ctl->count * zonesize;
275 else if (ctl->length)
276 zlen = ctl->length;
72a73102 277 else
f1b8b84d 278 zlen = ctl->total_sectors;
1ad8ef91 279 if (ctl->offset + zlen > ctl->total_sectors)
72a73102 280 zlen = ctl->total_sectors - ctl->offset;
1ad8ef91 281
f1b8b84d
DLM
282 if (ctl->length &&
283 (zlen & (zonesize - 1)) &&
284 ctl->offset + zlen != ctl->total_sectors)
285 errx(EXIT_FAILURE, _("%s: number of sectors %" PRIu64 " is not aligned "
c8df4b17 286 "to zone size %lu"),
f1b8b84d
DLM
287 ctl->devname, ctl->length, zonesize);
288
1ad8ef91
KZ
289 za.sector = ctl->offset;
290 za.nr_sectors = zlen;
291
292 if (ioctl(fd, BLKRESETZONE, &za) == -1)
293 err(EXIT_FAILURE, _("%s: BLKRESETZONE ioctl failed"), ctl->devname);
1ad8ef91
KZ
294 else if (ctl->verbose)
295 printf(_("%s: successfully reset in range from %" PRIu64 ", to %" PRIu64),
296 ctl->devname,
297 ctl->offset,
f1b8b84d 298 ctl->offset + zlen);
1ad8ef91
KZ
299 close(fd);
300 return 0;
301}
302
86be6a32 303static void __attribute__((__noreturn__)) usage(void)
1ad8ef91 304{
86be6a32 305 FILE *out = stdout;
1ad8ef91
KZ
306 size_t i;
307
308 fputs(USAGE_HEADER, out);
309 fprintf(out, _(" %s <command> [options] <device>\n"), program_invocation_short_name);
310
311 fputs(USAGE_SEPARATOR, out);
312 fputs(_("Run zone command on the given block device.\n"), out);
313
6e2d5a44 314 fputs(USAGE_COMMANDS, out);
1ad8ef91
KZ
315 for (i = 0; i < ARRAY_SIZE(commands); i++)
316 fprintf(out, " %-11s %s\n", commands[i].name, _(commands[i].help));
317
318 fputs(USAGE_OPTIONS, out);
319 fputs(_(" -o, --offset <sector> start sector of zone to act (in 512-byte sectors)\n"), out);
c16970f7
KZ
320 fputs(_(" -l, --length <sectors> maximum sectors to act (in 512-byte sectors)\n"), out);
321 fputs(_(" -c, --count <number> maximum number of zones\n"), out);
1ad8ef91
KZ
322 fputs(_(" -v, --verbose display more details\n"), out);
323 fputs(USAGE_SEPARATOR, out);
f45f3ec3 324 printf(USAGE_HELP_OPTIONS(24));
1ad8ef91 325
f45f3ec3 326 printf(USAGE_MAN_TAIL("blkzone(8)"));
86be6a32 327 exit(EXIT_SUCCESS);
1ad8ef91
KZ
328}
329
330int main(int argc, char **argv)
331{
332 int c;
f1b8b84d
DLM
333 struct blkzone_control ctl = {
334 .devname = NULL,
335 .offset = 0,
336 .count = 0,
337 .length = 0
338 };
1ad8ef91
KZ
339
340 static const struct option longopts[] = {
341 { "help", no_argument, NULL, 'h' },
c16970f7 342 { "count", required_argument, NULL, 'c' }, /* max #of zones to operate on */
f1b8b84d 343 { "length", required_argument, NULL, 'l' }, /* max of sectors to operate on */
1ad8ef91
KZ
344 { "offset", required_argument, NULL, 'o' }, /* starting LBA */
345 { "verbose", no_argument, NULL, 'v' },
346 { "version", no_argument, NULL, 'V' },
347 { NULL, 0, NULL, 0 }
348 };
c16970f7
KZ
349 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
350 { 'c', 'l' },
351 { 0 }
352 };
353 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
354
1ad8ef91
KZ
355
356 setlocale(LC_ALL, "");
357 bindtextdomain(PACKAGE, LOCALEDIR);
358 textdomain(PACKAGE);
359 atexit(close_stdout);
360
361 if (argc >= 2 && *argv[1] != '-') {
362 ctl.command = name_to_command(argv[1]);
363 if (!ctl.command)
364 errx(EXIT_FAILURE, _("%s is not valid command name"), argv[1]);
365 argv++;
366 argc--;
367 }
368
f1b8b84d 369 while ((c = getopt_long(argc, argv, "hc:l:o:vV", longopts, NULL)) != -1) {
c16970f7
KZ
370
371 err_exclusive_options(c, longopts, excl, excl_st);
372
1ad8ef91
KZ
373 switch (c) {
374 case 'h':
86be6a32 375 usage();
1ad8ef91 376 break;
f1b8b84d
DLM
377 case 'c':
378 ctl.count = strtou32_or_err(optarg,
379 _("failed to parse number of zones"));
380 break;
1ad8ef91
KZ
381 case 'l':
382 ctl.length = strtosize_or_err(optarg,
f1b8b84d 383 _("failed to parse number of sectors"));
1ad8ef91
KZ
384 break;
385 case 'o':
386 ctl.offset = strtosize_or_err(optarg,
387 _("failed to parse zone offset"));
388 break;
389 case 'v':
390 ctl.verbose = 1;
391 break;
392 case 'V':
393 printf(UTIL_LINUX_VERSION);
394 return EXIT_SUCCESS;
395 default:
396 errtryhelp(EXIT_FAILURE);
397 }
398 }
399
400 if (!ctl.command)
401 errx(EXIT_FAILURE, _("no command specified"));
402
403 if (optind == argc)
404 errx(EXIT_FAILURE, _("no device specified"));
405 ctl.devname = argv[optind++];
406
407 if (optind != argc)
408 errx(EXIT_FAILURE,_("unexpected number of arguments"));
409
410 if (ctl.command->handler(&ctl) < 0)
411 return EXIT_FAILURE;
412
413 return EXIT_SUCCESS;
414
415}