]> git.ipfire.org Git - thirdparty/util-linux.git/blob - disk-utils/blockdev.c
disk-utils: add SPDX and Copyright notices
[thirdparty/util-linux.git] / disk-utils / blockdev.c
1 /*
2 * SPDX-License-Identifier: GPL-2.0-or-later
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * blockdev.c --- Do various simple block device ioctls from the command line
10 * aeb, 991028
11 *
12 * Copyright (C) 2007-2023 Karel Zak <kzak@redhat.com>
13 */
14 #include <stdio.h>
15 #include <fcntl.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <unistd.h>
19 #include <sys/ioctl.h>
20 #include <errno.h>
21 #ifdef HAVE_LINUX_BLKZONED_H
22 #include <linux/blkzoned.h>
23 #endif
24
25 #include "c.h"
26 #include "nls.h"
27 #include "blkdev.h"
28 #include "pathnames.h"
29 #include "closestream.h"
30 #include "strutils.h"
31 #include "sysfs.h"
32
33 struct bdc {
34 long ioc; /* ioctl code */
35 const char *iocname; /* ioctl name (e.g. BLKROSET) */
36 long argval; /* default argument */
37
38 const char *name; /* --setfoo */
39 const char *argname; /* argument name or NULL */
40
41 const char *help;
42
43 int argtype;
44 int flags;
45 };
46
47 /* command flags */
48 enum {
49 FL_NOPTR = (1 << 1), /* does not assume pointer (ARG_INT only)*/
50 FL_NORESULT = (1 << 2) /* does not return any data */
51 };
52
53 /* ioctl argument types */
54 enum {
55 ARG_NONE,
56 ARG_USHRT,
57 ARG_INT,
58 ARG_UINT,
59 ARG_LONG,
60 ARG_ULONG,
61 ARG_LLONG,
62 ARG_ULLONG
63 };
64
65 #define IOCTL_ENTRY( io ) .ioc = io, .iocname = # io
66
67 static const struct bdc bdcms[] =
68 {
69 {
70 IOCTL_ENTRY(BLKROSET),
71 .name = "--setro",
72 .argtype = ARG_INT,
73 .argval = 1,
74 .flags = FL_NORESULT,
75 .help = N_("set read-only")
76 },{
77 IOCTL_ENTRY(BLKROSET),
78 .name = "--setrw",
79 .argtype = ARG_INT,
80 .argval = 0,
81 .flags = FL_NORESULT,
82 .help = N_("set read-write")
83 },{
84 IOCTL_ENTRY(BLKROGET),
85 .name = "--getro",
86 .argtype = ARG_INT,
87 .argval = -1,
88 .help = N_("get read-only")
89 },{
90 IOCTL_ENTRY(BLKDISCARDZEROES),
91 .name = "--getdiscardzeroes",
92 .argtype = ARG_UINT,
93 .argval = -1,
94 .help = N_("get discard zeroes support status")
95 },{
96 IOCTL_ENTRY(BLKSSZGET),
97 .name = "--getss",
98 .argtype = ARG_INT,
99 .argval = -1,
100 .help = N_("get logical block (sector) size")
101 },{
102 IOCTL_ENTRY(BLKPBSZGET),
103 .name = "--getpbsz",
104 .argtype = ARG_UINT,
105 .argval = -1,
106 .help = N_("get physical block (sector) size")
107 },{
108 IOCTL_ENTRY(BLKIOMIN),
109 .name = "--getiomin",
110 .argtype = ARG_UINT,
111 .argval = -1,
112 .help = N_("get minimum I/O size")
113 },{
114 IOCTL_ENTRY(BLKIOOPT),
115 .name = "--getioopt",
116 .argtype = ARG_UINT,
117 .argval = -1,
118 .help = N_("get optimal I/O size")
119 },{
120 IOCTL_ENTRY(BLKALIGNOFF),
121 .name = "--getalignoff",
122 .argtype = ARG_INT,
123 .argval = -1,
124 .help = N_("get alignment offset in bytes")
125 },{
126 IOCTL_ENTRY(BLKSECTGET),
127 .name = "--getmaxsect",
128 .argtype = ARG_USHRT,
129 .argval = -1,
130 .help = N_("get max sectors per request")
131 },{
132 IOCTL_ENTRY(BLKBSZGET),
133 .name = "--getbsz",
134 .argtype = ARG_INT,
135 .argval = -1,
136 .help = N_("get blocksize")
137 },{
138 IOCTL_ENTRY(BLKBSZSET),
139 .name = "--setbsz",
140 .argname = "<bytes>",
141 .argtype = ARG_INT,
142 .flags = FL_NORESULT,
143 .help = N_("set blocksize on file descriptor opening the block device")
144 },{
145 IOCTL_ENTRY(BLKGETSIZE),
146 .name = "--getsize",
147 .argtype = ARG_ULONG,
148 .argval = -1,
149 .help = N_("get 32-bit sector count (deprecated, use --getsz)")
150 },{
151 IOCTL_ENTRY(BLKGETSIZE64),
152 .name = "--getsize64",
153 .argtype = ARG_ULLONG,
154 .argval = -1,
155 .help = N_("get size in bytes")
156 },{
157 IOCTL_ENTRY(BLKRASET),
158 .name = "--setra",
159 .argname = "<sectors>",
160 .argtype = ARG_INT,
161 .flags = FL_NOPTR | FL_NORESULT,
162 .help = N_("set readahead")
163 },{
164 IOCTL_ENTRY(BLKRAGET),
165 .name = "--getra",
166 .argtype = ARG_LONG,
167 .argval = -1,
168 .help = N_("get readahead")
169 },{
170 IOCTL_ENTRY(BLKFRASET),
171 .name = "--setfra",
172 .argname = "<sectors>",
173 .argtype = ARG_INT,
174 .flags = FL_NOPTR | FL_NORESULT,
175 .help = N_("set filesystem readahead")
176 },{
177 IOCTL_ENTRY(BLKFRAGET),
178 .name = "--getfra",
179 .argtype = ARG_LONG,
180 .argval = -1,
181 .help = N_("get filesystem readahead")
182 },{
183 IOCTL_ENTRY(BLKGETDISKSEQ),
184 .name = "--getdiskseq",
185 .argtype = ARG_ULLONG,
186 .argval = -1,
187 .help = N_("get disk sequence number")
188 },{
189 #ifdef BLKGETZONESZ
190 IOCTL_ENTRY(BLKGETZONESZ),
191 .name = "--getzonesz",
192 .argtype = ARG_UINT,
193 .argval = -1,
194 .help = N_("get zone size")
195 },{
196 #endif
197 IOCTL_ENTRY(BLKFLSBUF),
198 .name = "--flushbufs",
199 .help = N_("flush buffers")
200 },{
201 IOCTL_ENTRY(BLKRRPART),
202 .name = "--rereadpt",
203 .help = N_("reread partition table")
204 }
205 };
206
207 static void __attribute__((__noreturn__)) usage(void)
208 {
209 size_t i;
210
211 fputs(USAGE_HEADER, stdout);
212 fprintf(stdout, _(
213 " %1$s [-v|-q] commands devices\n"
214 " %1$s --report [devices]\n"
215 " %1$s -h|-V\n"
216 ), program_invocation_short_name);
217
218 fputs(USAGE_SEPARATOR, stdout);
219 fputs( _("Call block device ioctls from the command line."), stdout);
220
221 fputs(USAGE_OPTIONS, stdout);
222 fputs( _(" -q quiet mode"), stdout);
223 fputs( _(" -v verbose mode"), stdout);
224 fputs( _(" --report print report for specified (or all) devices"), stdout);
225 fputs(USAGE_SEPARATOR, stdout);
226 fprintf(stdout, USAGE_HELP_OPTIONS(16));
227
228 fputs(USAGE_SEPARATOR, stdout);
229 fputs(_("Available commands:"), stdout);
230 fprintf(stdout, _(" %-25s get size in 512-byte sectors\n"), "--getsz");
231 for (i = 0; i < ARRAY_SIZE(bdcms); i++) {
232 if (bdcms[i].argname)
233 fprintf(stdout, " %s %-*s %s\n", bdcms[i].name,
234 (int)(24 - strlen(bdcms[i].name)),
235 bdcms[i].argname, _(bdcms[i].help));
236 else
237 fprintf(stdout, " %-25s %s\n", bdcms[i].name,
238 _(bdcms[i].help));
239 }
240
241 fprintf(stdout, USAGE_MAN_TAIL("blockdev(8)"));
242 exit(EXIT_SUCCESS);
243 }
244
245 static int find_cmd(char *s)
246 {
247 size_t j;
248
249 for (j = 0; j < ARRAY_SIZE(bdcms); j++)
250 if (!strcmp(s, bdcms[j].name))
251 return j;
252 return -1;
253 }
254
255 static void do_commands(int fd, char **argv, int d);
256 static void report_header(void);
257 static void report_device(char *device, int quiet);
258 static void report_all_devices(void);
259
260 int main(int argc, char **argv)
261 {
262 int fd, d, j, k;
263
264 setlocale(LC_ALL, "");
265 bindtextdomain(PACKAGE, LOCALEDIR);
266 textdomain(PACKAGE);
267 close_stdout_atexit();
268
269 if (argc < 2) {
270 warnx(_("not enough arguments"));
271 errtryhelp(EXIT_FAILURE);
272 }
273
274 /* -V not together with commands */
275 if (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version"))
276 print_version(EXIT_SUCCESS);
277 if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))
278 usage();
279
280 /* --report not together with other commands */
281 if (!strcmp(argv[1], "--report")) {
282 report_header();
283 if (argc > 2) {
284 for (d = 2; d < argc; d++)
285 report_device(argv[d], 0);
286 } else {
287 report_all_devices();
288 }
289 return EXIT_SUCCESS;
290 }
291
292 /* do each of the commands on each of the devices */
293 /* devices start after last command */
294 for (d = 1; d < argc; d++) {
295 j = find_cmd(argv[d]);
296 if (j >= 0) {
297 if (bdcms[j].argname)
298 d++;
299 continue;
300 }
301 if (!strcmp(argv[d], "--getsz"))
302 continue;
303 if (!strcmp(argv[d], "--")) {
304 d++;
305 break;
306 }
307 if (argv[d][0] != '-')
308 break;
309 }
310
311 if (d >= argc) {
312 warnx(_("no device specified"));
313 errtryhelp(EXIT_FAILURE);
314 }
315
316 for (k = d; k < argc; k++) {
317 fd = open(argv[k], O_RDONLY, 0);
318 if (fd < 0)
319 err(EXIT_FAILURE, _("cannot open %s"), argv[k]);
320 do_commands(fd, argv, d);
321 close(fd);
322 }
323 return EXIT_SUCCESS;
324 }
325
326 static void do_commands(int fd, char **argv, int d)
327 {
328 int res, i, j;
329 int iarg = 0;
330 unsigned int uarg = 0;
331 unsigned short huarg = 0;
332 long larg = 0;
333 long long llarg = 0;
334 unsigned long lu = 0;
335 unsigned long long llu = 0;
336 int verbose = 0;
337
338 for (i = 1; i < d; i++) {
339 if (!strcmp(argv[i], "-v")) {
340 verbose = 1;
341 continue;
342 }
343 if (!strcmp(argv[i], "-q")) {
344 verbose = 0;
345 continue;
346 }
347
348 if (!strcmp(argv[i], "--getsz")) {
349 res = blkdev_get_sectors(fd, &llu);
350 if (res == 0)
351 printf("%lld\n", llu);
352 else
353 errx(EXIT_FAILURE,
354 _("could not get device size"));
355 continue;
356 }
357
358 j = find_cmd(argv[i]);
359 if (j == -1) {
360 warnx(_("Unknown command: %s"), argv[i]);
361 errtryhelp(EXIT_FAILURE);
362 }
363
364 switch (bdcms[j].argtype) {
365 default:
366 case ARG_NONE:
367 res = ioctl(fd, bdcms[j].ioc, 0);
368 break;
369 case ARG_USHRT:
370 huarg = bdcms[j].argval;
371 res = ioctl(fd, bdcms[j].ioc, &huarg);
372 break;
373 case ARG_INT:
374 if (bdcms[j].argname) {
375 if (i == d - 1) {
376 warnx(_("%s requires an argument"),
377 bdcms[j].name);
378 errtryhelp(EXIT_FAILURE);
379 }
380 iarg = strtos32_or_err(argv[++i], _("failed to parse command argument"));
381 } else
382 iarg = bdcms[j].argval;
383
384 res = bdcms[j].flags & FL_NOPTR ?
385 ioctl(fd, bdcms[j].ioc, iarg) :
386 ioctl(fd, bdcms[j].ioc, &iarg);
387 break;
388 case ARG_UINT:
389 uarg = bdcms[j].argval;
390 res = ioctl(fd, bdcms[j].ioc, &uarg);
391 break;
392 case ARG_LONG:
393 larg = bdcms[j].argval;
394 res = ioctl(fd, bdcms[j].ioc, &larg);
395 break;
396 case ARG_LLONG:
397 llarg = bdcms[j].argval;
398 res = ioctl(fd, bdcms[j].ioc, &llarg);
399 break;
400 case ARG_ULONG:
401 lu = bdcms[j].argval;
402 res = ioctl(fd, bdcms[j].ioc, &lu);
403 break;
404 case ARG_ULLONG:
405 llu = bdcms[j].argval;
406 res = ioctl(fd, bdcms[j].ioc, &llu);
407 break;
408 }
409
410 if (res == -1) {
411 warn(_("ioctl error on %s"), bdcms[j].iocname);
412 if (verbose)
413 printf(_("%s failed.\n"), _(bdcms[j].help));
414 exit(EXIT_FAILURE);
415 }
416
417 if (bdcms[j].argtype == ARG_NONE ||
418 (bdcms[j].flags & FL_NORESULT)) {
419 if (verbose)
420 printf(_("%s succeeded.\n"), _(bdcms[j].help));
421 continue;
422 }
423
424 if (verbose)
425 printf("%s: ", _(bdcms[j].help));
426
427 switch (bdcms[j].argtype) {
428 case ARG_USHRT:
429 printf("%hu\n", huarg);
430 break;
431 case ARG_INT:
432 printf("%d\n", iarg);
433 break;
434 case ARG_UINT:
435 printf("%u\n", uarg);
436 break;
437 case ARG_LONG:
438 printf("%ld\n", larg);
439 break;
440 case ARG_LLONG:
441 printf("%lld\n", llarg);
442 break;
443 case ARG_ULONG:
444 printf("%lu\n", lu);
445 break;
446 case ARG_ULLONG:
447 printf("%llu\n", llu);
448 break;
449 }
450 }
451 }
452
453 static void report_all_devices(void)
454 {
455 FILE *procpt;
456 char line[200];
457 char ptname[200 + 1];
458 char device[210];
459 int ma, mi, sz;
460
461 procpt = fopen(_PATH_PROC_PARTITIONS, "r");
462 if (!procpt)
463 err(EXIT_FAILURE, _("cannot open %s"), _PATH_PROC_PARTITIONS);
464
465 while (fgets(line, sizeof(line), procpt)) {
466 if (sscanf(line, " %d %d %d %200[^\n ]",
467 &ma, &mi, &sz, ptname) != 4)
468 continue;
469
470 snprintf(device, sizeof(device), "/dev/%s", ptname);
471 report_device(device, 1);
472 }
473
474 fclose(procpt);
475 }
476
477 static void report_device(char *device, int quiet)
478 {
479 int fd;
480 int ro, ssz, bsz;
481 long ra;
482 unsigned long long bytes;
483 uint64_t start = 0;
484 char start_str[16] = { "\0" };
485 struct stat st;
486
487 fd = open(device, O_RDONLY | O_NONBLOCK);
488 if (fd < 0) {
489 if (!quiet)
490 warn(_("cannot open %s"), device);
491 return;
492 }
493
494 ro = ssz = bsz = 0;
495 ra = 0;
496 if (fstat(fd, &st) == 0) {
497 dev_t disk;
498 struct path_cxt *pc;
499
500 pc = ul_new_sysfs_path(st.st_rdev, NULL, NULL);
501 if (pc &&
502 sysfs_blkdev_get_wholedisk(pc, NULL, 0, &disk) == 0 &&
503 disk != st.st_rdev) {
504
505 if (ul_path_read_u64(pc, &start, "start") != 0)
506 /* TRANSLATORS: Start sector not available. Max. 15 letters. */
507 snprintf(start_str, sizeof(start_str), "%15s", _("N/A"));
508 }
509 ul_unref_path(pc);
510 }
511 if (!*start_str)
512 snprintf(start_str, sizeof(start_str), "%15ju", start);
513
514 if (ioctl(fd, BLKROGET, &ro) == 0 &&
515 ioctl(fd, BLKRAGET, &ra) == 0 &&
516 ioctl(fd, BLKSSZGET, &ssz) == 0 &&
517 ioctl(fd, BLKBSZGET, &bsz) == 0 &&
518 blkdev_get_size(fd, &bytes) == 0) {
519 printf("%s %5ld %5d %5d %s %15lld %s\n",
520 ro ? "ro" : "rw", ra, ssz, bsz, start_str, bytes, device);
521 } else {
522 if (!quiet)
523 warnx(_("ioctl error on %s"), device);
524 }
525
526 close(fd);
527 }
528
529 static void report_header(void)
530 {
531 printf(_("RO RA SSZ BSZ StartSec Size Device\n"));
532 }