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