]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/blkzone.c
hwclock: free temporary variable before return
[thirdparty/util-linux.git] / sys-utils / blkzone.c
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 <stdbool.h>
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"
46 #include "optutils.h"
47
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
61 struct blkzone_control;
62
63 static int blkzone_report(struct blkzone_control *ctl);
64 static int blkzone_action(struct blkzone_control *ctl);
65
66 struct blkzone_command {
67 const char *name;
68 int (*handler)(struct blkzone_control *);
69 unsigned long ioctl_cmd;
70 const char *ioctl_name;
71 const char *help;
72 };
73
74 struct 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;
83 uint32_t count;
84
85 unsigned int force : 1;
86 unsigned int verbose : 1;
87 };
88
89 static const struct blkzone_command commands[] = {
90 {
91 .name = "report",
92 .handler = blkzone_report,
93 .help = N_("Report zone information about the given device")
94 },{
95 .name = "capacity",
96 .handler = blkzone_report,
97 .help = N_("Report sum of zone capacities for the given device")
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 }
123 };
124
125 static 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
137 static 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
160 /*
161 * Get the device zone size indicated by chunk sectors).
162 */
163 static unsigned long blkdev_chunk_sectors(const char *dname)
164 {
165 struct path_cxt *pc = NULL;
166 dev_t devno = sysfs_devname_to_devno(dname);
167 dev_t disk;
168 uint64_t sz = 0;
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 */
176 pc = ul_new_sysfs_path(devno, NULL, NULL);
177 if (!pc)
178 return 0;
179
180 rc = sysfs_blkdev_get_wholedisk(pc, NULL, 0, &disk);
181 if (rc != 0)
182 goto done;
183
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 }
190
191 rc = ul_path_read_u64(pc, &sz, "queue/chunk_sectors");
192 done:
193 ul_unref_path(pc);
194 return rc == 0 ? sz : 0;
195 }
196
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
205 /*
206 * blkzone report
207 */
208 #define DEF_REPORT_LEN (1U << 12) /* 4k zones per report (256k kzalloc) */
209
210 static const char *type_text[] = {
211 "RESERVED",
212 "CONVENTIONAL",
213 "SEQ_WRITE_REQUIRED",
214 "SEQ_WRITE_PREFERRED",
215 };
216
217 static const char *condition_str[] = {
218 "nw", /* Not write pointer */
219 "em", /* Empty */
220 "oi", /* Implicitly opened */
221 "oe", /* Explicitly opened */
222 "cl", /* Closed */
223 "x5", "x6", "x7", "x8", "x9", "xA", "xB", "xC", /* xN: reserved */
224 "ro", /* Read only */
225 "fu", /* Full */
226 "of" /* Offline */
227 };
228
229 static int blkzone_report(struct blkzone_control *ctl)
230 {
231 bool only_capacity_sum = !strcmp(ctl->command->name, "capacity");
232 uint64_t capacity_sum = 0;
233 struct blk_zone_report *zi;
234 unsigned long zonesize;
235 uint32_t i, nr_zones;
236 int fd;
237
238 fd = init_device(ctl, O_RDONLY);
239
240 if (ctl->offset >= ctl->total_sectors)
241 errx(EXIT_FAILURE,
242 _("%s: offset is greater than or equal to device size"), ctl->devname);
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;
254
255 zi = xmalloc(sizeof(struct blk_zone_report) +
256 (DEF_REPORT_LEN * sizeof(struct blk_zone)));
257
258 while (nr_zones && ctl->offset < ctl->total_sectors) {
259
260 zi->nr_zones = min(nr_zones, DEF_REPORT_LEN);
261 zi->sector = ctl->offset;
262
263 if (ioctl(fd, BLKREPORTZONE, zi) == -1)
264 err(EXIT_FAILURE, _("%s: BLKREPORTZONE ioctl failed"), ctl->devname);
265
266 if (ctl->verbose)
267 printf(_("Found %d zones from 0x%"PRIx64"\n"),
268 zi->nr_zones, ctl->offset);
269
270 if (!zi->nr_zones)
271 break;
272
273 for (i = 0; i < zi->nr_zones; i++) {
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;
280 uint64_t cap;
281
282 if (!len) {
283 nr_zones = 0;
284 break;
285 }
286
287 if (has_zone_capacity(zi))
288 cap = zone_capacity(&entry);
289 else
290 cap = entry.len;
291
292 if (only_capacity_sum) {
293 capacity_sum += cap;
294 } else if (has_zone_capacity(zi)) {
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,
299 entry.reset, entry.non_seq,
300 cond, condition_str[cond & (ARRAY_SIZE(condition_str) - 1)],
301 type, type_text[type]);
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,
307 entry.reset, entry.non_seq,
308 cond, condition_str[cond & (ARRAY_SIZE(condition_str) - 1)],
309 type, type_text[type]);
310 }
311
312 nr_zones--;
313 ctl->offset = start + len;
314 }
315
316 }
317
318 if (only_capacity_sum)
319 printf(_("0x%09"PRIx64"\n"), capacity_sum);
320
321 free(zi);
322 close(fd);
323
324 return 0;
325 }
326
327 /*
328 * blkzone reset, open, close, and finish.
329 */
330 static int blkzone_action(struct blkzone_control *ctl)
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
341 fd = init_device(ctl, O_WRONLY | (ctl->force ? 0 : O_EXCL));
342
343 if (ctl->offset % zonesize )
344 errx(EXIT_FAILURE, _("%s: offset %" PRIu64 " is not aligned "
345 "to zone size %lu"),
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
351 if (ctl->count)
352 zlen = ctl->count * zonesize;
353 else if (ctl->length)
354 zlen = ctl->length;
355 else
356 zlen = ctl->total_sectors;
357 if (ctl->offset + zlen > ctl->total_sectors)
358 zlen = ctl->total_sectors - ctl->offset;
359
360 if (ctl->length &&
361 (zlen % zonesize) &&
362 ctl->offset + zlen != ctl->total_sectors)
363 errx(EXIT_FAILURE, _("%s: number of sectors %" PRIu64 " is not aligned "
364 "to zone size %lu"),
365 ctl->devname, ctl->length, zonesize);
366
367 za.sector = ctl->offset;
368 za.nr_sectors = zlen;
369
370 if (ioctl(fd, ctl->command->ioctl_cmd, &za) == -1)
371 err(EXIT_FAILURE, _("%s: %s ioctl failed"),
372 ctl->devname, ctl->command->ioctl_name);
373 else if (ctl->verbose)
374 printf(_("%s: successful %s of zones in range from %" PRIu64 ", to %" PRIu64),
375 ctl->devname,
376 ctl->command->name,
377 ctl->offset,
378 ctl->offset + zlen);
379 close(fd);
380 return 0;
381 }
382
383 static void __attribute__((__noreturn__)) usage(void)
384 {
385 FILE *out = stdout;
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
394 fputs(USAGE_COMMANDS, out);
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);
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);
402 fputs(_(" -f, --force enforce on block devices used by the system\n"), out);
403 fputs(_(" -v, --verbose display more details\n"), out);
404 fputs(USAGE_SEPARATOR, out);
405 fprintf(out, USAGE_HELP_OPTIONS(24));
406
407 fputs(USAGE_ARGUMENTS, out);
408 fprintf(out, USAGE_ARG_SIZE(_("<sector> and <sectors>")));
409
410 fprintf(out, USAGE_MAN_TAIL("blkzone(8)"));
411 exit(EXIT_SUCCESS);
412 }
413
414 int main(int argc, char **argv)
415 {
416 int c;
417 struct blkzone_control ctl = {
418 .devname = NULL
419 };
420
421 static const struct option longopts[] = {
422 { "help", no_argument, NULL, 'h' },
423 { "count", required_argument, NULL, 'c' }, /* max #of zones to operate on */
424 { "length", required_argument, NULL, 'l' }, /* max of sectors to operate on */
425 { "offset", required_argument, NULL, 'o' }, /* starting LBA */
426 { "force", no_argument, NULL, 'f' },
427 { "verbose", no_argument, NULL, 'v' },
428 { "version", no_argument, NULL, 'V' },
429 { NULL, 0, NULL, 0 }
430 };
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
437
438 setlocale(LC_ALL, "");
439 bindtextdomain(PACKAGE, LOCALEDIR);
440 textdomain(PACKAGE);
441 close_stdout_atexit();
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
451 while ((c = getopt_long(argc, argv, "hc:l:o:fvV", longopts, NULL)) != -1) {
452
453 err_exclusive_options(c, longopts, excl, excl_st);
454
455 switch (c) {
456 case 'c':
457 ctl.count = strtou32_or_err(optarg,
458 _("failed to parse number of zones"));
459 break;
460 case 'l':
461 ctl.length = strtosize_or_err(optarg,
462 _("failed to parse number of sectors"));
463 break;
464 case 'o':
465 ctl.offset = strtosize_or_err(optarg,
466 _("failed to parse zone offset"));
467 break;
468 case 'f':
469 ctl.force = 1;
470 break;
471 case 'v':
472 ctl.verbose = 1;
473 break;
474
475 case 'h':
476 usage();
477 case 'V':
478 print_version(EXIT_SUCCESS);
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 }