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