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