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