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