]> git.ipfire.org Git - thirdparty/util-linux.git/blame - misc-utils/lsblk.c
docs: Fix word repetitions
[thirdparty/util-linux.git] / misc-utils / lsblk.c
CommitLineData
2a4c734b 1/*
f77fa578 2 * lsblk(8) - list block devices
2a4c734b 3 *
28ffc2b7 4 * Copyright (C) 2010,2011,2012 Red Hat, Inc. All rights reserved.
2a4c734b
MB
5 * Written by Milan Broz <mbroz@redhat.com>
6 * Karel Zak <kzak@redhat.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it would be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
7cebf0bb
SK
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2a4c734b
MB
21 */
22
23#include <stdio.h>
24#include <errno.h>
25#include <getopt.h>
2a4c734b
MB
26#include <stdlib.h>
27#include <unistd.h>
28#include <stdint.h>
29#include <sys/types.h>
30#include <sys/stat.h>
31#include <dirent.h>
32#include <fcntl.h>
33#include <string.h>
2a4c734b
MB
34#include <sys/ioctl.h>
35#include <inttypes.h>
36#include <stdarg.h>
37#include <locale.h>
38#include <pwd.h>
39#include <grp.h>
18eac5ba 40#include <ctype.h>
2a4c734b
MB
41
42#include <blkid.h>
9554f7ab 43#include <libmount.h>
9bd4e5c0 44#include <libsmartcols.h>
2a4c734b 45
f17f5f48
IM
46#ifdef HAVE_LIBUDEV
47#include <libudev.h>
48#endif
49
2a4c734b
MB
50#include <assert.h>
51
540d506f 52#include "c.h"
2a4c734b
MB
53#include "pathnames.h"
54#include "blkdev.h"
55#include "canonicalize.h"
2a4c734b 56#include "nls.h"
2a4c734b
MB
57#include "xalloc.h"
58#include "strutils.h"
766ab9b0 59#include "sysfs.h"
c05a80ca 60#include "closestream.h"
d324270e 61#include "mangle.h"
05187653 62#include "optutils.h"
2a4c734b 63
7e786eca
KZ
64#include "debug.h"
65
66UL_DEBUG_DEFINE_MASK(lsblk);
67UL_DEBUG_DEFINE_MASKNAMES(lsblk) = UL_DEBUG_EMPTY_MASKNAMES;
68
69#define LSBLK_DEBUG_INIT (1 << 1)
70#define LSBLK_DEBUG_FILTER (1 << 2)
71#define LSBLK_DEBUG_DEV (1 << 3)
72#define LSBLK_DEBUG_CXT (1 << 4)
73#define LSBLK_DEBUG_ALL 0xFFFF
74
75#define DBG(m, x) __UL_DBG(lsblk, LSBLK_DEBUG_, m, x)
76#define ON_DBG(m, x) __UL_DBG_CALL(lsblk, LSBLK_DEBUG_, m, x)
77
1539750f
KZ
78
79#define LSBLK_EXIT_SOMEOK 64
80#define LSBLK_EXIT_ALLFAILED 32
81
2a4c734b
MB
82/* column IDs */
83enum {
84 COL_NAME = 0,
85 COL_KNAME,
86 COL_MAJMIN,
87 COL_FSTYPE,
88 COL_TARGET,
89 COL_LABEL,
90 COL_UUID,
4686ba17 91 COL_PARTTYPE,
aa0903e0
KZ
92 COL_PARTLABEL,
93 COL_PARTUUID,
5a2fd932 94 COL_PARTFLAGS,
150db7a9 95 COL_RA,
2a4c734b 96 COL_RO,
627970a7 97 COL_RM,
483987c2 98 COL_HOTPLUG,
2a4c734b 99 COL_MODEL,
460c7afb 100 COL_SERIAL,
2a4c734b 101 COL_SIZE,
01e487c0 102 COL_STATE,
2a4c734b
MB
103 COL_OWNER,
104 COL_GROUP,
105 COL_MODE,
106 COL_ALIOFF,
107 COL_MINIO,
108 COL_OPTIO,
109 COL_PHYSEC,
110 COL_LOGSEC,
111 COL_ROTA,
112 COL_SCHED,
64c9e22a 113 COL_RQ_SIZE,
18eac5ba 114 COL_TYPE,
2d232246
MP
115 COL_DALIGN,
116 COL_DGRAN,
117 COL_DMAX,
118 COL_DZERO,
2adb1a44 119 COL_WSAME,
88ca32b3 120 COL_WWN,
12b06c3d 121 COL_RAND,
310c0603 122 COL_PKNAME,
699e5c4f 123 COL_HCTL,
a5fb4d23 124 COL_TRANSPORT,
7f14ee1b 125 COL_SUBSYS,
d6681cee 126 COL_REV,
483987c2 127 COL_VENDOR
2a4c734b
MB
128};
129
9bd4e5c0
OO
130/* basic table settings */
131enum {
132 LSBLK_ASCII = (1 << 0),
133 LSBLK_RAW = (1 << 1),
134 LSBLK_NOHEADINGS = (1 << 2),
bb6e822a
KZ
135 LSBLK_EXPORT = (1 << 3),
136 LSBLK_TREE = (1 << 4),
4a102a48 137 LSBLK_JSON = (1 << 5),
9bd4e5c0
OO
138};
139
642048e4
KZ
140enum {
141 SORT_STRING = 0, /* default is to use scols_cell_get_data() */
142 SORT_U64 = 1 /* use private pointer from scols_cell_get_userdata() */
143};
144
2a4c734b
MB
145/* column names */
146struct colinfo {
147 const char *name; /* header */
148 double whint; /* width hint (N < 1 is in percent of termwidth) */
9bd4e5c0 149 int flags; /* SCOLS_FL_* */
2a4c734b 150 const char *help;
642048e4
KZ
151
152 int sort_type; /* SORT_* */
2a4c734b
MB
153};
154
155/* columns descriptions */
507d39c2 156static struct colinfo infos[] = {
9bd4e5c0 157 [COL_NAME] = { "NAME", 0.25, SCOLS_FL_TREE | SCOLS_FL_NOEXTREMES, N_("device name") },
e22d8b95 158 [COL_KNAME] = { "KNAME", 0.3, 0, N_("internal kernel device name") },
310c0603 159 [COL_PKNAME] = { "PKNAME", 0.3, 0, N_("internal parent kernel device name") },
642048e4 160 [COL_MAJMIN] = { "MAJ:MIN", 6, 0, N_("major:minor device number"), SORT_U64 },
9bd4e5c0
OO
161 [COL_FSTYPE] = { "FSTYPE", 0.1, SCOLS_FL_TRUNC, N_("filesystem type") },
162 [COL_TARGET] = { "MOUNTPOINT", 0.10, SCOLS_FL_TRUNC, N_("where the device is mounted") },
2a4c734b
MB
163 [COL_LABEL] = { "LABEL", 0.1, 0, N_("filesystem LABEL") },
164 [COL_UUID] = { "UUID", 36, 0, N_("filesystem UUID") },
aa0903e0 165
4686ba17 166 [COL_PARTTYPE] = { "PARTTYPE", 36, 0, N_("partition type UUID") },
aa0903e0
KZ
167 [COL_PARTLABEL] = { "PARTLABEL", 0.1, 0, N_("partition LABEL") },
168 [COL_PARTUUID] = { "PARTUUID", 36, 0, N_("partition UUID") },
5a2fd932 169 [COL_PARTFLAGS] = { "PARTFLAGS", 36, 0, N_("partition flags") },
aa0903e0 170
642048e4 171 [COL_RA] = { "RA", 3, SCOLS_FL_RIGHT, N_("read-ahead of the device"), SORT_U64 },
9bd4e5c0
OO
172 [COL_RO] = { "RO", 1, SCOLS_FL_RIGHT, N_("read-only device") },
173 [COL_RM] = { "RM", 1, SCOLS_FL_RIGHT, N_("removable device") },
483987c2 174 [COL_HOTPLUG]= { "HOTPLUG", 1, SCOLS_FL_RIGHT, N_("removable or hotplug device (usb, pcmcia, ...)") },
9bd4e5c0
OO
175 [COL_ROTA] = { "ROTA", 1, SCOLS_FL_RIGHT, N_("rotational device") },
176 [COL_RAND] = { "RAND", 1, SCOLS_FL_RIGHT, N_("adds randomness") },
177 [COL_MODEL] = { "MODEL", 0.1, SCOLS_FL_TRUNC, N_("device identifier") },
178 [COL_SERIAL] = { "SERIAL", 0.1, SCOLS_FL_TRUNC, N_("disk serial number") },
642048e4 179 [COL_SIZE] = { "SIZE", 5, SCOLS_FL_RIGHT, N_("size of the device"), SORT_U64 },
9bd4e5c0
OO
180 [COL_STATE] = { "STATE", 7, SCOLS_FL_TRUNC, N_("state of the device") },
181 [COL_OWNER] = { "OWNER", 0.1, SCOLS_FL_TRUNC, N_("user name"), },
182 [COL_GROUP] = { "GROUP", 0.1, SCOLS_FL_TRUNC, N_("group name") },
2a4c734b 183 [COL_MODE] = { "MODE", 10, 0, N_("device node permissions") },
642048e4
KZ
184 [COL_ALIOFF] = { "ALIGNMENT", 6, SCOLS_FL_RIGHT, N_("alignment offset"), SORT_U64 },
185 [COL_MINIO] = { "MIN-IO", 6, SCOLS_FL_RIGHT, N_("minimum I/O size"), SORT_U64 },
186 [COL_OPTIO] = { "OPT-IO", 6, SCOLS_FL_RIGHT, N_("optimal I/O size"), SORT_U64 },
187 [COL_PHYSEC] = { "PHY-SEC", 7, SCOLS_FL_RIGHT, N_("physical sector size"), SORT_U64 },
188 [COL_LOGSEC] = { "LOG-SEC", 7, SCOLS_FL_RIGHT, N_("logical sector size"), SORT_U64 },
18eac5ba 189 [COL_SCHED] = { "SCHED", 0.1, 0, N_("I/O scheduler name") },
642048e4 190 [COL_RQ_SIZE]= { "RQ-SIZE", 5, SCOLS_FL_RIGHT, N_("request queue size"), SORT_U64 },
2d232246 191 [COL_TYPE] = { "TYPE", 4, 0, N_("device type") },
642048e4
KZ
192 [COL_DALIGN] = { "DISC-ALN", 6, SCOLS_FL_RIGHT, N_("discard alignment offset"), SORT_U64 },
193 [COL_DGRAN] = { "DISC-GRAN", 6, SCOLS_FL_RIGHT, N_("discard granularity"), SORT_U64 },
194 [COL_DMAX] = { "DISC-MAX", 6, SCOLS_FL_RIGHT, N_("discard max bytes"), SORT_U64 },
9bd4e5c0 195 [COL_DZERO] = { "DISC-ZERO", 1, SCOLS_FL_RIGHT, N_("discard zeroes data") },
642048e4 196 [COL_WSAME] = { "WSAME", 6, SCOLS_FL_RIGHT, N_("write same max bytes"), SORT_U64 },
88ca32b3 197 [COL_WWN] = { "WWN", 18, 0, N_("unique storage identifier") },
699e5c4f 198 [COL_HCTL] = { "HCTL", 10, 0, N_("Host:Channel:Target:Lun for SCSI") },
a5fb4d23 199 [COL_TRANSPORT] = { "TRAN", 6, 0, N_("device transport type") },
7f14ee1b 200 [COL_SUBSYS] = { "SUBSYSTEMS", 0.1, SCOLS_FL_NOEXTREMES, N_("de-duplicated chain of subsystems") },
9bd4e5c0
OO
201 [COL_REV] = { "REV", 4, SCOLS_FL_RIGHT, N_("device revision") },
202 [COL_VENDOR] = { "VENDOR", 0.1, SCOLS_FL_TRUNC, N_("device vendor") },
2a4c734b
MB
203};
204
205struct lsblk {
9bd4e5c0 206 struct libscols_table *table; /* output table */
3fd1f771 207 struct libscols_column *sort_col;/* sort output by this column */
642048e4
KZ
208 int sort_id;
209
2dc03af7 210 unsigned int all_devices:1; /* print all devices, including empty */
9feec79c 211 unsigned int bytes:1; /* print SIZE in bytes */
09a71aa1 212 unsigned int inverse:1; /* print inverse dependencies */
9feec79c 213 unsigned int nodeps:1; /* don't print slaves/holders */
28ffc2b7 214 unsigned int scsi:1; /* print only device with HCTL (SCSI) */
c7e76cd1 215 unsigned int paths:1; /* print devnames with "/dev" prefix */
a6dc8dcd 216 unsigned int sort_hidden:1; /* sort column not between output columns */
2a4c734b
MB
217};
218
219struct lsblk *lsblk; /* global handler */
507d39c2 220
b6327c6f
KZ
221/* columns[] array specifies all currently wanted output column. The columns
222 * are defined by infos[] array and you can specify (on command line) each
223 * column twice. That's enough, dynamically allocated array of the columns is
224 * unnecessary overkill and over-engineering in this case */
225static int columns[ARRAY_SIZE(infos) * 2];
40b17508 226static size_t ncolumns;
b6327c6f
KZ
227
228static inline size_t err_columns_index(size_t arysz, size_t idx)
229{
230 if (idx >= arysz)
231 errx(EXIT_FAILURE, _("too many columns specified, "
1d231190 232 "the limit is %zu columns"),
b6327c6f
KZ
233 arysz - 1);
234 return idx;
235}
236
237#define add_column(ary, n, id) \
238 ((ary)[ err_columns_index(ARRAY_SIZE(ary), (n)) ] = (id))
2a4c734b 239
f6da50d4
BV
240static int excludes[256];
241static size_t nexcludes;
2a4c734b 242
2ef4e8ba
KZ
243static int includes[256];
244static size_t nincludes;
245
9554f7ab
KZ
246static struct libmnt_table *mtab, *swaps;
247static struct libmnt_cache *mntcache;
248
d44e3391
KZ
249#ifdef HAVE_LIBUDEV
250struct udev *udev;
251#endif
252
2a4c734b
MB
253struct blkdev_cxt {
254 struct blkdev_cxt *parent;
255
9bd4e5c0 256 struct libscols_line *scols_line;
2a4c734b
MB
257 struct stat st;
258
259 char *name; /* kernel name in /sys/block */
260 char *dm_name; /* DM name (dm/block) */
261
262 char *filename; /* path to device node */
766ab9b0
KZ
263
264 struct sysfs_cxt sysfs;
2a4c734b
MB
265
266 int partition; /* is partition? TRUE/FALSE */
267
268 int probed; /* already probed */
269 char *fstype; /* detected fs, NULL or "?" if cannot detect */
aa0903e0
KZ
270 char *uuid; /* filesystem UUID (or stack uuid) */
271 char *label; /* filesystem label */
9e930041 272 char *parttype; /* partition type UUID */
aa0903e0 273 char *partuuid; /* partition UUID */
9e930041 274 char *partlabel; /* partition label */
5a2fd932 275 char *partflags; /* partition flags */
88ca32b3 276 char *wwn; /* storage WWN */
460c7afb 277 char *serial; /* disk serial number */
2a4c734b 278
09a71aa1 279 int npartitions; /* # of partitions this device has */
2a4c734b 280 int nholders; /* # of devices mapped directly to this device
09a71aa1 281 * /sys/block/.../holders */
2a4c734b
MB
282 int nslaves; /* # of devices this device maps to */
283 int maj, min; /* devno */
98055888 284 int discard; /* supports discard */
2a4c734b
MB
285
286 uint64_t size; /* device size */
287};
288
7e786eca
KZ
289static void lsblk_init_debug(void)
290{
291 __UL_INIT_DEBUG(lsblk, LSBLK_DEBUG_, 0, LSBLK_DEBUG);
292}
293
2a4c734b
MB
294static int is_maj_excluded(int maj)
295{
6aace32f 296 size_t i;
2a4c734b
MB
297
298 assert(ARRAY_SIZE(excludes) > nexcludes);
299
2ef4e8ba 300 if (!nexcludes)
9e930041 301 return 0; /* filter not enabled, device not excluded */
2ef4e8ba 302
7e786eca
KZ
303 for (i = 0; i < nexcludes; i++) {
304 if (excludes[i] == maj) {
305 DBG(FILTER, ul_debug("exclude: maj=%d", maj));
2a4c734b 306 return 1;
7e786eca
KZ
307 }
308 }
2a4c734b
MB
309 return 0;
310}
311
2ef4e8ba
KZ
312static int is_maj_included(int maj)
313{
314 size_t i;
315
316 assert(ARRAY_SIZE(includes) > nincludes);
317
318 if (!nincludes)
319 return 1; /* filter not enabled, device is included */
320
7e786eca
KZ
321 for (i = 0; i < nincludes; i++) {
322 if (includes[i] == maj) {
323 DBG(FILTER, ul_debug("include: maj=%d", maj));
2ef4e8ba 324 return 1;
7e786eca
KZ
325 }
326 }
2ef4e8ba
KZ
327 return 0;
328}
329
2a4c734b
MB
330/* array with IDs of enabled columns */
331static int get_column_id(int num)
332{
40b17508
KZ
333 assert(num >= 0);
334 assert((size_t) num < ncolumns);
b6327c6f 335 assert(columns[num] < (int) ARRAY_SIZE(infos));
2a4c734b
MB
336 return columns[num];
337}
338
339static struct colinfo *get_column_info(int num)
340{
341 return &infos[ get_column_id(num) ];
342}
343
2a4c734b
MB
344static int column_name_to_id(const char *name, size_t namesz)
345{
507d39c2 346 size_t i;
2a4c734b 347
03df0d11 348 for (i = 0; i < ARRAY_SIZE(infos); i++) {
2a4c734b
MB
349 const char *cn = infos[i].name;
350
351 if (!strncasecmp(name, cn, namesz) && !*(cn + namesz))
352 return i;
353 }
354 warnx(_("unknown column: %s"), name);
355 return -1;
356}
357
642048e4
KZ
358static int column_id_to_number(int id)
359{
360 size_t i;
361
40b17508 362 for (i = 0; i < ncolumns; i++)
642048e4
KZ
363 if (columns[i] == id)
364 return i;
365 return -1;
366}
367
2a4c734b
MB
368static void reset_blkdev_cxt(struct blkdev_cxt *cxt)
369{
370 if (!cxt)
371 return;
7e786eca
KZ
372
373 DBG(CXT, ul_debugobj(cxt, "reset"));
374
2a4c734b
MB
375 free(cxt->name);
376 free(cxt->dm_name);
377 free(cxt->filename);
378 free(cxt->fstype);
379 free(cxt->uuid);
380 free(cxt->label);
4686ba17 381 free(cxt->parttype);
aa0903e0
KZ
382 free(cxt->partuuid);
383 free(cxt->partlabel);
88ca32b3 384 free(cxt->wwn);
460c7afb 385 free(cxt->serial);
2a4c734b 386
766ab9b0 387 sysfs_deinit(&cxt->sysfs);
2a4c734b
MB
388
389 memset(cxt, 0, sizeof(*cxt));
390}
391
392static int is_dm(const char *name)
393{
394 return strncmp(name, "dm-", 3) ? 0 : 1;
395}
396
397static struct dirent *xreaddir(DIR *dp)
398{
399 struct dirent *d;
400
401 assert(dp);
402
403 while ((d = readdir(dp))) {
404 if (!strcmp(d->d_name, ".") ||
405 !strcmp(d->d_name, ".."))
406 continue;
407
408 /* blacklist here? */
409 break;
410 }
411 return d;
412}
413
2a4c734b
MB
414static char *get_device_path(struct blkdev_cxt *cxt)
415{
416 char path[PATH_MAX];
417
418 assert(cxt);
419 assert(cxt->name);
420
421 if (is_dm(cxt->name))
422 return canonicalize_dm_name(cxt->name);
423
424 snprintf(path, sizeof(path), "/dev/%s", cxt->name);
92441d5c 425 sysfs_devname_sys_to_dev(path);
2a4c734b
MB
426 return xstrdup(path);
427}
428
1cd9d0d7
KZ
429static int table_parser_errcb(struct libmnt_table *tb __attribute__((__unused__)),
430 const char *filename, int line)
431{
432 if (filename)
b779c1ae 433 warnx(_("%s: parse error at line %d -- ignored"), filename, line);
1cd9d0d7
KZ
434 return 1;
435}
436
9554f7ab
KZ
437static int is_active_swap(const char *filename)
438{
439 if (!swaps) {
440 swaps = mnt_new_table();
441 if (!swaps)
442 return 0;
443 if (!mntcache)
444 mntcache = mnt_new_cache();
445
1cd9d0d7 446 mnt_table_set_parser_errcb(swaps, table_parser_errcb);
9554f7ab
KZ
447 mnt_table_set_cache(swaps, mntcache);
448 mnt_table_parse_swaps(swaps, NULL);
449 }
450
451 return mnt_table_find_srcpath(swaps, filename, MNT_ITER_BACKWARD) != 0;
452}
453
2a4c734b
MB
454static char *get_device_mountpoint(struct blkdev_cxt *cxt)
455{
9554f7ab
KZ
456 struct libmnt_fs *fs;
457 const char *fsroot;
2a4c734b 458
270e66ec
MB
459 assert(cxt);
460 assert(cxt->filename);
461
9554f7ab
KZ
462 if (!mtab) {
463 mtab = mnt_new_table();
464 if (!mtab)
465 return NULL;
466 if (!mntcache)
467 mntcache = mnt_new_cache();
2a4c734b 468
1cd9d0d7 469 mnt_table_set_parser_errcb(mtab, table_parser_errcb);
9554f7ab
KZ
470 mnt_table_set_cache(mtab, mntcache);
471 mnt_table_parse_mtab(mtab, NULL);
2a4c734b 472 }
9554f7ab 473
9e930041 474 /* Note that maj:min in /proc/self/mountinfo does not have to match with
da531dd8
KZ
475 * devno as returned by stat(), so we have to try devname too
476 */
477 fs = mnt_table_find_devno(mtab, makedev(cxt->maj, cxt->min), MNT_ITER_BACKWARD);
478 if (!fs)
479 fs = mnt_table_find_srcpath(mtab, cxt->filename, MNT_ITER_BACKWARD);
9554f7ab
KZ
480 if (!fs)
481 return is_active_swap(cxt->filename) ? xstrdup("[SWAP]") : NULL;
482
483 /* found */
484 fsroot = mnt_fs_get_root(fs);
485 if (fsroot && strcmp(fsroot, "/") != 0) {
486 /* hmm.. we found bind mount or btrfs subvolume, let's try to
487 * get real FS root mountpoint */
488 struct libmnt_fs *rfs;
489 struct libmnt_iter *itr = mnt_new_iter(MNT_ITER_BACKWARD);
490
491 mnt_table_set_iter(mtab, itr, fs);
492 while (mnt_table_next_fs(mtab, itr, &rfs) == 0) {
493 fsroot = mnt_fs_get_root(rfs);
494 if ((!fsroot || strcmp(fsroot, "/") == 0)
495 && mnt_fs_match_source(rfs, cxt->filename, mntcache)) {
496 fs = rfs;
497 break;
498 }
499 }
500 mnt_free_iter(itr);
501 }
502
7e786eca 503 DBG(DEV, ul_debugobj(cxt, "mountpoint: %s", mnt_fs_get_target(fs)));
9554f7ab 504 return xstrdup(mnt_fs_get_target(fs));
2a4c734b
MB
505}
506
f17f5f48 507#ifndef HAVE_LIBUDEV
88ca32b3 508static int get_udev_properties(struct blkdev_cxt *cxt
f17f5f48
IM
509 __attribute__((__unused__)))
510{
511 return -1;
512}
513#else
88ca32b3 514static int get_udev_properties(struct blkdev_cxt *cxt)
f17f5f48 515{
f17f5f48
IM
516 struct udev_device *dev;
517
88ca32b3
KZ
518 if (cxt->probed)
519 return 0; /* already done */
520
d44e3391
KZ
521 if (!udev)
522 udev = udev_new();
f17f5f48
IM
523 if (!udev)
524 return -1;
525
526 dev = udev_device_new_from_subsystem_sysname(udev, "block", cxt->name);
527 if (dev) {
528 const char *data;
529
88ca32b3 530 if ((data = udev_device_get_property_value(dev, "ID_FS_LABEL_ENC"))) {
f17f5f48 531 cxt->label = xstrdup(data);
d324270e
KZ
532 unhexmangle_string(cxt->label);
533 }
88ca32b3 534 if ((data = udev_device_get_property_value(dev, "ID_FS_UUID_ENC"))) {
f17f5f48 535 cxt->uuid = xstrdup(data);
88ca32b3
KZ
536 unhexmangle_string(cxt->uuid);
537 }
d324270e 538 if ((data = udev_device_get_property_value(dev, "ID_PART_ENTRY_NAME"))) {
aa0903e0 539 cxt->partlabel = xstrdup(data);
d324270e
KZ
540 unhexmangle_string(cxt->partlabel);
541 }
88ca32b3
KZ
542 if ((data = udev_device_get_property_value(dev, "ID_FS_TYPE")))
543 cxt->fstype = xstrdup(data);
4686ba17
MM
544 if ((data = udev_device_get_property_value(dev, "ID_PART_ENTRY_TYPE")))
545 cxt->parttype = xstrdup(data);
88ca32b3
KZ
546 if ((data = udev_device_get_property_value(dev, "ID_PART_ENTRY_UUID")))
547 cxt->partuuid = xstrdup(data);
5a2fd932
KZ
548 if ((data = udev_device_get_property_value(dev, "ID_PART_ENTRY_FLAGS")))
549 cxt->partflags = xstrdup(data);
4d7ef267
KZ
550
551 data = udev_device_get_property_value(dev, "ID_WWN_WITH_EXTENSION");
552 if (!data)
553 data = udev_device_get_property_value(dev, "ID_WWN");
554 if (data)
88ca32b3 555 cxt->wwn = xstrdup(data);
4d7ef267 556
460c7afb
KZ
557 if ((data = udev_device_get_property_value(dev, "ID_SERIAL_SHORT")))
558 cxt->serial = xstrdup(data);
f17f5f48 559 udev_device_unref(dev);
88ca32b3 560 cxt->probed = 1;
7e786eca 561 DBG(DEV, ul_debugobj(cxt, "%s: found udev properties", cxt->name));
f17f5f48
IM
562 }
563
88ca32b3
KZ
564 return cxt->probed == 1 ? 0 : -1;
565
f17f5f48
IM
566}
567#endif /* HAVE_LIBUDEV */
568
2a4c734b
MB
569static void probe_device(struct blkdev_cxt *cxt)
570{
2a4c734b
MB
571 blkid_probe pr = NULL;
572
573 if (cxt->probed)
574 return;
f17f5f48 575
2a4c734b
MB
576 if (!cxt->size)
577 return;
578
f17f5f48 579 /* try udev DB */
88ca32b3 580 if (get_udev_properties(cxt) == 0)
f17f5f48
IM
581 return; /* success */
582
88ca32b3
KZ
583 cxt->probed = 1;
584
585 /* try libblkid (fallback) */
586 if (getuid() != 0)
587 return; /* no permissions to read from the device */
588
2a4c734b
MB
589 pr = blkid_new_probe_from_filename(cxt->filename);
590 if (!pr)
591 return;
592
2a4c734b
MB
593 blkid_probe_enable_superblocks(pr, 1);
594 blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_LABEL |
595 BLKID_SUBLKS_UUID |
596 BLKID_SUBLKS_TYPE);
aa0903e0
KZ
597 blkid_probe_enable_partitions(pr, 1);
598 blkid_probe_set_partitions_flags(pr, BLKID_PARTS_ENTRY_DETAILS);
599
2a4c734b
MB
600 if (!blkid_do_safeprobe(pr)) {
601 const char *data = NULL;
602
603 if (!blkid_probe_lookup_value(pr, "TYPE", &data, NULL))
604 cxt->fstype = xstrdup(data);
605 if (!blkid_probe_lookup_value(pr, "UUID", &data, NULL))
606 cxt->uuid = xstrdup(data);
607 if (!blkid_probe_lookup_value(pr, "LABEL", &data, NULL))
608 cxt->label = xstrdup(data);
4686ba17
MM
609 if (!blkid_probe_lookup_value(pr, "PART_ENTRY_TYPE", &data, NULL))
610 cxt->parttype = xstrdup(data);
aa0903e0
KZ
611 if (!blkid_probe_lookup_value(pr, "PART_ENTRY_UUID", &data, NULL))
612 cxt->partuuid = xstrdup(data);
613 if (!blkid_probe_lookup_value(pr, "PART_ENTRY_NAME", &data, NULL))
614 cxt->partlabel = xstrdup(data);
5a2fd932
KZ
615 if (!blkid_probe_lookup_value(pr, "PART_ENTRY_FLAGS", &data, NULL))
616 cxt->partflags = xstrdup(data);
7e786eca 617 DBG(DEV, ul_debugobj(cxt, "%s: found blkid properties", cxt->name));
2a4c734b
MB
618 }
619
2a4c734b
MB
620 blkid_free_probe(pr);
621 return;
622}
623
624static int is_readonly_device(struct blkdev_cxt *cxt)
625{
626 int fd, ro = 0;
627
1ea962b2 628 if (sysfs_scanf(&cxt->sysfs, "ro", "%d", &ro) == 1)
2a4c734b
MB
629 return ro;
630
631 /* fallback if "ro" attribute does not exist */
632 fd = open(cxt->filename, O_RDONLY);
633 if (fd != -1) {
9c53a49c
KZ
634 if (ioctl(fd, BLKROGET, &ro) != 0)
635 ro = 0;
2a4c734b
MB
636 close(fd);
637 }
638 return ro;
639}
640
641static char *get_scheduler(struct blkdev_cxt *cxt)
642{
766ab9b0 643 char *str = sysfs_strdup(&cxt->sysfs, "queue/scheduler");
2a4c734b
MB
644 char *p, *res = NULL;
645
646 if (!str)
647 return NULL;
648 p = strchr(str, '[');
649 if (p) {
650 res = p + 1;
651 p = strchr(res, ']');
652 if (p) {
653 *p = '\0';
654 res = xstrdup(res);
655 } else
656 res = NULL;
657 }
658 free(str);
659 return res;
660}
661
18eac5ba
MB
662static char *get_type(struct blkdev_cxt *cxt)
663{
664 char *res = NULL, *p;
665
666 if (is_dm(cxt->name)) {
766ab9b0 667 char *dm_uuid = sysfs_strdup(&cxt->sysfs, "dm/uuid");
18eac5ba
MB
668
669 /* The DM_UUID prefix should be set to subsystem owning
670 * the device - LVM, CRYPT, DMRAID, MPATH, PART */
671 if (dm_uuid) {
672 char *tmp = dm_uuid;
673 char *dm_uuid_prefix = strsep(&tmp, "-");
674
675 if (dm_uuid_prefix) {
676 /* kpartx hack to remove partition number */
677 if (strncasecmp(dm_uuid_prefix, "part", 4) == 0)
678 dm_uuid_prefix[4] = '\0';
679
680 res = xstrdup(dm_uuid_prefix);
681 }
682 }
683
684 free(dm_uuid);
685 if (!res)
686 /* No UUID or no prefix - just mark it as DM device */
687 res = xstrdup("dm");
688
689 } else if (!strncmp(cxt->name, "loop", 4)) {
690 res = xstrdup("loop");
691
692 } else if (!strncmp(cxt->name, "md", 2)) {
766ab9b0 693 char *md_level = sysfs_strdup(&cxt->sysfs, "md/level");
18eac5ba
MB
694 res = md_level ? md_level : xstrdup("md");
695
696 } else {
8d687180 697 const char *type = NULL;
90e9fcda 698 int x = 0;
9d738ff8 699
8d687180
PR
700 if (!sysfs_read_int(&cxt->sysfs, "device/type", &x))
701 type = blkdev_scsi_type_to_name(x);
9d738ff8 702
9d738ff8
KZ
703 if (!type)
704 type = cxt->partition ? "part" : "disk";
705 res = xstrdup(type);
18eac5ba
MB
706 }
707
708 for (p = res; p && *p; p++)
709 *p = tolower((unsigned char) *p);
710 return res;
711}
712
a5fb4d23
MB
713/* Thanks to lsscsi code for idea of detection logic used here */
714static char *get_transport(struct blkdev_cxt *cxt)
715{
23a11c74 716 struct sysfs_cxt *sysfs = &cxt->sysfs;
2f2f7e80
KZ
717 char *attr = NULL;
718 const char *trans = NULL;
a5fb4d23 719
28ffc2b7 720 /* SCSI - Serial Peripheral Interface */
23a11c74 721 if (sysfs_scsi_host_is(sysfs, "spi"))
2f2f7e80 722 trans = "spi";
a5fb4d23
MB
723
724 /* FC/FCoE - Fibre Channel / Fibre Channel over Ethernet */
2f2f7e80 725 else if (sysfs_scsi_host_is(sysfs, "fc")) {
23a11c74
KZ
726 attr = sysfs_scsi_host_strdup_attribute(sysfs, "fc", "symbolic_name");
727 if (!attr)
a5fb4d23 728 return NULL;
2f2f7e80
KZ
729 trans = strstr(attr, " over ") ? "fcoe" : "fc";
730 free(attr);
a5fb4d23
MB
731 }
732
733 /* SAS - Serial Attached SCSI */
2f2f7e80
KZ
734 else if (sysfs_scsi_host_is(sysfs, "sas") ||
735 sysfs_scsi_has_attribute(sysfs, "sas_device"))
736 trans = "sas";
a5fb4d23 737
a5fb4d23
MB
738
739 /* SBP - Serial Bus Protocol (FireWire) */
2f2f7e80
KZ
740 else if (sysfs_scsi_has_attribute(sysfs, "ieee1394_id"))
741 trans = "sbp";
a5fb4d23
MB
742
743 /* iSCSI */
2f2f7e80
KZ
744 else if (sysfs_scsi_host_is(sysfs, "iscsi"))
745 trans ="iscsi";
a5fb4d23
MB
746
747 /* USB - Universal Serial Bus */
2f2f7e80
KZ
748 else if (sysfs_scsi_path_contains(sysfs, "usb"))
749 trans = "usb";
a5fb4d23
MB
750
751 /* ATA, SATA */
2f2f7e80 752 else if (sysfs_scsi_host_is(sysfs, "scsi")) {
23a11c74
KZ
753 attr = sysfs_scsi_host_strdup_attribute(sysfs, "scsi", "proc_name");
754 if (!attr)
a5fb4d23 755 return NULL;
2f2f7e80
KZ
756 if (!strncmp(attr, "ahci", 4) || !strncmp(attr, "sata", 4))
757 trans = "sata";
758 else if (strstr(attr, "ata"))
759 trans = "ata";
760 free(attr);
3d305170
KZ
761
762 } else if (strncmp(cxt->name, "nvme", 4) == 0)
763 trans = "nvme";
a5fb4d23 764
2f2f7e80 765 return trans ? xstrdup(trans) : NULL;
a5fb4d23
MB
766}
767
7f14ee1b
KZ
768static char *get_subsystems(struct blkdev_cxt *cxt)
769{
770 char path[PATH_MAX];
771 char *sub, *chain, *res = NULL;
772 size_t len = 0, last = 0;
773
774 chain = sysfs_get_devchain(&cxt->sysfs, path, sizeof(path));
775 if (!chain)
776 return NULL;
777
778 while (sysfs_next_subsystem(&cxt->sysfs, chain, &sub) == 0) {
779 size_t sz;
780
781 /* don't create "block:scsi:scsi", but "block:scsi" */
56e78cb5
AH
782 if (len && strcmp(res + last, sub) == 0) {
783 free(sub);
7f14ee1b 784 continue;
56e78cb5 785 }
7f14ee1b
KZ
786
787 sz = strlen(sub);
788 res = xrealloc(res, len + sz + 2);
789 if (len)
790 res[len++] = ':';
791
792 memcpy(res + len, sub, sz + 1);
793 last = len;
794 len += sz;
795 free(sub);
796 }
797
798 return res;
799}
800
801
9bd4e5c0 802#define is_parsable(_l) (scols_table_is_raw((_l)->table) || \
4a102a48
KZ
803 scols_table_is_export((_l)->table) || \
804 scols_table_is_json((_l)->table))
7c9c872c 805
c7e76cd1
KZ
806static char *mk_name(const char *name)
807{
808 char *p;
809 if (!name)
810 return NULL;
811 if (lsblk->paths)
812 xasprintf(&p, "/dev/%s", name);
813 else
814 p = xstrdup(name);
92441d5c
KZ
815 if (p)
816 sysfs_devname_sys_to_dev(p);
c7e76cd1
KZ
817 return p;
818}
819
820static char *mk_dm_name(const char *name)
821{
822 char *p;
823 if (!name)
824 return NULL;
825 if (lsblk->paths)
826 xasprintf(&p, "/dev/mapper/%s", name);
827 else
828 p = xstrdup(name);
829 return p;
830}
831
642048e4
KZ
832/* stores data to scols cell userdata (invisible and independent on output)
833 * to make the original values accessible for sort functions
834 */
835static void set_sortdata_u64(struct libscols_line *ln, int col, uint64_t x)
836{
837 struct libscols_cell *ce = scols_line_get_cell(ln, col);
838 uint64_t *data;
839
840 if (!ce)
841 return;
842 data = xmalloc(sizeof(uint64_t));
843 *data = x;
844 scols_cell_set_userdata(ce, data);
845}
846
847static void set_sortdata_u64_from_string(struct libscols_line *ln, int col, const char *str)
848{
849 uint64_t x;
850
851 if (!str || sscanf(str, "%"SCNu64, &x) != 1)
852 return;
853
854 set_sortdata_u64(ln, col, x);
855}
856
857static void unref_sortdata(struct libscols_table *tb)
858{
859 struct libscols_iter *itr;
860 struct libscols_line *ln;
861
862 if (!tb || !lsblk->sort_col)
863 return;
864 itr = scols_new_iter(SCOLS_ITER_FORWARD);
865 if (!itr)
866 return;
867 while (scols_table_next_line(tb, itr, &ln) == 0) {
868 struct libscols_cell *ce = scols_line_get_column_cell(ln,
869 lsblk->sort_col);
870 void *data = scols_cell_get_userdata(ce);
871 free(data);
872 }
873
874 scols_free_iter(itr);
875}
876
9bd4e5c0 877static void set_scols_data(struct blkdev_cxt *cxt, int col, int id, struct libscols_line *ln)
2a4c734b 878{
642048e4 879 int sort = 0, st_rc = 0;
bb6e822a 880 char *str = NULL;
2a4c734b
MB
881
882 if (!cxt->st.st_rdev && (id == COL_OWNER || id == COL_GROUP ||
883 id == COL_MODE))
1dc42eb6 884 st_rc = stat(cxt->filename, &cxt->st);
2a4c734b 885
642048e4
KZ
886 if (lsblk->sort_id == id)
887 sort = 1;
888
2a4c734b
MB
889 switch(id) {
890 case COL_NAME:
bb6e822a 891 str = cxt->dm_name ? mk_dm_name(cxt->dm_name) : mk_name(cxt->name);
c7e76cd1 892 break;
2a4c734b 893 case COL_KNAME:
bb6e822a 894 str = mk_name(cxt->name);
2a4c734b 895 break;
310c0603
MB
896 case COL_PKNAME:
897 if (cxt->parent)
bb6e822a 898 str = mk_name(cxt->parent->name);
310c0603 899 break;
2a4c734b
MB
900 case COL_OWNER:
901 {
1dc42eb6 902 struct passwd *pw = st_rc ? NULL : getpwuid(cxt->st.st_uid);
2a4c734b 903 if (pw)
bb6e822a 904 str = xstrdup(pw->pw_name);
2a4c734b
MB
905 break;
906 }
907 case COL_GROUP:
908 {
1dc42eb6 909 struct group *gr = st_rc ? NULL : getgrgid(cxt->st.st_gid);
2a4c734b 910 if (gr)
bb6e822a 911 str = xstrdup(gr->gr_name);
2a4c734b
MB
912 break;
913 }
914 case COL_MODE:
915 {
916 char md[11];
1dc42eb6
KZ
917
918 if (!st_rc) {
b0b24b11 919 xstrmode(cxt->st.st_mode, md);
bb6e822a 920 str = xstrdup(md);
1dc42eb6 921 }
2a4c734b
MB
922 break;
923 }
924 case COL_MAJMIN:
7c9c872c 925 if (is_parsable(lsblk))
bb6e822a 926 xasprintf(&str, "%u:%u", cxt->maj, cxt->min);
2a4c734b 927 else
bb6e822a 928 xasprintf(&str, "%3u:%-3u", cxt->maj, cxt->min);
642048e4
KZ
929 if (sort)
930 set_sortdata_u64(ln, col, makedev(cxt->maj, cxt->min));
2a4c734b
MB
931 break;
932 case COL_FSTYPE:
933 probe_device(cxt);
934 if (cxt->fstype)
bb6e822a 935 str = xstrdup(cxt->fstype);
2a4c734b
MB
936 break;
937 case COL_TARGET:
c49ff158 938 str = get_device_mountpoint(cxt);
2a4c734b
MB
939 break;
940 case COL_LABEL:
941 probe_device(cxt);
bb6e822a
KZ
942 if (cxt->label)
943 str = xstrdup(cxt->label);
2a4c734b
MB
944 break;
945 case COL_UUID:
946 probe_device(cxt);
947 if (cxt->uuid)
bb6e822a 948 str = xstrdup(cxt->uuid);
2a4c734b 949 break;
4686ba17
MM
950 case COL_PARTTYPE:
951 probe_device(cxt);
952 if (cxt->parttype)
bb6e822a 953 str = xstrdup(cxt->parttype);
4686ba17 954 break;
aa0903e0
KZ
955 case COL_PARTLABEL:
956 probe_device(cxt);
bb6e822a
KZ
957 if (cxt->partlabel)
958 str = xstrdup(cxt->partlabel);
aa0903e0
KZ
959 break;
960 case COL_PARTUUID:
961 probe_device(cxt);
742b877b 962 if (cxt->partuuid)
bb6e822a 963 str = xstrdup(cxt->partuuid);
aa0903e0 964 break;
5a2fd932
KZ
965 case COL_PARTFLAGS:
966 probe_device(cxt);
967 if (cxt->partflags)
bb6e822a 968 str = xstrdup(cxt->partflags);
5a2fd932 969 break;
88ca32b3
KZ
970 case COL_WWN:
971 get_udev_properties(cxt);
972 if (cxt->wwn)
bb6e822a 973 str = xstrdup(cxt->wwn);
88ca32b3 974 break;
150db7a9 975 case COL_RA:
bb6e822a 976 str = sysfs_strdup(&cxt->sysfs, "queue/read_ahead_kb");
642048e4
KZ
977 if (sort)
978 set_sortdata_u64_from_string(ln, col, str);
150db7a9 979 break;
2a4c734b 980 case COL_RO:
bb6e822a 981 str = xstrdup(is_readonly_device(cxt) ? "1" : "0");
2a4c734b 982 break;
627970a7 983 case COL_RM:
bb6e822a
KZ
984 str = sysfs_strdup(&cxt->sysfs, "removable");
985 if (!str && cxt->sysfs.parent)
986 str = sysfs_strdup(cxt->sysfs.parent, "removable");
2a4c734b 987 break;
483987c2
KZ
988 case COL_HOTPLUG:
989 str = sysfs_is_hotpluggable(&cxt->sysfs) ? xstrdup("1") : xstrdup("0");
990 break;
2a4c734b 991 case COL_ROTA:
bb6e822a 992 str = sysfs_strdup(&cxt->sysfs, "queue/rotational");
2a4c734b 993 break;
12b06c3d 994 case COL_RAND:
bb6e822a 995 str = sysfs_strdup(&cxt->sysfs, "queue/add_random");
12b06c3d 996 break;
2a4c734b 997 case COL_MODEL:
bb6e822a
KZ
998 if (!cxt->partition && cxt->nslaves == 0)
999 str = sysfs_strdup(&cxt->sysfs, "device/model");
d6681cee 1000 break;
460c7afb
KZ
1001 case COL_SERIAL:
1002 if (!cxt->partition && cxt->nslaves == 0) {
1003 get_udev_properties(cxt);
1004 if (cxt->serial)
bb6e822a 1005 str = xstrdup(cxt->serial);
3d305170
KZ
1006 else
1007 str = sysfs_strdup(&cxt->sysfs, "device/serial");
460c7afb
KZ
1008 }
1009 break;
d6681cee 1010 case COL_REV:
bb6e822a
KZ
1011 if (!cxt->partition && cxt->nslaves == 0)
1012 str = sysfs_strdup(&cxt->sysfs, "device/rev");
99f43b72
MB
1013 break;
1014 case COL_VENDOR:
bb6e822a
KZ
1015 if (!cxt->partition && cxt->nslaves == 0)
1016 str = sysfs_strdup(&cxt->sysfs, "device/vendor");
2a4c734b
MB
1017 break;
1018 case COL_SIZE:
bb6e822a
KZ
1019 if (!cxt->size)
1020 break;
1021 if (lsblk->bytes)
95fb0ead 1022 xasprintf(&str, "%ju", cxt->size);
bb6e822a
KZ
1023 else
1024 str = size_to_human_string(SIZE_SUFFIX_1LETTER, cxt->size);
642048e4
KZ
1025 if (sort)
1026 set_sortdata_u64(ln, col, cxt->size);
2a4c734b 1027 break;
01e487c0 1028 case COL_STATE:
bb6e822a
KZ
1029 if (!cxt->partition && !cxt->dm_name)
1030 str = sysfs_strdup(&cxt->sysfs, "device/state");
1031 else if (cxt->dm_name) {
01e487c0
MB
1032 int x = 0;
1033 if (sysfs_read_int(&cxt->sysfs, "dm/suspended", &x) == 0)
bb6e822a 1034 str = xstrdup(x ? "suspended" : "running");
01e487c0 1035 }
01e487c0 1036 break;
2a4c734b 1037 case COL_ALIOFF:
bb6e822a 1038 str = sysfs_strdup(&cxt->sysfs, "alignment_offset");
642048e4
KZ
1039 if (sort)
1040 set_sortdata_u64_from_string(ln, col, str);
2a4c734b
MB
1041 break;
1042 case COL_MINIO:
bb6e822a 1043 str = sysfs_strdup(&cxt->sysfs, "queue/minimum_io_size");
642048e4
KZ
1044 if (sort)
1045 set_sortdata_u64_from_string(ln, col, str);
2a4c734b
MB
1046 break;
1047 case COL_OPTIO:
bb6e822a 1048 str = sysfs_strdup(&cxt->sysfs, "queue/optimal_io_size");
642048e4
KZ
1049 if (sort)
1050 set_sortdata_u64_from_string(ln, col, str);
2a4c734b
MB
1051 break;
1052 case COL_PHYSEC:
bb6e822a 1053 str = sysfs_strdup(&cxt->sysfs, "queue/physical_block_size");
642048e4
KZ
1054 if (sort)
1055 set_sortdata_u64_from_string(ln, col, str);
2a4c734b
MB
1056 break;
1057 case COL_LOGSEC:
bb6e822a 1058 str = sysfs_strdup(&cxt->sysfs, "queue/logical_block_size");
642048e4
KZ
1059 if (sort)
1060 set_sortdata_u64_from_string(ln, col, str);
2a4c734b
MB
1061 break;
1062 case COL_SCHED:
bb6e822a 1063 str = get_scheduler(cxt);
2a4c734b 1064 break;
64c9e22a 1065 case COL_RQ_SIZE:
bb6e822a 1066 str = sysfs_strdup(&cxt->sysfs, "queue/nr_requests");
642048e4
KZ
1067 if (sort)
1068 set_sortdata_u64_from_string(ln, col, str);
64c9e22a 1069 break;
18eac5ba 1070 case COL_TYPE:
bb6e822a 1071 str = get_type(cxt);
18eac5ba 1072 break;
699e5c4f
MB
1073 case COL_HCTL:
1074 {
1075 int h, c, t, l;
bb6e822a
KZ
1076 if (sysfs_scsi_get_hctl(&cxt->sysfs, &h, &c, &t, &l) == 0)
1077 xasprintf(&str, "%d:%d:%d:%d", h, c, t, l);
699e5c4f
MB
1078 break;
1079 }
a5fb4d23 1080 case COL_TRANSPORT:
bb6e822a 1081 str = get_transport(cxt);
a5fb4d23 1082 break;
7f14ee1b
KZ
1083 case COL_SUBSYS:
1084 str = get_subsystems(cxt);
1085 break;
2d232246 1086 case COL_DALIGN:
bb6e822a
KZ
1087 if (cxt->discard)
1088 str = sysfs_strdup(&cxt->sysfs, "discard_alignment");
1089 if (!str)
1090 str = xstrdup("0");
642048e4
KZ
1091 if (sort)
1092 set_sortdata_u64_from_string(ln, col, str);
2d232246
MP
1093 break;
1094 case COL_DGRAN:
642048e4 1095 if (lsblk->bytes) {
bb6e822a 1096 str = sysfs_strdup(&cxt->sysfs, "queue/discard_granularity");
642048e4
KZ
1097 if (sort)
1098 set_sortdata_u64_from_string(ln, col, str);
1099 } else {
9cfe1b9c 1100 uint64_t x;
9cfe1b9c 1101 if (sysfs_read_u64(&cxt->sysfs,
642048e4 1102 "queue/discard_granularity", &x) == 0) {
bb6e822a 1103 str = size_to_human_string(SIZE_SUFFIX_1LETTER, x);
642048e4
KZ
1104 if (sort)
1105 set_sortdata_u64(ln, col, x);
1106 }
9cfe1b9c 1107 }
2d232246
MP
1108 break;
1109 case COL_DMAX:
642048e4 1110 if (lsblk->bytes) {
bb6e822a 1111 str = sysfs_strdup(&cxt->sysfs, "queue/discard_max_bytes");
642048e4
KZ
1112 if (sort)
1113 set_sortdata_u64_from_string(ln, col, str);
1114 } else {
9cfe1b9c 1115 uint64_t x;
9cfe1b9c 1116 if (sysfs_read_u64(&cxt->sysfs,
642048e4 1117 "queue/discard_max_bytes", &x) == 0) {
bb6e822a 1118 str = size_to_human_string(SIZE_SUFFIX_1LETTER, x);
642048e4
KZ
1119 if (sort)
1120 set_sortdata_u64(ln, col, x);
1121 }
9cfe1b9c 1122 }
2d232246
MP
1123 break;
1124 case COL_DZERO:
bb6e822a
KZ
1125 if (cxt->discard)
1126 str = sysfs_strdup(&cxt->sysfs, "queue/discard_zeroes_data");
1127 if (!str)
1128 str = xstrdup("0");
2d232246 1129 break;
2adb1a44 1130 case COL_WSAME:
642048e4 1131 if (lsblk->bytes) {
bb6e822a 1132 str = sysfs_strdup(&cxt->sysfs, "queue/write_same_max_bytes");
642048e4
KZ
1133 if (sort)
1134 set_sortdata_u64_from_string(ln, col, str);
1135 } else {
2adb1a44
MB
1136 uint64_t x;
1137
1138 if (sysfs_read_u64(&cxt->sysfs,
642048e4 1139 "queue/write_same_max_bytes", &x) == 0) {
bb6e822a 1140 str = size_to_human_string(SIZE_SUFFIX_1LETTER, x);
642048e4
KZ
1141 if (sort)
1142 set_sortdata_u64(ln, col, x);
1143 }
2adb1a44 1144 }
bb6e822a
KZ
1145 if (!str)
1146 str = xstrdup("0");
2adb1a44 1147 break;
2a4c734b 1148 };
bb6e822a
KZ
1149
1150 if (str)
1151 scols_line_refer_data(ln, col, str);
2a4c734b
MB
1152}
1153
642048e4 1154static void fill_table_line(struct blkdev_cxt *cxt, struct libscols_line *scols_parent)
2a4c734b 1155{
40b17508 1156 size_t i;
2a4c734b 1157
9bd4e5c0
OO
1158 cxt->scols_line = scols_table_new_line(lsblk->table, scols_parent);
1159 if (!cxt->scols_line)
1160 return;
2a4c734b
MB
1161
1162 for (i = 0; i < ncolumns; i++)
9bd4e5c0 1163 set_scols_data(cxt, i, get_column_id(i), cxt->scols_line);
2a4c734b
MB
1164}
1165
1166static int set_cxt(struct blkdev_cxt *cxt,
1167 struct blkdev_cxt *parent,
09a71aa1
PR
1168 struct blkdev_cxt *wholedisk,
1169 const char *name)
2a4c734b 1170{
766ab9b0 1171 dev_t devno;
2a4c734b 1172
7e786eca
KZ
1173 DBG(CXT, ul_debugobj(cxt, "setting context for %s [parent=%p, wholedisk=%p]",
1174 name, parent, wholedisk));
1175
2a4c734b
MB
1176 cxt->parent = parent;
1177 cxt->name = xstrdup(name);
09a71aa1 1178 cxt->partition = wholedisk != NULL;
2a4c734b
MB
1179
1180 cxt->filename = get_device_path(cxt);
270e66ec 1181 if (!cxt->filename) {
63107d1b 1182 DBG(CXT, ul_debugobj(cxt, "%s: failed to get device path", cxt->name));
270e66ec
MB
1183 return -1;
1184 }
7e786eca 1185 DBG(CXT, ul_debugobj(cxt, "%s: filename=%s", cxt->name, cxt->filename));
2a4c734b 1186
6c62abc4 1187 devno = sysfs_devname_to_devno(cxt->name, wholedisk ? wholedisk->name : NULL);
09a71aa1 1188
270e66ec 1189 if (!devno) {
63107d1b 1190 DBG(CXT, ul_debugobj(cxt, "%s: unknown device name", cxt->name));
270e66ec
MB
1191 return -1;
1192 }
2a4c734b 1193
09a71aa1
PR
1194 if (lsblk->inverse) {
1195 if (sysfs_init(&cxt->sysfs, devno, wholedisk ? &wholedisk->sysfs : NULL)) {
63107d1b 1196 DBG(CXT, ul_debugobj(cxt, "%s: failed to initialize sysfs handler", cxt->name));
09a71aa1
PR
1197 return -1;
1198 }
1199 if (parent)
1200 parent->sysfs.parent = &cxt->sysfs;
1201 } else {
1202 if (sysfs_init(&cxt->sysfs, devno, parent ? &parent->sysfs : NULL)) {
63107d1b 1203 DBG(CXT, ul_debugobj(cxt, "%s: failed to initialize sysfs handler", cxt->name));
09a71aa1
PR
1204 return -1;
1205 }
270e66ec 1206 }
766ab9b0
KZ
1207
1208 cxt->maj = major(devno);
1209 cxt->min = minor(devno);
32c67d2c 1210 cxt->size = 0;
2a4c734b 1211
32c67d2c
KZ
1212 if (sysfs_read_u64(&cxt->sysfs, "size", &cxt->size) == 0) /* in sectors */
1213 cxt->size <<= 9; /* in bytes */
90e9fcda 1214
65060bd0
KZ
1215 if (sysfs_read_int(&cxt->sysfs,
1216 "queue/discard_granularity", &cxt->discard) != 0)
1217 cxt->discard = 0;
2a4c734b
MB
1218
1219 /* Ignore devices of zero size */
7e786eca
KZ
1220 if (!lsblk->all_devices && cxt->size == 0) {
1221 DBG(CXT, ul_debugobj(cxt, "zero size device -- ignore"));
2a4c734b 1222 return -1;
7e786eca 1223 }
6c62abc4 1224 if (is_dm(cxt->name)) {
766ab9b0 1225 cxt->dm_name = sysfs_strdup(&cxt->sysfs, "dm/name");
270e66ec 1226 if (!cxt->dm_name) {
63107d1b 1227 DBG(CXT, ul_debugobj(cxt, "%s: failed to get dm name", cxt->name));
270e66ec
MB
1228 return -1;
1229 }
766ab9b0 1230 }
2a4c734b 1231
92441d5c 1232 cxt->npartitions = sysfs_count_partitions(&cxt->sysfs, cxt->name);
09a71aa1 1233 cxt->nholders = sysfs_count_dirents(&cxt->sysfs, "holders");
766ab9b0 1234 cxt->nslaves = sysfs_count_dirents(&cxt->sysfs, "slaves");
2a4c734b 1235
7e786eca
KZ
1236 DBG(CXT, ul_debugobj(cxt, "%s: npartitions=%d, nholders=%d, nslaves=%d",
1237 cxt->name, cxt->npartitions, cxt->nholders, cxt->nslaves));
1238
28ffc2b7 1239 /* ignore non-SCSI devices */
7e786eca
KZ
1240 if (lsblk->scsi && sysfs_scsi_get_hctl(&cxt->sysfs, NULL, NULL, NULL, NULL)) {
1241 DBG(CXT, ul_debugobj(cxt, "non-scsi device -- ignore"));
28ffc2b7 1242 return -1;
7e786eca 1243 }
28ffc2b7 1244
7e786eca 1245 DBG(CXT, ul_debugobj(cxt, "%s: context successfully initialized", cxt->name));
2a4c734b
MB
1246 return 0;
1247}
1248
09a71aa1
PR
1249static int process_blkdev(struct blkdev_cxt *cxt, struct blkdev_cxt *parent,
1250 int do_partitions, const char *part_name);
1251
2a4c734b 1252/*
09a71aa1 1253 * List device partitions if any.
2a4c734b 1254 */
09a71aa1
PR
1255static int list_partitions(struct blkdev_cxt *wholedisk_cxt, struct blkdev_cxt *parent_cxt,
1256 const char *part_name)
2a4c734b
MB
1257{
1258 DIR *dir;
1259 struct dirent *d;
7fe033ef 1260 struct blkdev_cxt part_cxt = { 0 };
09a71aa1 1261 int r = -1;
2a4c734b 1262
09a71aa1 1263 assert(wholedisk_cxt);
f31505fe 1264
09a71aa1
PR
1265 /*
1266 * Do not process further if there are no partitions for
1267 * this device or the device itself is a partition.
1268 */
1269 if (!wholedisk_cxt->npartitions || wholedisk_cxt->partition)
1270 return -1;
2a4c734b 1271
7e786eca
KZ
1272 DBG(CXT, ul_debugobj(wholedisk_cxt, "probe whole-disk for partitions"));
1273
09a71aa1 1274 dir = sysfs_opendir(&wholedisk_cxt->sysfs, NULL);
2a4c734b
MB
1275 if (!dir)
1276 err(EXIT_FAILURE, _("failed to open device directory in sysfs"));
1277
1278 while ((d = xreaddir(dir))) {
09a71aa1
PR
1279 /* Process particular partition only? */
1280 if (part_name && strcmp(part_name, d->d_name))
2a4c734b
MB
1281 continue;
1282
09a71aa1 1283 if (!(sysfs_is_partition_dirent(dir, d, wholedisk_cxt->name)))
270e66ec 1284 continue;
09a71aa1 1285
7e786eca
KZ
1286 DBG(CXT, ul_debugobj(wholedisk_cxt, " checking %s", d->d_name));
1287
09a71aa1
PR
1288 if (lsblk->inverse) {
1289 /*
1290 * <parent_cxt>
1291 * `-<part_cxt>
1292 * `-<wholedisk_cxt>
1293 * `-...
1294 */
1295 if (set_cxt(&part_cxt, parent_cxt, wholedisk_cxt, d->d_name))
1296 goto next;
1297
1298 if (!parent_cxt && part_cxt.nholders)
1299 goto next;
1300
1301 wholedisk_cxt->parent = &part_cxt;
642048e4 1302 fill_table_line(&part_cxt, parent_cxt ? parent_cxt->scols_line : NULL);
09a71aa1
PR
1303 if (!lsblk->nodeps)
1304 process_blkdev(wholedisk_cxt, &part_cxt, 0, NULL);
1305 } else {
1306 /*
1307 * <parent_cxt>
1308 * `-<wholedisk_cxt>
1309 * `-<part_cxt>
1310 * `-...
1311 */
a0e3e3de
KZ
1312 int ps = set_cxt(&part_cxt, wholedisk_cxt, wholedisk_cxt, d->d_name);
1313
09a71aa1
PR
1314 /* Print whole disk only once */
1315 if (r)
642048e4 1316 fill_table_line(wholedisk_cxt, parent_cxt ? parent_cxt->scols_line : NULL);
a0e3e3de 1317 if (ps == 0 && !lsblk->nodeps)
09a71aa1 1318 process_blkdev(&part_cxt, wholedisk_cxt, 0, NULL);
270e66ec 1319 }
09a71aa1
PR
1320 next:
1321 reset_blkdev_cxt(&part_cxt);
1322 r = 0;
2a4c734b 1323 }
09a71aa1 1324
7e786eca 1325 DBG(CXT, ul_debugobj(wholedisk_cxt, "probe whole-disk for partitions -- done"));
2a4c734b 1326 closedir(dir);
09a71aa1
PR
1327 return r;
1328}
1329
2208b3cc 1330static int get_wholedisk_from_partition_dirent(DIR *dir,
540d506f 1331 struct dirent *d, struct blkdev_cxt *cxt)
09a71aa1
PR
1332{
1333 char path[PATH_MAX];
1334 char *p;
1335 int len;
1336
2208b3cc 1337 if ((len = readlinkat(dirfd(dir), d->d_name, path, sizeof(path) - 1)) < 0)
09a71aa1 1338 return 0;
540d506f
KZ
1339
1340 path[len] = '\0';
09a71aa1
PR
1341
1342 /* The path ends with ".../<device>/<partition>" */
540d506f
KZ
1343 p = strrchr(path, '/');
1344 if (!p)
1345 return 0;
1346 *p = '\0';
1347
1348 p = strrchr(path, '/');
1349 if (!p)
1350 return 0;
1351 p++;
2a4c734b 1352
09a71aa1
PR
1353 return set_cxt(cxt, NULL, NULL, p);
1354}
1355
1356/*
1357 * List device dependencies: partitions, holders (inverse = 0) or slaves (inverse = 1).
1358 */
1359static int list_deps(struct blkdev_cxt *cxt)
1360{
1361 DIR *dir;
1362 struct dirent *d;
7fe033ef 1363 struct blkdev_cxt dep = { 0 };
540d506f 1364 const char *depname;
09a71aa1
PR
1365
1366 assert(cxt);
1367
1368 if (lsblk->nodeps)
1369 return 0;
1370
7e786eca
KZ
1371 DBG(CXT, ul_debugobj(cxt, "%s: list dependencies", cxt->name));
1372
09a71aa1
PR
1373 if (!(lsblk->inverse ? cxt->nslaves : cxt->nholders))
1374 return 0;
1375
540d506f
KZ
1376 depname = lsblk->inverse ? "slaves" : "holders";
1377 dir = sysfs_opendir(&cxt->sysfs, depname);
2a4c734b
MB
1378 if (!dir)
1379 return 0;
1380
7e786eca
KZ
1381 DBG(CXT, ul_debugobj(cxt, "%s: checking for '%s' dependence", cxt->name, depname));
1382
2a4c734b 1383 while ((d = xreaddir(dir))) {
09a71aa1
PR
1384 /* Is the dependency a partition? */
1385 if (sysfs_is_partition_dirent(dir, d, NULL)) {
2208b3cc 1386 if (!get_wholedisk_from_partition_dirent(dir, d, &dep)) {
7e786eca
KZ
1387 DBG(CXT, ul_debugobj(cxt, "%s: %s: dependence is partition",
1388 cxt->name, d->d_name));
1389 process_blkdev(&dep, cxt, 1, d->d_name);
1390 }
270e66ec 1391 }
09a71aa1 1392 /* The dependency is a whole device. */
7e786eca
KZ
1393 else if (!set_cxt(&dep, cxt, NULL, d->d_name)) {
1394 DBG(CXT, ul_debugobj(cxt, "%s: %s: dependence is whole-disk",
1395 cxt->name, d->d_name));
92abdcae 1396 /* For inverse tree we don't want to show partitions
9e930041 1397 * if the dependence is on whole-disk */
92abdcae 1398 process_blkdev(&dep, cxt, lsblk->inverse ? 0 : 1, NULL);
7e786eca 1399 }
09a71aa1 1400 reset_blkdev_cxt(&dep);
2a4c734b
MB
1401 }
1402 closedir(dir);
1403
7e786eca 1404 DBG(CXT, ul_debugobj(cxt, "%s: checking for '%s' -- done", cxt->name, depname));
2a4c734b
MB
1405 return 0;
1406}
1407
09a71aa1
PR
1408static int process_blkdev(struct blkdev_cxt *cxt, struct blkdev_cxt *parent,
1409 int do_partitions, const char *part_name)
1410{
1411 if (do_partitions && cxt->npartitions)
9e930041 1412 list_partitions(cxt, parent, part_name); /* partitions + whole-disk */
296dc15c
KZ
1413 else
1414 fill_table_line(cxt, parent ? parent->scols_line : NULL); /* whole-disk only */
09a71aa1 1415
09a71aa1
PR
1416 return list_deps(cxt);
1417}
1418
1419/* Iterate devices in sysfs */
2a4c734b
MB
1420static int iterate_block_devices(void)
1421{
1422 DIR *dir;
1423 struct dirent *d;
7fe033ef 1424 struct blkdev_cxt cxt = { 0 };
2a4c734b
MB
1425
1426 if (!(dir = opendir(_PATH_SYS_BLOCK)))
1539750f 1427 return -errno;
2a4c734b 1428
7e786eca
KZ
1429 DBG(DEV, ul_debug("iterate on " _PATH_SYS_BLOCK));
1430
2a4c734b 1431 while ((d = xreaddir(dir))) {
7e786eca
KZ
1432
1433 DBG(DEV, ul_debug(" %s dentry", d->d_name));
1434
09a71aa1 1435 if (set_cxt(&cxt, NULL, NULL, d->d_name))
2a4c734b
MB
1436 goto next;
1437
2ef4e8ba 1438 if (is_maj_excluded(cxt.maj) || !is_maj_included(cxt.maj))
2a4c734b
MB
1439 goto next;
1440
09a71aa1
PR
1441 /* Skip devices in the middle of dependency tree. */
1442 if ((lsblk->inverse ? cxt.nholders : cxt.nslaves) > 0)
2a4c734b
MB
1443 goto next;
1444
09a71aa1 1445 process_blkdev(&cxt, NULL, 1, NULL);
2a4c734b
MB
1446 next:
1447 reset_blkdev_cxt(&cxt);
1448 }
1449
1450 closedir(dir);
1451
7e786eca 1452 DBG(DEV, ul_debug("iterate on " _PATH_SYS_BLOCK " -- done"));
1539750f 1453 return 0;
2a4c734b
MB
1454}
1455
da30cb2a
PR
1456static char *devno_to_sysfs_name(dev_t devno, char *devname, char *buf, size_t buf_size)
1457{
1458 char path[PATH_MAX];
1459 ssize_t len;
1460
1461 if (!sysfs_devno_path(devno, path, sizeof(path))) {
1462 warn(_("%s: failed to compose sysfs path"), devname);
1463 return NULL;
1464 }
1465
9f51089e 1466 len = readlink(path, buf, buf_size - 1);
da30cb2a
PR
1467 if (len < 0) {
1468 warn(_("%s: failed to read link"), path);
1469 return NULL;
1470 }
1471 buf[len] = '\0';
1472
1473 return xstrdup(strrchr(buf, '/') + 1);
1474}
1475
2a4c734b
MB
1476static int process_one_device(char *devname)
1477{
7fe033ef 1478 struct blkdev_cxt parent = { 0 }, cxt = { 0 };
2a4c734b 1479 struct stat st;
1539750f 1480 char buf[PATH_MAX + 1], *name = NULL, *diskname = NULL;
2a4c734b 1481 dev_t disk = 0;
1539750f 1482 int real_part = 0, rc = -EINVAL;
2a4c734b
MB
1483
1484 if (stat(devname, &st) || !S_ISBLK(st.st_mode)) {
1485 warnx(_("%s: not a block device"), devname);
1539750f 1486 goto leave;
2a4c734b 1487 }
da30cb2a
PR
1488
1489 if (!(name = devno_to_sysfs_name(st.st_rdev, devname, buf, PATH_MAX))) {
1490 warn(_("%s: failed to get sysfs name"), devname);
1539750f 1491 goto leave;
2a4c734b 1492 }
da30cb2a
PR
1493
1494 if (!strncmp(name, "dm-", 3)) {
1495 /* dm mapping is never a real partition! */
1496 real_part = 0;
1497 } else {
1498 if (blkid_devno_to_wholedisk(st.st_rdev, buf, sizeof(buf), &disk)) {
1499 warn(_("%s: failed to get whole-disk device number"), devname);
1539750f 1500 goto leave;
da30cb2a
PR
1501 }
1502 diskname = buf;
1503 real_part = st.st_rdev != disk;
1504 }
1505
1506 if (!real_part) {
2a4c734b 1507 /*
09a71aa1 1508 * Device is not a partition.
2a4c734b 1509 */
da30cb2a 1510 if (set_cxt(&cxt, NULL, NULL, name))
270e66ec 1511 goto leave;
09a71aa1 1512 process_blkdev(&cxt, NULL, !lsblk->inverse, NULL);
270e66ec 1513 } else {
2a4c734b 1514 /*
09a71aa1 1515 * Partition, read sysfs name of the device.
2a4c734b 1516 */
09a71aa1 1517 if (set_cxt(&parent, NULL, NULL, diskname))
270e66ec 1518 goto leave;
09a71aa1 1519 if (set_cxt(&cxt, &parent, &parent, name))
270e66ec 1520 goto leave;
09a71aa1
PR
1521
1522 if (lsblk->inverse)
1523 process_blkdev(&parent, &cxt, 1, cxt.name);
1524 else
1525 process_blkdev(&cxt, &parent, 1, NULL);
2a4c734b
MB
1526 }
1527
1539750f 1528 rc = 0;
270e66ec 1529leave:
da30cb2a 1530 free(name);
2a4c734b
MB
1531 reset_blkdev_cxt(&cxt);
1532
da30cb2a 1533 if (real_part)
2a4c734b
MB
1534 reset_blkdev_cxt(&parent);
1535
1539750f 1536 return rc;
2a4c734b
MB
1537}
1538
2dc03af7 1539static void parse_excludes(const char *str0)
2a4c734b 1540{
2dc03af7
KZ
1541 const char *str = str0;
1542
2a4c734b
MB
1543 while (str && *str) {
1544 char *end = NULL;
ed34643c 1545 unsigned long n;
2a4c734b
MB
1546
1547 errno = 0;
1548 n = strtoul(str, &end, 10);
1549
2dc03af7
KZ
1550 if (end == str || (end && *end && *end != ','))
1551 errx(EXIT_FAILURE, _("failed to parse list '%s'"), str0);
1552 if (errno != 0 && (n == ULONG_MAX || n == 0))
1553 err(EXIT_FAILURE, _("failed to parse list '%s'"), str0);
2a4c734b
MB
1554 excludes[nexcludes++] = n;
1555
1556 if (nexcludes == ARRAY_SIZE(excludes))
f14f02f5 1557 /* TRANSLATORS: The standard value for %d is 256. */
2a4c734b
MB
1558 errx(EXIT_FAILURE, _("the list of excluded devices is "
1559 "too large (limit is %d devices)"),
1560 (int)ARRAY_SIZE(excludes));
2dc03af7 1561
2a4c734b
MB
1562 str = end && *end ? end + 1 : NULL;
1563 }
1564}
1565
2dc03af7 1566static void parse_includes(const char *str0)
2ef4e8ba 1567{
2dc03af7
KZ
1568 const char *str = str0;
1569
2ef4e8ba
KZ
1570 while (str && *str) {
1571 char *end = NULL;
1572 unsigned long n;
1573
1574 errno = 0;
1575 n = strtoul(str, &end, 10);
1576
2dc03af7
KZ
1577 if (end == str || (end && *end && *end != ','))
1578 errx(EXIT_FAILURE, _("failed to parse list '%s'"), str0);
1579 if (errno != 0 && (n == ULONG_MAX || n == 0))
1580 err(EXIT_FAILURE, _("failed to parse list '%s'"), str0);
2ef4e8ba
KZ
1581 includes[nincludes++] = n;
1582
1583 if (nincludes == ARRAY_SIZE(includes))
1584 /* TRANSLATORS: The standard value for %d is 256. */
1585 errx(EXIT_FAILURE, _("the list of included devices is "
1586 "too large (limit is %d devices)"),
1587 (int)ARRAY_SIZE(includes));
1588 str = end && *end ? end + 1 : NULL;
1589 }
1590}
1591
642048e4
KZ
1592/*
1593 * see set_sortdata_u64() and columns initialization in main()
1594 */
1595static int cmp_u64_cells(struct libscols_cell *a,
1596 struct libscols_cell *b,
1597 __attribute__((__unused__)) void *data)
1598{
1599 uint64_t *adata = (uint64_t *) scols_cell_get_userdata(a),
1600 *bdata = (uint64_t *) scols_cell_get_userdata(b);
1601
1602 if (adata == NULL && bdata == NULL)
1603 return 0;
1604 if (adata == NULL)
1605 return -1;
1606 if (bdata == NULL)
1607 return 1;
1608 return *adata == *bdata ? 0 : *adata >= *bdata ? 1 : -1;
1609}
1610
abafd686 1611static void __attribute__((__noreturn__)) help(FILE *out)
2a4c734b 1612{
507d39c2 1613 size_t i;
2a4c734b 1614
39b232c1
SK
1615 fputs(USAGE_HEADER, out);
1616 fprintf(out, _(" %s [options] [<device> ...]\n"), program_invocation_short_name);
451dbcfa
BS
1617
1618 fputs(USAGE_SEPARATOR, out);
1619 fputs(_("List information about block devices.\n"), out);
1620
39b232c1
SK
1621 fputs(USAGE_OPTIONS, out);
1622 fputs(_(" -a, --all print all devices\n"), out);
1623 fputs(_(" -b, --bytes print SIZE in bytes rather than in human readable format\n"), out);
1624 fputs(_(" -d, --nodeps don't print slaves or holders\n"), out);
1625 fputs(_(" -D, --discard print discard capabilities\n"), out);
1626 fputs(_(" -e, --exclude <list> exclude devices by major number (default: RAM disks)\n"), out);
39b232c1 1627 fputs(_(" -f, --fs output info about filesystems\n"), out);
39b232c1 1628 fputs(_(" -i, --ascii use ascii characters only\n"), out);
3e59c481 1629 fputs(_(" -I, --include <list> show only devices with specified major numbers\n"), out);
4a102a48 1630 fputs(_(" -J, --json use JSON output format\n"), out);
39b232c1 1631 fputs(_(" -l, --list use list format output\n"), out);
3e59c481 1632 fputs(_(" -m, --perms output info about permissions\n"), out);
39b232c1
SK
1633 fputs(_(" -n, --noheadings don't print headings\n"), out);
1634 fputs(_(" -o, --output <list> output columns\n"), out);
1b4d2a4a 1635 fputs(_(" -O, --output-all output all columns\n"), out);
ef75bc88 1636 fputs(_(" -p, --paths print complete device path\n"), out);
39b232c1
SK
1637 fputs(_(" -P, --pairs use key=\"value\" output format\n"), out);
1638 fputs(_(" -r, --raw use raw output format\n"), out);
1639 fputs(_(" -s, --inverse inverse dependencies\n"), out);
39b232c1 1640 fputs(_(" -S, --scsi output info about SCSI devices\n"), out);
3e59c481 1641 fputs(_(" -t, --topology output info about topology\n"), out);
b5af3ee8 1642 fputs(_(" -x, --sort <column> sort output by <column>\n"), out);
39b232c1
SK
1643 fputs(USAGE_SEPARATOR, out);
1644 fputs(USAGE_HELP, out);
1645 fputs(USAGE_VERSION, out);
2a4c734b 1646
57912387 1647 fprintf(out, _("\nAvailable columns (for --output):\n"));
2a4c734b 1648
03df0d11 1649 for (i = 0; i < ARRAY_SIZE(infos); i++)
57912387 1650 fprintf(out, " %11s %s\n", infos[i].name, _(infos[i].help));
2a4c734b 1651
57912387 1652 fprintf(out, USAGE_MAN_TAIL("lsblk(8)"));
2a4c734b
MB
1653
1654 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
1655}
1656
f65b3bb6
SK
1657static void check_sysdevblock(void)
1658{
1659 if (access(_PATH_SYS_DEVBLOCK, R_OK) != 0)
1660 err(EXIT_FAILURE, _("failed to access sysfs directory: %s"),
1661 _PATH_SYS_DEVBLOCK);
1662}
1663
2a4c734b
MB
1664int main(int argc, char *argv[])
1665{
642048e4 1666 struct lsblk _ls = { .sort_id = -1 };
9bd4e5c0 1667 int scols_flags = LSBLK_TREE;
40b17508 1668 int c, status = EXIT_FAILURE;
43bca827 1669 char *outarg = NULL;
40b17508 1670 size_t i;
2a4c734b 1671
6c7d5ae9 1672 static const struct option longopts[] = {
2a4c734b 1673 { "all", 0, 0, 'a' },
f31505fe
KZ
1674 { "bytes", 0, 0, 'b' },
1675 { "nodeps", 0, 0, 'd' },
2d232246 1676 { "discard", 0, 0, 'D' },
2a4c734b 1677 { "help", 0, 0, 'h' },
4a102a48 1678 { "json", 0, 0, 'J' },
2a4c734b 1679 { "output", 1, 0, 'o' },
1b4d2a4a 1680 { "output-all", 0, 0, 'O' },
2a4c734b
MB
1681 { "perms", 0, 0, 'm' },
1682 { "noheadings", 0, 0, 'n' },
1683 { "list", 0, 0, 'l' },
1684 { "ascii", 0, 0, 'i' },
1685 { "raw", 0, 0, 'r' },
09a71aa1 1686 { "inverse", 0, 0, 's' },
2a4c734b
MB
1687 { "fs", 0, 0, 'f' },
1688 { "exclude", 1, 0, 'e' },
2ef4e8ba 1689 { "include", 1, 0, 'I' },
2a4c734b 1690 { "topology", 0, 0, 't' },
c7e76cd1 1691 { "paths", 0, 0, 'p' },
6ea1bdd3 1692 { "pairs", 0, 0, 'P' },
28ffc2b7 1693 { "scsi", 0, 0, 'S' },
642048e4 1694 { "sort", 1, 0, 'x' },
57912387 1695 { "version", 0, 0, 'V' },
2a4c734b
MB
1696 { NULL, 0, 0, 0 },
1697 };
1698
a7349ee3 1699 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
1b4d2a4a 1700 { 'D','O' },
98f2dc7a 1701 { 'I','e' },
4a102a48 1702 { 'J', 'P', 'r' },
1b4d2a4a
MY
1703 { 'O','S' },
1704 { 'O','f' },
1705 { 'O','m' },
1706 { 'O','t' },
98f2dc7a
KZ
1707 { 'P','l','r' },
1708 { 0 }
1709 };
1710 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
1711
2a4c734b
MB
1712 setlocale(LC_ALL, "");
1713 bindtextdomain(PACKAGE, LOCALEDIR);
1714 textdomain(PACKAGE);
c05a80ca 1715 atexit(close_stdout);
2a4c734b
MB
1716
1717 lsblk = &_ls;
2a4c734b 1718
7e786eca
KZ
1719 lsblk_init_debug();
1720
98f2dc7a 1721 while((c = getopt_long(argc, argv,
4a102a48 1722 "abdDe:fhJlnmo:OpPiI:rstVSx:", longopts, NULL)) != -1) {
98f2dc7a
KZ
1723
1724 err_exclusive_options(c, longopts, excl, excl_st);
1725
2a4c734b
MB
1726 switch(c) {
1727 case 'a':
1728 lsblk->all_devices = 1;
1729 break;
1730 case 'b':
1731 lsblk->bytes = 1;
1732 break;
f31505fe
KZ
1733 case 'd':
1734 lsblk->nodeps = 1;
1735 break;
2d232246 1736 case 'D':
b6327c6f
KZ
1737 add_column(columns, ncolumns++, COL_NAME);
1738 add_column(columns, ncolumns++, COL_DALIGN);
1739 add_column(columns, ncolumns++, COL_DGRAN);
1740 add_column(columns, ncolumns++, COL_DMAX);
1741 add_column(columns, ncolumns++, COL_DZERO);
2d232246 1742 break;
2a4c734b
MB
1743 case 'e':
1744 parse_excludes(optarg);
1745 break;
1746 case 'h':
1747 help(stdout);
1748 break;
4a102a48
KZ
1749 case 'J':
1750 scols_flags |= LSBLK_JSON;
1751 break;
2a4c734b 1752 case 'l':
9bd4e5c0 1753 scols_flags &= ~LSBLK_TREE; /* disable the default */
2a4c734b
MB
1754 break;
1755 case 'n':
9bd4e5c0 1756 scols_flags |= LSBLK_NOHEADINGS;
2a4c734b
MB
1757 break;
1758 case 'o':
43bca827 1759 outarg = optarg;
2a4c734b 1760 break;
1b4d2a4a 1761 case 'O':
40b17508 1762 for (ncolumns = 0 ; ncolumns < ARRAY_SIZE(infos); ncolumns++)
1b4d2a4a
MY
1763 columns[ncolumns] = ncolumns;
1764 break;
c7e76cd1
KZ
1765 case 'p':
1766 lsblk->paths = 1;
1767 break;
6ea1bdd3 1768 case 'P':
9bd4e5c0
OO
1769 scols_flags |= LSBLK_EXPORT;
1770 scols_flags &= ~LSBLK_TREE; /* disable the default */
6ea1bdd3 1771 break;
2a4c734b 1772 case 'i':
9bd4e5c0 1773 scols_flags |= LSBLK_ASCII;
2a4c734b 1774 break;
2ef4e8ba 1775 case 'I':
2ef4e8ba
KZ
1776 parse_includes(optarg);
1777 break;
2a4c734b 1778 case 'r':
9bd4e5c0
OO
1779 scols_flags &= ~LSBLK_TREE; /* disable the default */
1780 scols_flags |= LSBLK_RAW; /* enable raw */
2a4c734b 1781 break;
09a71aa1
PR
1782 case 's':
1783 lsblk->inverse = 1;
1784 break;
2a4c734b 1785 case 'f':
b6327c6f
KZ
1786 add_column(columns, ncolumns++, COL_NAME);
1787 add_column(columns, ncolumns++, COL_FSTYPE);
1788 add_column(columns, ncolumns++, COL_LABEL);
1789 add_column(columns, ncolumns++, COL_UUID);
1790 add_column(columns, ncolumns++, COL_TARGET);
2a4c734b
MB
1791 break;
1792 case 'm':
b6327c6f
KZ
1793 add_column(columns, ncolumns++, COL_NAME);
1794 add_column(columns, ncolumns++, COL_SIZE);
1795 add_column(columns, ncolumns++, COL_OWNER);
1796 add_column(columns, ncolumns++, COL_GROUP);
1797 add_column(columns, ncolumns++, COL_MODE);
2a4c734b
MB
1798 break;
1799 case 't':
b6327c6f
KZ
1800 add_column(columns, ncolumns++, COL_NAME);
1801 add_column(columns, ncolumns++, COL_ALIOFF);
1802 add_column(columns, ncolumns++, COL_MINIO);
1803 add_column(columns, ncolumns++, COL_OPTIO);
1804 add_column(columns, ncolumns++, COL_PHYSEC);
1805 add_column(columns, ncolumns++, COL_LOGSEC);
1806 add_column(columns, ncolumns++, COL_ROTA);
1807 add_column(columns, ncolumns++, COL_SCHED);
1808 add_column(columns, ncolumns++, COL_RQ_SIZE);
1809 add_column(columns, ncolumns++, COL_RA);
1810 add_column(columns, ncolumns++, COL_WSAME);
2a4c734b 1811 break;
28ffc2b7
MB
1812 case 'S':
1813 lsblk->nodeps = 1;
1814 lsblk->scsi = 1;
b6327c6f
KZ
1815 add_column(columns, ncolumns++, COL_NAME);
1816 add_column(columns, ncolumns++, COL_HCTL);
1817 add_column(columns, ncolumns++, COL_TYPE);
1818 add_column(columns, ncolumns++, COL_VENDOR);
1819 add_column(columns, ncolumns++, COL_MODEL);
1820 add_column(columns, ncolumns++, COL_REV);
1821 add_column(columns, ncolumns++, COL_TRANSPORT);
28ffc2b7 1822 break;
57912387 1823 case 'V':
e421313d 1824 printf(UTIL_LINUX_VERSION);
57912387 1825 return EXIT_SUCCESS;
642048e4
KZ
1826 case 'x':
1827 scols_flags &= ~LSBLK_TREE; /* disable the default */
1828 lsblk->sort_id = column_name_to_id(optarg, strlen(optarg));
1829 if (lsblk->sort_id >= 0)
1830 break;
1831 /* fallthrough */
2a4c734b 1832 default:
193b3239 1833 errtryhelp(EXIT_FAILURE);
2a4c734b
MB
1834 }
1835 }
1836
f65b3bb6
SK
1837 check_sysdevblock();
1838
2a4c734b 1839 if (!ncolumns) {
b6327c6f
KZ
1840 add_column(columns, ncolumns++, COL_NAME);
1841 add_column(columns, ncolumns++, COL_MAJMIN);
1842 add_column(columns, ncolumns++, COL_RM);
1843 add_column(columns, ncolumns++, COL_SIZE);
1844 add_column(columns, ncolumns++, COL_RO);
1845 add_column(columns, ncolumns++, COL_TYPE);
1846 add_column(columns, ncolumns++, COL_TARGET);
2a4c734b
MB
1847 }
1848
43bca827
MB
1849 if (outarg && string_add_to_idarray(outarg, columns, ARRAY_SIZE(columns),
1850 &ncolumns, column_name_to_id) < 0)
1851 return EXIT_FAILURE;
1852
2ef4e8ba 1853 if (nexcludes == 0 && nincludes == 0)
2a4c734b 1854 excludes[nexcludes++] = 1; /* default: ignore RAM disks */
9554f7ab 1855
5e2305ca
KZ
1856 if (lsblk->sort_id < 0)
1857 /* Since Linux 4.8 we have sort devices by default, because
1858 * /sys is no more sorted */
1859 lsblk->sort_id = COL_MAJMIN;
1860
a6dc8dcd
KZ
1861 if (lsblk->sort_id >= 0 && column_id_to_number(lsblk->sort_id) < 0) {
1862 /* the sort column is not between output columns -- add as hidden */
1863 add_column(columns, ncolumns++, lsblk->sort_id);
1864 lsblk->sort_hidden = 1;
1865 }
642048e4 1866
9554f7ab 1867 mnt_init_debug(0);
710ed55d 1868 scols_init_debug(0);
9554f7ab 1869
2a4c734b
MB
1870 /*
1871 * initialize output columns
1872 */
0925a9dd 1873 if (!(lsblk->table = scols_new_table()))
2a4c734b 1874 errx(EXIT_FAILURE, _("failed to initialize output table"));
0925a9dd
KZ
1875 scols_table_enable_raw(lsblk->table, !!(scols_flags & LSBLK_RAW));
1876 scols_table_enable_export(lsblk->table, !!(scols_flags & LSBLK_EXPORT));
1877 scols_table_enable_ascii(lsblk->table, !!(scols_flags & LSBLK_ASCII));
4a102a48 1878 scols_table_enable_json(lsblk->table, !!(scols_flags & LSBLK_JSON));
0925a9dd 1879 scols_table_enable_noheadings(lsblk->table, !!(scols_flags & LSBLK_NOHEADINGS));
2a4c734b 1880
4a102a48
KZ
1881 if (scols_flags & LSBLK_JSON)
1882 scols_table_set_name(lsblk->table, "blockdevices");
1883
2a4c734b
MB
1884 for (i = 0; i < ncolumns; i++) {
1885 struct colinfo *ci = get_column_info(i);
642048e4
KZ
1886 struct libscols_column *cl;
1887 int id = get_column_id(i), fl = ci->flags;
2a4c734b 1888
642048e4 1889 if (!(scols_flags & LSBLK_TREE) && id == COL_NAME)
9bd4e5c0 1890 fl &= ~SCOLS_FL_TREE;
a6dc8dcd
KZ
1891 if (lsblk->sort_hidden && lsblk->sort_id == id)
1892 fl |= SCOLS_FL_HIDDEN;
2a4c734b 1893
642048e4
KZ
1894 cl = scols_table_new_column(lsblk->table, ci->name, ci->whint, fl);
1895 if (!cl) {
2a4c734b
MB
1896 warn(_("failed to initialize output column"));
1897 goto leave;
1898 }
642048e4
KZ
1899 if (!lsblk->sort_col && lsblk->sort_id == id) {
1900 lsblk->sort_col = cl;
1901 scols_column_set_cmpfunc(cl,
1902 ci->sort_type == SORT_STRING ?
1903 scols_cmpstr_cells : cmp_u64_cells, NULL);
1904 }
2a4c734b
MB
1905 }
1906
1907 if (optind == argc)
1539750f
KZ
1908 status = iterate_block_devices() == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
1909 else {
1910 int cnt = 0, cnt_err = 0;
1911
1912 while (optind < argc) {
1913 if (process_one_device(argv[optind++]) != 0)
1914 cnt_err++;
1915 cnt++;
1916 }
1917 status = cnt == 0 ? EXIT_FAILURE : /* nothing */
1918 cnt == cnt_err ? LSBLK_EXIT_ALLFAILED :/* all failed */
1919 cnt_err ? LSBLK_EXIT_SOMEOK : /* some ok */
1920 EXIT_SUCCESS; /* all success */
1921 }
2a4c734b 1922
642048e4
KZ
1923 if (lsblk->sort_col)
1924 scols_sort_table(lsblk->table, lsblk->sort_col);
1925
9bd4e5c0 1926 scols_print_table(lsblk->table);
2a4c734b
MB
1927
1928leave:
642048e4
KZ
1929 if (lsblk->sort_col)
1930 unref_sortdata(lsblk->table);
1931
9bd4e5c0 1932 scols_unref_table(lsblk->table);
50fccba1
KZ
1933
1934 mnt_unref_table(mtab);
1935 mnt_unref_table(swaps);
6195f9e6 1936 mnt_unref_cache(mntcache);
d44e3391
KZ
1937#ifdef HAVE_LIBUDEV
1938 udev_unref(udev);
1939#endif
2a4c734b
MB
1940 return status;
1941}