]> git.ipfire.org Git - thirdparty/util-linux.git/blame - misc-utils/lsblk.c
Merge branch 'meson-more-build-options' of https://github.com/jwillikers/util-linux
[thirdparty/util-linux.git] / misc-utils / lsblk.c
CommitLineData
2a4c734b 1/*
f77fa578 2 * lsblk(8) - list block devices
2a4c734b 3 *
b93585e8 4 * Copyright (C) 2010-2018 Red Hat, Inc. All rights reserved.
67b8731f 5 * Written by Milan Broz <gmazyland@gmail.com>
2a4c734b
MB
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 21 */
2a4c734b
MB
22#include <stdio.h>
23#include <errno.h>
24#include <getopt.h>
2a4c734b
MB
25#include <stdlib.h>
26#include <unistd.h>
2a4c734b
MB
27#include <sys/types.h>
28#include <sys/stat.h>
29#include <dirent.h>
30#include <fcntl.h>
31#include <string.h>
2a4c734b 32#include <sys/ioctl.h>
2a4c734b
MB
33#include <stdarg.h>
34#include <locale.h>
35#include <pwd.h>
36#include <grp.h>
18eac5ba 37#include <ctype.h>
ccafadb7 38#include <assert.h>
2a4c734b
MB
39
40#include <blkid.h>
2a4c734b 41
540d506f 42#include "c.h"
2a4c734b
MB
43#include "pathnames.h"
44#include "blkdev.h"
45#include "canonicalize.h"
2a4c734b 46#include "nls.h"
2a4c734b
MB
47#include "xalloc.h"
48#include "strutils.h"
766ab9b0 49#include "sysfs.h"
c05a80ca 50#include "closestream.h"
05187653 51#include "optutils.h"
7761bd3b 52#include "fileutils.h"
14bb8e3c 53#include "loopdev.h"
c6648d2d 54#include "buffer.h"
c3317cdc 55#include "colors.h"
ed02618d 56#include "column-list-table.h"
2a4c734b 57
14560b7f 58#include "lsblk.h"
7e786eca 59
14560b7f 60UL_DEBUG_DEFINE_MASK(lsblk);
7e786eca
KZ
61UL_DEBUG_DEFINE_MASKNAMES(lsblk) = UL_DEBUG_EMPTY_MASKNAMES;
62
1539750f
KZ
63#define LSBLK_EXIT_SOMEOK 64
64#define LSBLK_EXIT_ALLFAILED 32
65
be685b98
KZ
66static int column_id_to_number(int id);
67
2a4c734b
MB
68/* column IDs */
69enum {
98a586f6 70 COL_ALIOFF = 0,
e054895f 71 COL_IDLINK,
875d6a31 72 COL_ID,
98a586f6
KZ
73 COL_DALIGN,
74 COL_DAX,
75 COL_DGRAN,
36e0914f 76 COL_DISKSEQ,
98a586f6
KZ
77 COL_DMAX,
78 COL_DZERO,
bc405d64 79 COL_FSAVAIL,
98a586f6 80 COL_FSROOTS,
bc405d64 81 COL_FSSIZE,
2a4c734b 82 COL_FSTYPE,
bc405d64
KZ
83 COL_FSUSED,
84 COL_FSUSEPERC,
9cca1ef2 85 COL_FSVERSION,
98a586f6
KZ
86 COL_GROUP,
87 COL_HCTL,
88 COL_HOTPLUG,
89 COL_KNAME,
2a4c734b 90 COL_LABEL,
98a586f6
KZ
91 COL_LOGSEC,
92 COL_MAJMIN,
0dbb3e06
KZ
93 COL_MAJ,
94 COL_MIN,
98a586f6
KZ
95 COL_MINIO,
96 COL_MODE,
97 COL_MODEL,
3b262733 98 COL_MQ,
98a586f6
KZ
99 COL_NAME,
100 COL_OPTIO,
101 COL_OWNER,
102 COL_PARTFLAGS,
103 COL_PARTLABEL,
e4ed8665 104 COL_PARTN,
4686ba17 105 COL_PARTTYPE,
107e9559 106 COL_PARTTYPENAME,
aa0903e0 107 COL_PARTUUID,
98a586f6
KZ
108 COL_PATH,
109 COL_PHYSEC,
110 COL_PKNAME,
111 COL_PTTYPE,
112 COL_PTUUID,
150db7a9 113 COL_RA,
98a586f6
KZ
114 COL_RAND,
115 COL_REV,
627970a7 116 COL_RM,
98a586f6
KZ
117 COL_RO,
118 COL_ROTA,
119 COL_RQ_SIZE,
120 COL_SCHED,
460c7afb 121 COL_SERIAL,
2a4c734b 122 COL_SIZE,
98a586f6 123 COL_START,
01e487c0 124 COL_STATE,
98a586f6
KZ
125 COL_SUBSYS,
126 COL_TARGET,
127 COL_TARGETS,
128 COL_TRANSPORT,
18eac5ba 129 COL_TYPE,
98a586f6
KZ
130 COL_UUID,
131 COL_VENDOR,
2adb1a44 132 COL_WSAME,
88ca32b3 133 COL_WWN,
f2df4365 134 COL_ZONED,
ca5f4bd0
NA
135 COL_ZONE_SZ,
136 COL_ZONE_WGRAN,
137 COL_ZONE_APP,
138 COL_ZONE_NR,
139 COL_ZONE_OMAX,
140 COL_ZONE_AMAX,
2a4c734b
MB
141};
142
9bd4e5c0
OO
143/* basic table settings */
144enum {
145 LSBLK_ASCII = (1 << 0),
146 LSBLK_RAW = (1 << 1),
147 LSBLK_NOHEADINGS = (1 << 2),
bb6e822a
KZ
148 LSBLK_EXPORT = (1 << 3),
149 LSBLK_TREE = (1 << 4),
4a102a48 150 LSBLK_JSON = (1 << 5),
25fb0638 151 LSBLK_SHELLVAR = (1 << 6)
9bd4e5c0
OO
152};
153
407e0ea6 154/* Types used for qsort() and JSON */
642048e4 155enum {
407e0ea6
KZ
156 COLTYPE_STR = 0, /* default */
157 COLTYPE_NUM = 1, /* always u64 number */
158 COLTYPE_SORTNUM = 2, /* string on output, u64 for qsort() */
159 COLTYPE_SIZE = 3, /* srring by default, number when --bytes */
160 COLTYPE_BOOL = 4 /* 0 or 1 */
642048e4
KZ
161};
162
2a4c734b
MB
163/* column names */
164struct colinfo {
37b2b3fa 165 const char * const name; /* header */
2a4c734b 166 double whint; /* width hint (N < 1 is in percent of termwidth) */
9bd4e5c0 167 int flags; /* SCOLS_FL_* */
98a586f6 168 const char *help;
407e0ea6 169 int type; /* COLTYPE_* */
2a4c734b
MB
170};
171
172/* columns descriptions */
37b2b3fa 173static const struct colinfo infos[] = {
98a586f6 174 [COL_ALIOFF] = { "ALIGNMENT", 6, SCOLS_FL_RIGHT, N_("alignment offset"), COLTYPE_NUM },
875d6a31 175 [COL_ID] = { "ID", 0.1, SCOLS_FL_NOEXTREMES, N_("udev ID (based on ID-LINK)") },
e054895f 176 [COL_IDLINK] = { "ID-LINK", 0.1, SCOLS_FL_NOEXTREMES, N_("the shortest udev /dev/disk/by-id link name") },
98a586f6
KZ
177 [COL_DALIGN] = { "DISC-ALN", 6, SCOLS_FL_RIGHT, N_("discard alignment offset"), COLTYPE_NUM },
178 [COL_DAX] = { "DAX", 1, SCOLS_FL_RIGHT, N_("dax-capable device"), COLTYPE_BOOL },
ed02618d 179 [COL_DGRAN] = { "DISC-GRAN", 6, SCOLS_FL_RIGHT, N_("discard granularity, use <number> if --bytes is given"), COLTYPE_SIZE },
36e0914f 180 [COL_DISKSEQ] = { "DISK-SEQ", 1, SCOLS_FL_RIGHT, N_("disk sequence number"), COLTYPE_NUM },
ed02618d 181 [COL_DMAX] = { "DISC-MAX", 6, SCOLS_FL_RIGHT, N_("discard max bytes, use <number> if --bytes is given"), COLTYPE_SIZE },
98a586f6 182 [COL_DZERO] = { "DISC-ZERO", 1, SCOLS_FL_RIGHT, N_("discard zeroes data"), COLTYPE_BOOL },
563fdcb0 183 [COL_FSAVAIL] = { "FSAVAIL", 5, SCOLS_FL_RIGHT, N_("filesystem size available for unprivileged users, use <number> if --bytes is given"), COLTYPE_SIZE },
98a586f6 184 [COL_FSROOTS] = { "FSROOTS", 0.1, SCOLS_FL_WRAP, N_("mounted filesystem roots") },
ed02618d 185 [COL_FSSIZE] = { "FSSIZE", 5, SCOLS_FL_RIGHT, N_("filesystem size, use <number> if --bytes is given"), COLTYPE_SIZE },
98a586f6 186 [COL_FSTYPE] = { "FSTYPE", 0.1, SCOLS_FL_TRUNC, N_("filesystem type") },
ed02618d 187 [COL_FSUSED] = { "FSUSED", 5, SCOLS_FL_RIGHT, N_("filesystem size used, use <number> if --bytes is given"), COLTYPE_SIZE },
bc405d64 188 [COL_FSUSEPERC] = { "FSUSE%", 3, SCOLS_FL_RIGHT, N_("filesystem use percentage") },
9cca1ef2 189 [COL_FSVERSION] = { "FSVER", 0.1, SCOLS_FL_TRUNC, N_("filesystem version") },
98a586f6
KZ
190 [COL_GROUP] = { "GROUP", 0.1, SCOLS_FL_TRUNC, N_("group name") },
191 [COL_HCTL] = { "HCTL", 10, 0, N_("Host:Channel:Target:Lun for SCSI") },
192 [COL_HOTPLUG] = { "HOTPLUG", 1, SCOLS_FL_RIGHT, N_("removable or hotplug device (usb, pcmcia, ...)"), COLTYPE_BOOL },
193 [COL_KNAME] = { "KNAME", 0.3, 0, N_("internal kernel device name") },
194 [COL_LABEL] = { "LABEL", 0.1, 0, N_("filesystem LABEL") },
195 [COL_LOGSEC] = { "LOG-SEC", 7, SCOLS_FL_RIGHT, N_("logical sector size"), COLTYPE_NUM },
196 [COL_MAJMIN] = { "MAJ:MIN", 6, 0, N_("major:minor device number"), COLTYPE_SORTNUM },
0dbb3e06
KZ
197 [COL_MAJ] = { "MAJ", 3, 0, N_("major device number"), COLTYPE_SORTNUM },
198 [COL_MIN] = { "MIN", 3, 0, N_("minor device number"), COLTYPE_SORTNUM },
98a586f6
KZ
199 [COL_MINIO] = { "MIN-IO", 6, SCOLS_FL_RIGHT, N_("minimum I/O size"), COLTYPE_NUM },
200 [COL_MODEL] = { "MODEL", 0.1, SCOLS_FL_TRUNC, N_("device identifier") },
201 [COL_MODE] = { "MODE", 10, 0, N_("device node permissions") },
3b262733 202 [COL_MQ] = { "MQ", 3, SCOLS_FL_RIGHT, N_("device queues") },
98a586f6
KZ
203 [COL_NAME] = { "NAME", 0.25, SCOLS_FL_NOEXTREMES, N_("device name") },
204 [COL_OPTIO] = { "OPT-IO", 6, SCOLS_FL_RIGHT, N_("optimal I/O size"), COLTYPE_NUM },
205 [COL_OWNER] = { "OWNER", 0.1, SCOLS_FL_TRUNC, N_("user name"), },
206 [COL_PARTFLAGS] = { "PARTFLAGS", 36, 0, N_("partition flags") },
aa0903e0 207 [COL_PARTLABEL] = { "PARTLABEL", 0.1, 0, N_("partition LABEL") },
e4ed8665 208 [COL_PARTN] = { "PARTN", 2, SCOLS_FL_RIGHT, N_("partition number as read from the partition table"), COLTYPE_NUM },
98a586f6
KZ
209 [COL_PARTTYPENAME] = { "PARTTYPENAME", 0.1, 0, N_("partition type name") },
210 [COL_PARTTYPE] = { "PARTTYPE", 36, 0, N_("partition type code or UUID") },
211 [COL_PARTUUID] = { "PARTUUID", 36, 0, N_("partition UUID") },
212 [COL_PATH] = { "PATH", 0.3, 0, N_("path to the device node") },
407e0ea6 213 [COL_PHYSEC] = { "PHY-SEC", 7, SCOLS_FL_RIGHT, N_("physical sector size"), COLTYPE_NUM },
98a586f6
KZ
214 [COL_PKNAME] = { "PKNAME", 0.3, 0, N_("internal parent kernel device name") },
215 [COL_PTTYPE] = { "PTTYPE", 0.1, 0, N_("partition table type") },
216 [COL_PTUUID] = { "PTUUID", 36, 0, N_("partition table identifier (usually UUID)") },
217 [COL_RAND] = { "RAND", 1, SCOLS_FL_RIGHT, N_("adds randomness"), COLTYPE_BOOL },
218 [COL_RA] = { "RA", 3, SCOLS_FL_RIGHT, N_("read-ahead of the device"), COLTYPE_NUM },
219 [COL_REV] = { "REV", 4, SCOLS_FL_RIGHT, N_("device revision") },
220 [COL_RM] = { "RM", 1, SCOLS_FL_RIGHT, N_("removable device"), COLTYPE_BOOL },
221 [COL_ROTA] = { "ROTA", 1, SCOLS_FL_RIGHT, N_("rotational device"), COLTYPE_BOOL },
222 [COL_RO] = { "RO", 1, SCOLS_FL_RIGHT, N_("read-only device"), COLTYPE_BOOL },
407e0ea6 223 [COL_RQ_SIZE]= { "RQ-SIZE", 5, SCOLS_FL_RIGHT, N_("request queue size"), COLTYPE_NUM },
98a586f6
KZ
224 [COL_SCHED] = { "SCHED", 0.1, 0, N_("I/O scheduler name") },
225 [COL_SERIAL] = { "SERIAL", 0.1, SCOLS_FL_TRUNC, N_("disk serial number") },
ed02618d 226 [COL_SIZE] = { "SIZE", 5, SCOLS_FL_RIGHT, N_("size of the device, use <number> if --bytes is given"), COLTYPE_SIZE },
61c40f48 227 [COL_START] = { "START", 5, SCOLS_FL_RIGHT, N_("partition start offset (in 512-byte sectors)"), COLTYPE_NUM },
98a586f6 228 [COL_STATE] = { "STATE", 7, SCOLS_FL_TRUNC, N_("state of the device") },
7f14ee1b 229 [COL_SUBSYS] = { "SUBSYSTEMS", 0.1, SCOLS_FL_NOEXTREMES, N_("de-duplicated chain of subsystems") },
3471bc1d
KZ
230 [COL_TARGETS] = { "MOUNTPOINTS", 0.10, SCOLS_FL_WRAP | SCOLS_FL_NOEXTREMES, N_("all locations where device is mounted") },
231 [COL_TARGET] = { "MOUNTPOINT", 0.10, SCOLS_FL_TRUNC | SCOLS_FL_NOEXTREMES, N_("where the device is mounted") },
98a586f6
KZ
232 [COL_TRANSPORT] = { "TRAN", 6, 0, N_("device transport type") },
233 [COL_TYPE] = { "TYPE", 4, 0, N_("device type") },
234 [COL_UUID] = { "UUID", 36, 0, N_("filesystem UUID") },
9bd4e5c0 235 [COL_VENDOR] = { "VENDOR", 0.1, SCOLS_FL_TRUNC, N_("device vendor") },
ed02618d 236 [COL_WSAME] = { "WSAME", 6, SCOLS_FL_RIGHT, N_("write same max bytes, use <number> if --bytes is given"), COLTYPE_SIZE },
98a586f6
KZ
237 [COL_WWN] = { "WWN", 18, 0, N_("unique storage identifier") },
238 [COL_ZONED] = { "ZONED", 0.3, 0, N_("zone model") },
ed02618d
KZ
239 [COL_ZONE_SZ] = { "ZONE-SZ", 9, SCOLS_FL_RIGHT, N_("zone size, use <number> if --bytes is given"), COLTYPE_SIZE },
240 [COL_ZONE_WGRAN] = { "ZONE-WGRAN", 10, SCOLS_FL_RIGHT, N_("zone write granularity, use <number> if --bytes is given"), COLTYPE_SIZE },
241 [COL_ZONE_APP] = { "ZONE-APP", 11, SCOLS_FL_RIGHT, N_("zone append max bytes, use <number> if --bytes is given"), COLTYPE_SIZE },
ca5f4bd0
NA
242 [COL_ZONE_NR] = { "ZONE-NR", 8, SCOLS_FL_RIGHT, N_("number of zones"), COLTYPE_NUM },
243 [COL_ZONE_OMAX] = { "ZONE-OMAX", 10, SCOLS_FL_RIGHT, N_("maximum number of open zones"), COLTYPE_NUM },
244 [COL_ZONE_AMAX] = { "ZONE-AMAX", 10, SCOLS_FL_RIGHT, N_("maximum number of active zones"), COLTYPE_NUM },
2a4c734b
MB
245};
246
3e0b4a82
KZ
247/* "userdata" used by callback for libsmartcols filter */
248struct filler_data {
249 struct lsblk_device *dev;
250 struct lsblk_device *parent;
251};
252
14560b7f 253struct lsblk *lsblk; /* global handler */
507d39c2 254
b93585e8
KZ
255/*
256 * columns[] array specifies all currently wanted output column. The columns
b6327c6f
KZ
257 * are defined by infos[] array and you can specify (on command line) each
258 * column twice. That's enough, dynamically allocated array of the columns is
b93585e8
KZ
259 * unnecessary overkill and over-engineering in this case
260 */
b6327c6f 261static int columns[ARRAY_SIZE(infos) * 2];
40b17508 262static size_t ncolumns;
b6327c6f 263
be685b98 264static inline void add_column(int id)
b6327c6f 265{
be685b98 266 if (ncolumns >= ARRAY_SIZE(columns))
b6327c6f 267 errx(EXIT_FAILURE, _("too many columns specified, "
1d231190 268 "the limit is %zu columns"),
be685b98
KZ
269 ARRAY_SIZE(columns) - 1);
270 columns[ ncolumns++ ] = id;
b6327c6f
KZ
271}
272
be685b98
KZ
273static inline void add_uniq_column(int id)
274{
275 if (column_id_to_number(id) < 0)
276 add_column(id);
277}
2a4c734b 278
b93585e8
KZ
279static void lsblk_init_debug(void)
280{
281 __UL_INIT_DEBUG_FROM_ENV(lsblk, LSBLK_DEBUG_, 0, LSBLK_DEBUG);
282}
283
284/*
285 * exclude/include devices filter based on major device numbers
286 */
f6da50d4
BV
287static int excludes[256];
288static size_t nexcludes;
2a4c734b 289
2ef4e8ba
KZ
290static int includes[256];
291static size_t nincludes;
292
2a4c734b
MB
293static int is_maj_excluded(int maj)
294{
6aace32f 295 size_t i;
2a4c734b
MB
296
297 assert(ARRAY_SIZE(excludes) > nexcludes);
298
2ef4e8ba 299 if (!nexcludes)
9e930041 300 return 0; /* filter not enabled, device not excluded */
2ef4e8ba 301
7e786eca
KZ
302 for (i = 0; i < nexcludes; i++) {
303 if (excludes[i] == maj) {
304 DBG(FILTER, ul_debug("exclude: maj=%d", maj));
2a4c734b 305 return 1;
7e786eca
KZ
306 }
307 }
2a4c734b
MB
308 return 0;
309}
310
2ef4e8ba
KZ
311static int is_maj_included(int maj)
312{
313 size_t i;
314
315 assert(ARRAY_SIZE(includes) > nincludes);
316
317 if (!nincludes)
318 return 1; /* filter not enabled, device is included */
319
7e786eca
KZ
320 for (i = 0; i < nincludes; i++) {
321 if (includes[i] == maj) {
322 DBG(FILTER, ul_debug("include: maj=%d", maj));
2ef4e8ba 323 return 1;
7e786eca
KZ
324 }
325 }
2ef4e8ba
KZ
326 return 0;
327}
328
b93585e8 329/* Converts column sequential number to column ID (COL_*) */
2a4c734b
MB
330static int get_column_id(int num)
331{
40b17508
KZ
332 assert(num >= 0);
333 assert((size_t) num < ncolumns);
b6327c6f 334 assert(columns[num] < (int) ARRAY_SIZE(infos));
2a4c734b
MB
335 return columns[num];
336}
337
b93585e8 338/* Returns column description for the column sequential number */
37b2b3fa 339static const struct colinfo *get_column_info(int num)
2a4c734b
MB
340{
341 return &infos[ get_column_id(num) ];
342}
343
b93585e8 344/* Converts column name (as defined in the infos[] to the column ID */
2a4c734b
MB
345static int column_name_to_id(const char *name, size_t namesz)
346{
507d39c2 347 size_t i;
2a4c734b 348
30edd8e1 349 /* name as diplayed for users */
03df0d11 350 for (i = 0; i < ARRAY_SIZE(infos); i++) {
2a4c734b
MB
351 const char *cn = infos[i].name;
352
353 if (!strncasecmp(name, cn, namesz) && !*(cn + namesz))
354 return i;
355 }
30edd8e1
KZ
356
357 /* name as used in expressions, JSON output etc. */
358 if (strnchr(name, namesz, '_')) {
359 char *buf = NULL;
360 size_t bufsz = 0;
361
362 for (i = 0; i < ARRAY_SIZE(infos); i++) {
363 if (scols_shellvar_name(infos[i].name, &buf, &bufsz) != 0)
364 continue;
365 if (!strncasecmp(name, buf, namesz) && !*(buf + namesz)) {
366 free(buf);
367 return i;
368 }
369 }
370 free(buf);
371 }
372
2a4c734b
MB
373 warnx(_("unknown column: %s"), name);
374 return -1;
375}
376
b93585e8 377/* Converts column ID (COL_*) to column sequential number */
642048e4
KZ
378static int column_id_to_number(int id)
379{
380 size_t i;
381
40b17508 382 for (i = 0; i < ncolumns; i++)
642048e4
KZ
383 if (columns[i] == id)
384 return i;
385 return -1;
386}
387
b93585e8 388/* Checks for DM prefix in the device name */
2a4c734b
MB
389static int is_dm(const char *name)
390{
391 return strncmp(name, "dm-", 3) ? 0 : 1;
392}
393
b158bd29 394/* Returns full pat to the device node (TODO: what about sysfs_blkdev_get_path()) */
fed34a1e 395static char *get_device_path(struct lsblk_device *dev)
2a4c734b
MB
396{
397 char path[PATH_MAX];
398
fed34a1e
KZ
399 assert(dev);
400 assert(dev->name);
2a4c734b 401
fed34a1e
KZ
402 if (is_dm(dev->name))
403 return __canonicalize_dm_name(lsblk->sysroot, dev->name);
2a4c734b 404
fed34a1e 405 snprintf(path, sizeof(path), "/dev/%s", dev->name);
92441d5c 406 sysfs_devname_sys_to_dev(path);
2a4c734b
MB
407 return xstrdup(path);
408}
409
fed34a1e 410static int is_readonly_device(struct lsblk_device *dev)
2a4c734b
MB
411{
412 int fd, ro = 0;
413
fed34a1e 414 if (ul_path_scanf(dev->sysfs, "ro", "%d", &ro) == 1)
2a4c734b
MB
415 return ro;
416
417 /* fallback if "ro" attribute does not exist */
fed34a1e 418 fd = open(dev->filename, O_RDONLY);
2a4c734b 419 if (fd != -1) {
9c53a49c
KZ
420 if (ioctl(fd, BLKROGET, &ro) != 0)
421 ro = 0;
2a4c734b
MB
422 close(fd);
423 }
424 return ro;
425}
426
fed34a1e 427static char *get_scheduler(struct lsblk_device *dev)
2a4c734b 428{
f153614c 429 char buf[128];
2a4c734b
MB
430 char *p, *res = NULL;
431
fed34a1e 432 if (ul_path_read_buffer(dev->sysfs, buf, sizeof(buf), "queue/scheduler") == 0)
2a4c734b 433 return NULL;
f153614c 434 p = strchr(buf, '[');
2a4c734b
MB
435 if (p) {
436 res = p + 1;
437 p = strchr(res, ']');
438 if (p) {
439 *p = '\0';
440 res = xstrdup(res);
441 } else
442 res = NULL;
443 }
2a4c734b
MB
444 return res;
445}
446
fed34a1e 447static char *get_type(struct lsblk_device *dev)
18eac5ba
MB
448{
449 char *res = NULL, *p;
450
0bd05f5e 451 if (device_is_partition(dev))
ef2ce68b
KZ
452 return xstrdup("part");
453
fed34a1e 454 if (is_dm(dev->name)) {
f153614c 455 char *dm_uuid = NULL;
18eac5ba
MB
456
457 /* The DM_UUID prefix should be set to subsystem owning
458 * the device - LVM, CRYPT, DMRAID, MPATH, PART */
fed34a1e 459 if (ul_path_read_string(dev->sysfs, &dm_uuid, "dm/uuid") > 0
f153614c 460 && dm_uuid) {
18eac5ba
MB
461 char *tmp = dm_uuid;
462 char *dm_uuid_prefix = strsep(&tmp, "-");
463
464 if (dm_uuid_prefix) {
465 /* kpartx hack to remove partition number */
466 if (strncasecmp(dm_uuid_prefix, "part", 4) == 0)
467 dm_uuid_prefix[4] = '\0';
468
469 res = xstrdup(dm_uuid_prefix);
470 }
471 }
472
473 free(dm_uuid);
474 if (!res)
475 /* No UUID or no prefix - just mark it as DM device */
476 res = xstrdup("dm");
477
fed34a1e 478 } else if (!strncmp(dev->name, "loop", 4)) {
18eac5ba
MB
479 res = xstrdup("loop");
480
fed34a1e 481 } else if (!strncmp(dev->name, "md", 2)) {
f153614c
KZ
482 char *md_level = NULL;
483
fed34a1e 484 ul_path_read_string(dev->sysfs, &md_level, "md/level");
18eac5ba
MB
485 res = md_level ? md_level : xstrdup("md");
486
487 } else {
8d687180 488 const char *type = NULL;
90e9fcda 489 int x = 0;
9d738ff8 490
fed34a1e 491 if (ul_path_read_s32(dev->sysfs, &x, "device/type") == 0)
8d687180 492 type = blkdev_scsi_type_to_name(x);
9d738ff8 493 if (!type)
ef2ce68b 494 type = "disk";
9d738ff8 495 res = xstrdup(type);
18eac5ba
MB
496 }
497
498 for (p = res; p && *p; p++)
499 *p = tolower((unsigned char) *p);
500 return res;
501}
502
a5fb4d23 503/* Thanks to lsscsi code for idea of detection logic used here */
3769f26e 504static const char *get_transport(struct lsblk_device *dev)
a5fb4d23 505{
fed34a1e 506 struct path_cxt *sysfs = dev->sysfs;
2f2f7e80
KZ
507 char *attr = NULL;
508 const char *trans = NULL;
a5fb4d23 509
f153614c 510
28ffc2b7 511 /* SCSI - Serial Peripheral Interface */
f153614c 512 if (sysfs_blkdev_scsi_host_is(sysfs, "spi"))
2f2f7e80 513 trans = "spi";
a5fb4d23
MB
514
515 /* FC/FCoE - Fibre Channel / Fibre Channel over Ethernet */
f153614c
KZ
516 else if (sysfs_blkdev_scsi_host_is(sysfs, "fc")) {
517 attr = sysfs_blkdev_scsi_host_strdup_attribute(sysfs, "fc", "symbolic_name");
23a11c74 518 if (!attr)
a5fb4d23 519 return NULL;
2f2f7e80
KZ
520 trans = strstr(attr, " over ") ? "fcoe" : "fc";
521 free(attr);
a5fb4d23
MB
522 }
523
524 /* SAS - Serial Attached SCSI */
f153614c
KZ
525 else if (sysfs_blkdev_scsi_host_is(sysfs, "sas") ||
526 sysfs_blkdev_scsi_has_attribute(sysfs, "sas_device"))
2f2f7e80 527 trans = "sas";
a5fb4d23 528
a5fb4d23
MB
529
530 /* SBP - Serial Bus Protocol (FireWire) */
f153614c 531 else if (sysfs_blkdev_scsi_has_attribute(sysfs, "ieee1394_id"))
2f2f7e80 532 trans = "sbp";
a5fb4d23
MB
533
534 /* iSCSI */
f153614c 535 else if (sysfs_blkdev_scsi_host_is(sysfs, "iscsi"))
2f2f7e80 536 trans ="iscsi";
a5fb4d23
MB
537
538 /* USB - Universal Serial Bus */
f153614c 539 else if (sysfs_blkdev_scsi_path_contains(sysfs, "usb"))
2f2f7e80 540 trans = "usb";
a5fb4d23
MB
541
542 /* ATA, SATA */
f153614c
KZ
543 else if (sysfs_blkdev_scsi_host_is(sysfs, "scsi")) {
544 attr = sysfs_blkdev_scsi_host_strdup_attribute(sysfs, "scsi", "proc_name");
23a11c74 545 if (!attr)
a5fb4d23 546 return NULL;
2f2f7e80
KZ
547 if (!strncmp(attr, "ahci", 4) || !strncmp(attr, "sata", 4))
548 trans = "sata";
549 else if (strstr(attr, "ata"))
550 trans = "ata";
551 free(attr);
3d305170 552
0540d076 553 } else if (strncmp(dev->name, "nvme", 4) == 0) {
3d305170 554 trans = "nvme";
0540d076 555 } else if (strncmp(dev->name, "vd", 2) == 0)
556 trans = "virtio";
62491dc1
TW
557 else if (strncmp(dev->name, "mmcblk", 6) == 0)
558 trans = "mmc";
a5fb4d23 559
3769f26e 560 return trans;
a5fb4d23
MB
561}
562
fed34a1e 563static char *get_subsystems(struct lsblk_device *dev)
7f14ee1b
KZ
564{
565 char path[PATH_MAX];
566 char *sub, *chain, *res = NULL;
567 size_t len = 0, last = 0;
568
fed34a1e 569 chain = sysfs_blkdev_get_devchain(dev->sysfs, path, sizeof(path));
7f14ee1b
KZ
570 if (!chain)
571 return NULL;
572
fed34a1e 573 while (sysfs_blkdev_next_subsystem(dev->sysfs, chain, &sub) == 0) {
7f14ee1b
KZ
574 size_t sz;
575
576 /* don't create "block:scsi:scsi", but "block:scsi" */
56e78cb5
AH
577 if (len && strcmp(res + last, sub) == 0) {
578 free(sub);
7f14ee1b 579 continue;
56e78cb5 580 }
7f14ee1b
KZ
581
582 sz = strlen(sub);
583 res = xrealloc(res, len + sz + 2);
584 if (len)
585 res[len++] = ':';
586
587 memcpy(res + len, sub, sz + 1);
588 last = len;
589 len += sz;
590 free(sub);
591 }
592
593 return res;
594}
595
596
9bd4e5c0 597#define is_parsable(_l) (scols_table_is_raw((_l)->table) || \
4a102a48
KZ
598 scols_table_is_export((_l)->table) || \
599 scols_table_is_json((_l)->table))
7c9c872c 600
c7e76cd1
KZ
601static char *mk_name(const char *name)
602{
603 char *p;
604 if (!name)
605 return NULL;
606 if (lsblk->paths)
607 xasprintf(&p, "/dev/%s", name);
608 else
609 p = xstrdup(name);
92441d5c
KZ
610 if (p)
611 sysfs_devname_sys_to_dev(p);
c7e76cd1
KZ
612 return p;
613}
614
615static char *mk_dm_name(const char *name)
616{
617 char *p;
618 if (!name)
619 return NULL;
620 if (lsblk->paths)
621 xasprintf(&p, "/dev/mapper/%s", name);
622 else
623 p = xstrdup(name);
624 return p;
625}
626
642048e4
KZ
627/* stores data to scols cell userdata (invisible and independent on output)
628 * to make the original values accessible for sort functions
629 */
b816c845 630static void set_rawdata_u64(struct libscols_line *ln, int col, uint64_t x)
642048e4
KZ
631{
632 struct libscols_cell *ce = scols_line_get_cell(ln, col);
633 uint64_t *data;
634
635 if (!ce)
636 return;
637 data = xmalloc(sizeof(uint64_t));
638 *data = x;
639 scols_cell_set_userdata(ce, data);
640}
641
d81e32bd
KZ
642/* do not modify *data on any error */
643static void str2u64(const char *str, uint64_t *data)
642048e4 644{
d81e32bd
KZ
645 uintmax_t num;
646 char *end = NULL;
642048e4 647
d81e32bd
KZ
648 errno = 0;
649 if (str == NULL || *str == '\0')
642048e4 650 return;
d81e32bd 651 num = strtoumax(str, &end, 10);
642048e4 652
d81e32bd
KZ
653 if (errno || str == end || (end && *end))
654 return;
655 *data = num;
642048e4
KZ
656}
657
1aca4989
KZ
658static void unref_line_rawdata(struct libscols_line *ln, struct libscols_table *tb)
659{
660 size_t i;
661
662 for (i = 0; i < ncolumns; i++) {
663 struct libscols_column *cl = scols_table_get_column(tb, i);
664 struct libscols_cell *ce;
665 void *data;
666
667 if (cl != lsblk->sort_col && !scols_column_has_data_func(cl))
668 continue;
669
670 ce = scols_line_get_column_cell(ln, cl);
671 data = scols_cell_get_userdata(ce);
672 free(data);
673 }
674}
675
676static void unref_table_rawdata(struct libscols_table *tb)
642048e4
KZ
677{
678 struct libscols_iter *itr;
679 struct libscols_line *ln;
680
af5fa636 681 if (!tb || !lsblk->rawdata)
642048e4 682 return;
af5fa636 683
642048e4
KZ
684 itr = scols_new_iter(SCOLS_ITER_FORWARD);
685 if (!itr)
686 return;
1aca4989
KZ
687 while (scols_table_next_line(tb, itr, &ln) == 0)
688 unref_line_rawdata(ln, tb);
642048e4
KZ
689
690 scols_free_iter(itr);
691}
692
fed34a1e 693static char *get_vfs_attribute(struct lsblk_device *dev, int id)
bc405d64
KZ
694{
695 char *sizestr;
696 uint64_t vfs_attr = 0;
bc405d64 697
fed34a1e 698 if (!dev->fsstat.f_blocks) {
c6648d2d 699 const char *mnt = lsblk_device_get_mountpoint(dev);
fed34a1e 700 if (!mnt || dev->is_swap)
bc405d64 701 return NULL;
fed34a1e 702 if (statvfs(mnt, &dev->fsstat) != 0)
bc405d64
KZ
703 return NULL;
704 }
705
706 switch(id) {
707 case COL_FSSIZE:
fed34a1e 708 vfs_attr = dev->fsstat.f_frsize * dev->fsstat.f_blocks;
bc405d64
KZ
709 break;
710 case COL_FSAVAIL:
fed34a1e 711 vfs_attr = dev->fsstat.f_frsize * dev->fsstat.f_bavail;
bc405d64
KZ
712 break;
713 case COL_FSUSED:
fed34a1e 714 vfs_attr = dev->fsstat.f_frsize * (dev->fsstat.f_blocks - dev->fsstat.f_bfree);
bc405d64
KZ
715 break;
716 case COL_FSUSEPERC:
fed34a1e 717 if (dev->fsstat.f_blocks == 0)
bc405d64
KZ
718 return xstrdup("-");
719
720 xasprintf(&sizestr, "%.0f%%",
fed34a1e
KZ
721 (double)(dev->fsstat.f_blocks - dev->fsstat.f_bfree) /
722 dev->fsstat.f_blocks * 100);
bc405d64
KZ
723 return sizestr;
724 }
725
726 if (!vfs_attr)
727 sizestr = xstrdup("0");
728 else if (lsblk->bytes)
729 xasprintf(&sizestr, "%ju", vfs_attr);
730 else
731 sizestr = size_to_human_string(SIZE_SUFFIX_1LETTER, vfs_attr);
732
733 return sizestr;
734}
735
fed34a1e 736static struct stat *device_get_stat(struct lsblk_device *dev)
652cb1cd 737{
404eef65
KZ
738 if (!dev->st.st_rdev
739 && stat(dev->filename, &dev->st) != 0)
740 return NULL;
652cb1cd 741
fed34a1e 742 return &dev->st;
652cb1cd 743}
bc405d64 744
81a8936c
KZ
745static int is_removable_device(struct lsblk_device *dev, struct lsblk_device *parent)
746{
747 struct path_cxt *pc;
748
749 if (dev->removable != -1)
750 goto done;
81a8936c 751
60c3686b
KZ
752 dev->removable = sysfs_blkdev_is_removable(dev->sysfs);
753
754 if (!dev->removable && parent) {
81a8936c
KZ
755 pc = sysfs_blkdev_get_parent(dev->sysfs);
756 if (!pc)
757 goto done;
758
759 if (pc == parent->sysfs)
60c3686b 760 /* dev is partition and parent is whole-disk */
81a8936c 761 dev->removable = is_removable_device(parent, NULL);
60c3686b
KZ
762 else
763 /* parent is something else, use sysfs parent */
764 dev->removable = sysfs_blkdev_is_removable(pc);
81a8936c 765 }
81a8936c
KZ
766done:
767 if (dev->removable == -1)
768 dev->removable = 0;
769 return dev->removable;
770}
771
8a2f175c
KZ
772static uint64_t device_get_discard_granularity(struct lsblk_device *dev)
773{
774 if (dev->discard_granularity == (uint64_t) -1
775 && ul_path_read_u64(dev->sysfs, &dev->discard_granularity,
776 "queue/discard_granularity") != 0)
777 dev->discard_granularity = 0;
778
779 return dev->discard_granularity;
780}
781
abb6e8e6 782static void device_read_bytes(struct lsblk_device *dev, char *path, char **str,
b816c845 783 uint64_t *rawdata)
abb6e8e6
NA
784{
785 uint64_t x;
786
787 if (lsblk->bytes) {
788 ul_path_read_string(dev->sysfs, str, path);
b816c845
KZ
789 if (rawdata)
790 str2u64(*str, rawdata);
abb6e8e6
NA
791 return;
792 }
793
794 if (ul_path_read_u64(dev->sysfs, &x, path) == 0) {
795 *str = size_to_human_string(SIZE_SUFFIX_1LETTER, x);
b816c845
KZ
796 if (rawdata)
797 *rawdata = x;
abb6e8e6
NA
798 }
799}
800
3b262733 801static void process_mq(struct lsblk_device *dev, char **str)
802{
3b262733 803 unsigned int queues = 0;
804
805 DBG(DEV, ul_debugobj(dev, "%s: process mq", dev->name));
806
26f6fa2b
KZ
807 queues = ul_path_count_dirents(dev->sysfs, "mq");
808 if (!queues) {
3b262733 809 *str = xstrdup("1");
810 DBG(DEV, ul_debugobj(dev, "%s: no mq supported, use a single queue", dev->name));
811 return;
812 }
813
3b262733 814 DBG(DEV, ul_debugobj(dev, "%s: has %d queues", dev->name, queues));
815 xasprintf(str, "%3u", queues);
816}
817
b93585e8 818/*
b816c845 819 * Generates data (string) for column specified by column ID for specified device. If rawdata
d81e32bd
KZ
820 * is not NULL then returns number usable to sort the column if the data are available for the
821 * column.
b93585e8 822 */
d81e32bd
KZ
823static char *device_get_data(
824 struct lsblk_device *dev, /* device */
825 struct lsblk_device *parent, /* device parent as defined in the tree */
826 int id, /* column ID (COL_*) */
b816c845 827 uint64_t *rawdata, /* returns sort data as number */
2eab8ab4 828 size_t *datasiz)
2a4c734b 829{
6f74ede5 830 struct lsblk_devprop *prop = NULL;
bb6e822a 831 char *str = NULL;
2a4c734b 832
2a4c734b
MB
833 switch(id) {
834 case COL_NAME:
fed34a1e 835 str = dev->dm_name ? mk_dm_name(dev->dm_name) : mk_name(dev->name);
c7e76cd1 836 break;
2a4c734b 837 case COL_KNAME:
fed34a1e 838 str = mk_name(dev->name);
2a4c734b 839 break;
310c0603 840 case COL_PKNAME:
ae4c2c37
KZ
841 if (parent)
842 str = mk_name(parent->name);
310c0603 843 break;
66638b0b 844 case COL_PATH:
fed34a1e
KZ
845 if (dev->filename)
846 str = xstrdup(dev->filename);
66638b0b 847 break;
2a4c734b 848 case COL_OWNER:
6f74ede5
KZ
849 if (lsblk->sysroot)
850 prop = lsblk_device_get_properties(dev);
851 if (prop && prop->owner) {
852 str = xstrdup(prop->owner);
853 } else {
854 struct stat *st = device_get_stat(dev);
855 struct passwd *pw = st ? getpwuid(st->st_uid) : NULL;
856 if (pw)
857 str = xstrdup(pw->pw_name);
858 }
2a4c734b 859 break;
2a4c734b 860 case COL_GROUP:
6f74ede5
KZ
861 if (lsblk->sysroot)
862 prop = lsblk_device_get_properties(dev);
863 if (prop && prop->group) {
864 str = xstrdup(prop->group);
865 } else {
866 struct stat *st = device_get_stat(dev);
867 struct group *gr = st ? getgrgid(st->st_gid) : NULL;
868 if (gr)
869 str = xstrdup(gr->gr_name);
870 }
2a4c734b 871 break;
2a4c734b 872 case COL_MODE:
6f74ede5
KZ
873 if (lsblk->sysroot)
874 prop = lsblk_device_get_properties(dev);
875 if (prop && prop->mode) {
876 str = xstrdup(prop->mode);
877 } else {
878 struct stat *st = device_get_stat(dev);
879 char md[11] = { '\0' };
880
881 if (st)
882 str = xstrdup(xstrmode(st->st_mode, md));
883 }
2a4c734b 884 break;
2a4c734b 885 case COL_MAJMIN:
7c9c872c 886 if (is_parsable(lsblk))
fed34a1e 887 xasprintf(&str, "%u:%u", dev->maj, dev->min);
2a4c734b 888 else
fed34a1e 889 xasprintf(&str, "%3u:%-3u", dev->maj, dev->min);
b816c845
KZ
890 if (rawdata)
891 *rawdata = makedev(dev->maj, dev->min);
2a4c734b 892 break;
0dbb3e06
KZ
893 case COL_MAJ:
894 xasprintf(&str, "%u", dev->maj);
bf9a7c54
KZ
895 if (rawdata)
896 *rawdata = dev->maj;
0dbb3e06
KZ
897 break;
898 case COL_MIN:
899 xasprintf(&str, "%u", dev->min);
bf9a7c54
KZ
900 if (rawdata)
901 *rawdata = dev->min;
0dbb3e06 902 break;
2a4c734b 903 case COL_FSTYPE:
fed34a1e 904 prop = lsblk_device_get_properties(dev);
baad6dcc
KZ
905 if (prop && prop->fstype)
906 str = xstrdup(prop->fstype);
2a4c734b 907 break;
bc405d64
KZ
908 case COL_FSSIZE:
909 case COL_FSAVAIL:
910 case COL_FSUSED:
911 case COL_FSUSEPERC:
fed34a1e 912 str = get_vfs_attribute(dev, id);
bc405d64 913 break;
9cca1ef2
KZ
914 case COL_FSVERSION:
915 prop = lsblk_device_get_properties(dev);
916 if (prop && prop->fsversion)
917 str = xstrdup(prop->fsversion);
918 break;
2a4c734b 919 case COL_TARGET:
f3aded3f 920 {
c6648d2d
KZ
921 const char *p = lsblk_device_get_mountpoint(dev);
922 if (p)
923 str = xstrdup(p);
924 break;
925 }
926 case COL_TARGETS:
927 {
928 size_t i, n = 0;
929 struct ul_buffer buf = UL_INIT_BUFFER;
930 struct libmnt_fs **fss = lsblk_device_get_filesystems(dev, &n);
931
932 for (i = 0; i < n; i++) {
933 struct libmnt_fs *fs = fss[i];
934 if (mnt_fs_is_swaparea(fs))
935 ul_buffer_append_string(&buf, "[SWAP]");
936 else
937 ul_buffer_append_string(&buf, mnt_fs_get_target(fs));
af4c7dac 938 ul_buffer_append_data(&buf, "\0", 1);
c6648d2d 939 }
2eab8ab4 940 str = ul_buffer_get_data(&buf, datasiz, NULL);
2a4c734b 941 break;
f3aded3f 942 }
37da403d
KZ
943 case COL_FSROOTS:
944 {
945 size_t i, n = 0;
946 struct ul_buffer buf = UL_INIT_BUFFER;
947 struct libmnt_fs **fss = lsblk_device_get_filesystems(dev, &n);
948
949 for (i = 0; i < n; i++) {
950 struct libmnt_fs *fs = fss[i];
951 const char *root = mnt_fs_get_root(fs);
952 if (mnt_fs_is_swaparea(fs))
953 continue;
954 ul_buffer_append_string(&buf, root ? root : "/");
af4c7dac 955 ul_buffer_append_data(&buf, "\0", 1);
37da403d 956 }
2eab8ab4 957 str = ul_buffer_get_data(&buf, datasiz, NULL);
37da403d
KZ
958 break;
959 }
2a4c734b 960 case COL_LABEL:
fed34a1e 961 prop = lsblk_device_get_properties(dev);
baad6dcc
KZ
962 if (prop && prop->label)
963 str = xstrdup(prop->label);
2a4c734b
MB
964 break;
965 case COL_UUID:
fed34a1e 966 prop = lsblk_device_get_properties(dev);
baad6dcc
KZ
967 if (prop && prop->uuid)
968 str = xstrdup(prop->uuid);
2a4c734b 969 break;
1b06b33d 970 case COL_PTUUID:
fed34a1e 971 prop = lsblk_device_get_properties(dev);
baad6dcc
KZ
972 if (prop && prop->ptuuid)
973 str = xstrdup(prop->ptuuid);
1b06b33d
MB
974 break;
975 case COL_PTTYPE:
fed34a1e 976 prop = lsblk_device_get_properties(dev);
baad6dcc
KZ
977 if (prop && prop->pttype)
978 str = xstrdup(prop->pttype);
1b06b33d 979 break;
4686ba17 980 case COL_PARTTYPE:
fed34a1e 981 prop = lsblk_device_get_properties(dev);
baad6dcc
KZ
982 if (prop && prop->parttype)
983 str = xstrdup(prop->parttype);
4686ba17 984 break;
107e9559
KZ
985 case COL_PARTTYPENAME:
986 prop = lsblk_device_get_properties(dev);
987 if (prop && prop->parttype && prop->pttype) {
988 const char *x = lsblk_parttype_code_to_string(
989 prop->parttype, prop->pttype);
990 if (x)
991 str = xstrdup(x);
992 }
993 break;
aa0903e0 994 case COL_PARTLABEL:
fed34a1e 995 prop = lsblk_device_get_properties(dev);
baad6dcc
KZ
996 if (prop && prop->partlabel)
997 str = xstrdup(prop->partlabel);
aa0903e0
KZ
998 break;
999 case COL_PARTUUID:
fed34a1e 1000 prop = lsblk_device_get_properties(dev);
baad6dcc
KZ
1001 if (prop && prop->partuuid)
1002 str = xstrdup(prop->partuuid);
aa0903e0 1003 break;
5a2fd932 1004 case COL_PARTFLAGS:
fed34a1e 1005 prop = lsblk_device_get_properties(dev);
baad6dcc
KZ
1006 if (prop && prop->partflags)
1007 str = xstrdup(prop->partflags);
5a2fd932 1008 break;
e4ed8665
KZ
1009 case COL_PARTN:
1010 prop = lsblk_device_get_properties(dev);
1011 if (prop && prop->partn)
1012 str = xstrdup(prop->partn);
1013 break;
88ca32b3 1014 case COL_WWN:
fed34a1e 1015 prop = lsblk_device_get_properties(dev);
baad6dcc
KZ
1016 if (prop && prop->wwn)
1017 str = xstrdup(prop->wwn);
88ca32b3 1018 break;
e054895f 1019 case COL_IDLINK:
ef7d50a0 1020 prop = lsblk_device_get_properties(dev);
e054895f
KZ
1021 if (prop && prop->idlink)
1022 str = xstrdup(prop->idlink);
ef7d50a0 1023 break;
875d6a31
KZ
1024 case COL_ID:
1025 prop = lsblk_device_get_properties(dev);
1026 if (prop && prop->idlink) {
1027 /* skip bus/subsystem prefix */
1028 const char *p = strchr(prop->idlink, '-');
1029 str = p && *(p + 1) ? xstrdup(p+1) : xstrdup(prop->idlink);
1030 }
1031 break;
150db7a9 1032 case COL_RA:
fed34a1e 1033 ul_path_read_string(dev->sysfs, &str, "queue/read_ahead_kb");
b816c845
KZ
1034 if (rawdata)
1035 str2u64(str, rawdata);
150db7a9 1036 break;
2a4c734b 1037 case COL_RO:
fed34a1e 1038 str = xstrdup(is_readonly_device(dev) ? "1" : "0");
2a4c734b 1039 break;
627970a7 1040 case COL_RM:
81a8936c 1041 str = xstrdup(is_removable_device(dev, parent) ? "1" : "0");
2a4c734b 1042 break;
483987c2 1043 case COL_HOTPLUG:
fed34a1e 1044 str = sysfs_blkdev_is_hotpluggable(dev->sysfs) ? xstrdup("1") : xstrdup("0");
483987c2 1045 break;
2a4c734b 1046 case COL_ROTA:
fed34a1e 1047 ul_path_read_string(dev->sysfs, &str, "queue/rotational");
2a4c734b 1048 break;
12b06c3d 1049 case COL_RAND:
fed34a1e 1050 ul_path_read_string(dev->sysfs, &str, "queue/add_random");
12b06c3d 1051 break;
2a4c734b 1052 case COL_MODEL:
10501add 1053 if (!device_is_partition(dev) && dev->nslaves == 0) {
fed34a1e 1054 prop = lsblk_device_get_properties(dev);
baad6dcc
KZ
1055 if (prop && prop->model)
1056 str = xstrdup(prop->model);
13cbc6f2 1057 else
fed34a1e 1058 ul_path_read_string(dev->sysfs, &str, "device/model");
13cbc6f2 1059 }
d6681cee 1060 break;
460c7afb 1061 case COL_SERIAL:
10501add 1062 if (!device_is_partition(dev) && dev->nslaves == 0) {
fed34a1e 1063 prop = lsblk_device_get_properties(dev);
baad6dcc
KZ
1064 if (prop && prop->serial)
1065 str = xstrdup(prop->serial);
3d305170 1066 else
fed34a1e 1067 ul_path_read_string(dev->sysfs, &str, "device/serial");
460c7afb
KZ
1068 }
1069 break;
d6681cee 1070 case COL_REV:
8e89dd8b
MB
1071 if (!device_is_partition(dev) && dev->nslaves == 0) {
1072 prop = lsblk_device_get_properties(dev);
1073 if (prop && prop->revision)
1074 str = xstrdup(prop->revision);
1075 else
1076 ul_path_read_string(dev->sysfs, &str, "device/rev");
1077 }
99f43b72
MB
1078 break;
1079 case COL_VENDOR:
10501add 1080 if (!device_is_partition(dev) && dev->nslaves == 0)
fed34a1e 1081 ul_path_read_string(dev->sysfs, &str, "device/vendor");
2a4c734b
MB
1082 break;
1083 case COL_SIZE:
bb6e822a 1084 if (lsblk->bytes)
fed34a1e 1085 xasprintf(&str, "%ju", dev->size);
bb6e822a 1086 else
fed34a1e 1087 str = size_to_human_string(SIZE_SUFFIX_1LETTER, dev->size);
b816c845
KZ
1088 if (rawdata)
1089 *rawdata = dev->size;
2a4c734b 1090 break;
856457bf
KZ
1091 case COL_START:
1092 ul_path_read_string(dev->sysfs, &str, "start");
b816c845
KZ
1093 if (rawdata)
1094 str2u64(str, rawdata);
856457bf 1095 break;
01e487c0 1096 case COL_STATE:
10501add 1097 if (!device_is_partition(dev) && !dev->dm_name)
fed34a1e
KZ
1098 ul_path_read_string(dev->sysfs, &str, "device/state");
1099 else if (dev->dm_name) {
01e487c0 1100 int x = 0;
fed34a1e 1101 if (ul_path_read_s32(dev->sysfs, &x, "dm/suspended") == 0)
bb6e822a 1102 str = xstrdup(x ? "suspended" : "running");
01e487c0 1103 }
01e487c0 1104 break;
2a4c734b 1105 case COL_ALIOFF:
fed34a1e 1106 ul_path_read_string(dev->sysfs, &str, "alignment_offset");
b816c845
KZ
1107 if (rawdata)
1108 str2u64(str, rawdata);
2a4c734b
MB
1109 break;
1110 case COL_MINIO:
fed34a1e 1111 ul_path_read_string(dev->sysfs, &str, "queue/minimum_io_size");
b816c845
KZ
1112 if (rawdata)
1113 str2u64(str, rawdata);
2a4c734b
MB
1114 break;
1115 case COL_OPTIO:
fed34a1e 1116 ul_path_read_string(dev->sysfs, &str, "queue/optimal_io_size");
b816c845
KZ
1117 if (rawdata)
1118 str2u64(str, rawdata);
2a4c734b
MB
1119 break;
1120 case COL_PHYSEC:
fed34a1e 1121 ul_path_read_string(dev->sysfs, &str, "queue/physical_block_size");
b816c845
KZ
1122 if (rawdata)
1123 str2u64(str, rawdata);
2a4c734b
MB
1124 break;
1125 case COL_LOGSEC:
fed34a1e 1126 ul_path_read_string(dev->sysfs, &str, "queue/logical_block_size");
b816c845
KZ
1127 if (rawdata)
1128 str2u64(str, rawdata);
2a4c734b
MB
1129 break;
1130 case COL_SCHED:
fed34a1e 1131 str = get_scheduler(dev);
2a4c734b 1132 break;
64c9e22a 1133 case COL_RQ_SIZE:
fed34a1e 1134 ul_path_read_string(dev->sysfs, &str, "queue/nr_requests");
b816c845
KZ
1135 if (rawdata)
1136 str2u64(str, rawdata);
64c9e22a 1137 break;
18eac5ba 1138 case COL_TYPE:
fed34a1e 1139 str = get_type(dev);
18eac5ba 1140 break;
699e5c4f
MB
1141 case COL_HCTL:
1142 {
1143 int h, c, t, l;
fed34a1e 1144 if (sysfs_blkdev_scsi_get_hctl(dev->sysfs, &h, &c, &t, &l) == 0)
bb6e822a 1145 xasprintf(&str, "%d:%d:%d:%d", h, c, t, l);
699e5c4f
MB
1146 break;
1147 }
a5fb4d23 1148 case COL_TRANSPORT:
3769f26e
KZ
1149 {
1150 const char *trans = get_transport(dev);
1151 if (trans)
1152 str = xstrdup(trans);
a5fb4d23 1153 break;
3769f26e 1154 }
7f14ee1b 1155 case COL_SUBSYS:
fed34a1e 1156 str = get_subsystems(dev);
7f14ee1b 1157 break;
2d232246 1158 case COL_DALIGN:
8a2f175c 1159 if (device_get_discard_granularity(dev) > 0)
fed34a1e 1160 ul_path_read_string(dev->sysfs, &str, "discard_alignment");
bb6e822a
KZ
1161 if (!str)
1162 str = xstrdup("0");
b816c845
KZ
1163 if (rawdata)
1164 str2u64(str, rawdata);
2d232246
MP
1165 break;
1166 case COL_DGRAN:
642048e4 1167 if (lsblk->bytes) {
fed34a1e 1168 ul_path_read_string(dev->sysfs, &str, "queue/discard_granularity");
b816c845
KZ
1169 if (rawdata)
1170 str2u64(str, rawdata);
642048e4 1171 } else {
8a2f175c
KZ
1172 uint64_t x = device_get_discard_granularity(dev);
1173 str = size_to_human_string(SIZE_SUFFIX_1LETTER, x);
b816c845
KZ
1174 if (rawdata)
1175 *rawdata = x;
9cfe1b9c 1176 }
2d232246
MP
1177 break;
1178 case COL_DMAX:
b816c845 1179 device_read_bytes(dev, "queue/discard_max_bytes", &str, rawdata);
2d232246
MP
1180 break;
1181 case COL_DZERO:
8a2f175c 1182 if (device_get_discard_granularity(dev) > 0)
fed34a1e 1183 ul_path_read_string(dev->sysfs, &str, "queue/discard_zeroes_data");
bb6e822a
KZ
1184 if (!str)
1185 str = xstrdup("0");
2d232246 1186 break;
2adb1a44 1187 case COL_WSAME:
b816c845 1188 device_read_bytes(dev, "queue/write_same_max_bytes", &str, rawdata);
bb6e822a
KZ
1189 if (!str)
1190 str = xstrdup("0");
2adb1a44 1191 break;
f2df4365 1192 case COL_ZONED:
fed34a1e 1193 ul_path_read_string(dev->sysfs, &str, "queue/zoned");
f2df4365 1194 break;
ca5f4bd0
NA
1195 case COL_ZONE_SZ:
1196 {
1197 uint64_t x;
1198
1199 if (ul_path_read_u64(dev->sysfs, &x, "queue/chunk_sectors") == 0) {
1200 x <<= 9;
1201 if (lsblk->bytes)
1202 xasprintf(&str, "%ju", x);
1203 else
1204 str = size_to_human_string(SIZE_SUFFIX_1LETTER, x);
b816c845
KZ
1205 if (rawdata)
1206 *rawdata = x;
ca5f4bd0
NA
1207 }
1208 break;
1209 }
1210 case COL_ZONE_WGRAN:
b816c845 1211 device_read_bytes(dev, "queue/zone_write_granularity", &str, rawdata);
ca5f4bd0
NA
1212 break;
1213 case COL_ZONE_APP:
b816c845 1214 device_read_bytes(dev, "queue/zone_append_max_bytes", &str, rawdata);
ca5f4bd0
NA
1215 break;
1216 case COL_ZONE_NR:
1217 ul_path_read_string(dev->sysfs, &str, "queue/nr_zones");
b816c845
KZ
1218 if (rawdata)
1219 str2u64(str, rawdata);
ca5f4bd0
NA
1220 break;
1221 case COL_ZONE_OMAX:
1222 ul_path_read_string(dev->sysfs, &str, "queue/max_open_zones");
1223 if (!str)
1224 str = xstrdup("0");
b816c845
KZ
1225 if (rawdata)
1226 str2u64(str, rawdata);
ca5f4bd0
NA
1227 break;
1228 case COL_ZONE_AMAX:
1229 ul_path_read_string(dev->sysfs, &str, "queue/max_active_zones");
1230 if (!str)
1231 str = xstrdup("0");
b816c845
KZ
1232 if (rawdata)
1233 str2u64(str, rawdata);
ca5f4bd0 1234 break;
b239651b
AI
1235 case COL_DAX:
1236 ul_path_read_string(dev->sysfs, &str, "queue/dax");
1237 break;
3b262733 1238 case COL_MQ:
1239 process_mq(dev, &str);
1240 break;
36e0914f
KZ
1241 case COL_DISKSEQ:
1242 ul_path_read_string(dev->sysfs, &str, "diskseq");
b816c845
KZ
1243 if (rawdata)
1244 str2u64(str, rawdata);
36e0914f 1245 break;
2a4c734b 1246 };
bb6e822a 1247
d81e32bd 1248 return str;
2a4c734b
MB
1249}
1250
3e0b4a82
KZ
1251static void device_fill_scols_cell(struct lsblk_device *dev,
1252 struct lsblk_device *parent,
1253 struct libscols_line *ln,
1254 size_t colnum)
1255{
1256 struct libscols_cell *ce;
af5fa636 1257 struct libscols_column *cl = scols_table_get_column(lsblk->table, colnum);
3e0b4a82
KZ
1258 char *data;
1259 size_t datasiz = 0;
1260 int rc, id = get_column_id(colnum);
1261
b0df377d 1262 if (lsblk->sort_id == id || scols_column_has_data_func(cl)) {
b816c845 1263 uint64_t rawdata = (uint64_t) -1;
3e0b4a82 1264
b816c845
KZ
1265 data = device_get_data(dev, parent, id, &rawdata, &datasiz);
1266 if (data && rawdata != (uint64_t) -1)
1267 set_rawdata_u64(ln, colnum, rawdata);
af5fa636
KZ
1268 } else
1269 data = device_get_data(dev, parent, id, NULL, &datasiz);
3e0b4a82
KZ
1270
1271 if (!data)
1272 return;
1273 DBG(DEV, ul_debugobj(dev, " refer data[%zu]=\"%s\"", colnum, data));
1274 ce = scols_line_get_cell(ln, colnum);
1275 if (!ce)
1276 return;
af4c7dac 1277 rc = datasiz ? scols_cell_refer_memory(ce, data, datasiz)
3e0b4a82
KZ
1278 : scols_cell_refer_data(ce, data);
1279 if (rc)
1280 err(EXIT_FAILURE, _("failed to add output data"));
1281}
1282
1283static int filter_filler_cb(
1284 struct libscols_filter *fltr __attribute__((__unused__)),
1285 struct libscols_line *ln,
1286 size_t colnum,
1287 void *userdata)
1288{
1289 struct filler_data *fid = (struct filler_data *) userdata;
1290
1291 device_fill_scols_cell(fid->dev, fid->parent, ln, colnum);
1292 return 0;
1293}
1294
b93585e8
KZ
1295/*
1296 * Adds data for all wanted columns about the device to the smartcols table
1297 */
d81e32bd
KZ
1298static void device_to_scols(
1299 struct lsblk_device *dev,
1300 struct lsblk_device *parent,
1301 struct libscols_table *tab,
1302 struct libscols_line *parent_line)
2a4c734b 1303{
40b17508 1304 size_t i;
d81e32bd 1305 struct libscols_line *ln;
f43b8297
KZ
1306 struct lsblk_iter itr;
1307 struct lsblk_device *child = NULL;
7de6a8a9 1308 int link_group = 0, nocount = 0;
2a4c734b 1309
fe22543d
KZ
1310
1311 DBG(DEV, ul_debugobj(dev, "add '%s' to scols", dev->name));
1312 ON_DBG(DEV, if (ul_path_isopen_dirfd(dev->sysfs)) ul_debugobj(dev, " %s ---> is open!", dev->name));
ff7992d6 1313
e3bb9bfb
KZ
1314 if (!parent && dev->wholedisk)
1315 parent = dev->wholedisk;
1316
91b636b5
KZ
1317 /* Do not print device more than once on --list if tree order is not requested */
1318 if (!(lsblk->flags & LSBLK_TREE) && !lsblk->force_tree_order && dev->is_printed)
0bd05f5e
KZ
1319 return;
1320
1321 if (lsblk->merge && list_count_entries(&dev->parents) > 1) {
1322 if (!lsblk_device_is_last_parent(dev, parent))
1323 return;
1324 link_group = 1;
1325 }
1326
1327 ln = scols_table_new_line(tab, link_group ? NULL : parent_line);
d81e32bd 1328 if (!ln)
780ce22c 1329 err(EXIT_FAILURE, _("failed to allocate output line"));
2a4c734b 1330
0bd05f5e
KZ
1331 dev->is_printed = 1;
1332
3e0b4a82 1333 /* filter lines, smartcols filter can ask for data */
9f55de78 1334 if (lsblk->filter) {
3e0b4a82
KZ
1335 int status = 0;
1336 struct filler_data fid = {
1337 .dev = dev,
1338 .parent = parent
1339 };
1340
9f55de78 1341 scols_filter_set_filler_cb(lsblk->filter,
3e0b4a82
KZ
1342 filter_filler_cb, (void *) &fid);
1343
9f55de78 1344 if (scols_line_apply_filter(ln, lsblk->filter, &status))
3e0b4a82
KZ
1345 err(EXIT_FAILURE, _("failed to apply filter"));
1346 if (status == 0) {
1347 struct libscols_line *x = scols_line_get_parent(ln);
1348
1349 if (x)
1350 scols_line_remove_child(x, ln);
1aca4989 1351 unref_line_rawdata(ln, tab);
3e0b4a82 1352 scols_table_remove_line(tab, ln);
c4b78214 1353 ln = NULL;
3e0b4a82
KZ
1354 }
1355 }
1356
1357 /* read column specific data and set it to smartcols table line */
c4b78214 1358 for (i = 0; ln && i < ncolumns; i++) {
3e0b4a82
KZ
1359 if (scols_line_is_filled(ln, i))
1360 continue;
1361 device_fill_scols_cell(dev, parent, ln, i);
1362 }
1363
c4b78214 1364 if (ln && link_group) {
0bd05f5e
KZ
1365 struct lsblk_device *p;
1366 struct libscols_line *gr = parent_line;
1367
1368 /* Merge all my parents to the one group */
fe22543d 1369 DBG(DEV, ul_debugobj(dev, " grouping parents [--merge]"));
0bd05f5e
KZ
1370 lsblk_reset_iter(&itr, LSBLK_ITER_FORWARD);
1371 while (lsblk_device_next_parent(dev, &itr, &p) == 0) {
fe22543d
KZ
1372 if (!p->scols_line) {
1373 DBG(DEV, ul_debugobj(dev, " *** ignore '%s' no scols line yet", p->name));
0bd05f5e 1374 continue;
fe22543d
KZ
1375 }
1376 DBG(DEV, ul_debugobj(dev, " group '%s'", p->name));
1377 scols_table_group_lines(tab, p->scols_line, gr, 0);
0bd05f5e
KZ
1378 }
1379
1380 /* Link the group -- this makes group->child connection */
fe22543d 1381 DBG(DEV, ul_debugobj(dev, " linking the group [--merge]"));
0bd05f5e
KZ
1382 scols_line_link_group(ln, gr, 0);
1383 }
1384
7de6a8a9
KZ
1385 /* The same device could be printed more than once, don't use it in counter */
1386 if (dev->scols_line)
1387 nocount = 1;
f43b8297 1388
0bd05f5e
KZ
1389 dev->scols_line = ln;
1390
ff7992d6
KZ
1391 if (dev->npartitions == 0)
1392 /* For partitions we often read from parental whole-disk sysfs,
1393 * otherwise we can close */
1394 ul_path_close_dirfd(dev->sysfs);
1395
0bd05f5e 1396 lsblk_reset_iter(&itr, LSBLK_ITER_FORWARD);
fe22543d
KZ
1397 while (lsblk_device_next_child(dev, &itr, &child) == 0) {
1398 DBG(DEV, ul_debugobj(dev, "%s -> continue to child", dev->name));
d81e32bd 1399 device_to_scols(child, dev, tab, ln);
fe22543d
KZ
1400 DBG(DEV, ul_debugobj(dev, "%s <- child done", dev->name));
1401 }
ff7992d6 1402
0398951e
KZ
1403 /* apply highligther */
1404 if (ln && lsblk->hlighter) {
c3317cdc
KZ
1405 int status = 0;
1406
9f55de78 1407 if (scols_line_apply_filter(ln, lsblk->hlighter, &status) == 0
c3317cdc
KZ
1408 && status)
1409 scols_line_set_color(ln, lsblk->hlighter_seq);
1410 }
1411
0398951e 1412 /* apply counters */
7de6a8a9
KZ
1413 if (!nocount) {
1414 for (i = 0; ln && i < lsblk->ncts; i++)
1415 scols_line_apply_filter(ln, lsblk->ct_filters[i], NULL);
1416 }
0398951e 1417
ff7992d6
KZ
1418 /* Let's be careful with number of open files */
1419 ul_path_close_dirfd(dev->sysfs);
f43b8297
KZ
1420}
1421
b93585e8
KZ
1422/*
1423 * Walks on tree and adds one line for each device to the smartcols table
1424 */
f43b8297
KZ
1425static void devtree_to_scols(struct lsblk_devtree *tr, struct libscols_table *tab)
1426{
1427 struct lsblk_iter itr;
1428 struct lsblk_device *dev = NULL;
1429
1430 lsblk_reset_iter(&itr, LSBLK_ITER_FORWARD);
1431
1432 while (lsblk_devtree_next_root(tr, &itr, &dev) == 0)
d81e32bd 1433 device_to_scols(dev, NULL, tab, NULL);
2a4c734b
MB
1434}
1435
14bb8e3c
KZ
1436static int ignore_empty(struct lsblk_device *dev)
1437{
c90a8f1a
KZ
1438 /* show all non-empty devices */
1439 if (dev->size)
14bb8e3c 1440 return 0;
c90a8f1a 1441
554e866b
KZ
1442 if (lsblk->noempty && dev->size == 0)
1443 return 1;
1444
c90a8f1a
KZ
1445 /* ignore empty loop devices without backing file */
1446 if (dev->maj == LOOPDEV_MAJOR &&
1447 !loopdev_has_backing_file(dev->filename))
1448 return 1;
1449
1450 return 0;
14bb8e3c
KZ
1451}
1452
b93585e8
KZ
1453/*
1454 * Reads very basic information about the device from sysfs into the device struct
1455 */
b158bd29 1456static int initialize_device(struct lsblk_device *dev,
fed34a1e 1457 struct lsblk_device *wholedisk,
09a71aa1 1458 const char *name)
2a4c734b 1459{
766ab9b0 1460 dev_t devno;
2a4c734b 1461
b158bd29
KZ
1462 DBG(DEV, ul_debugobj(dev, "initialize %s [wholedisk=%p %s]",
1463 name, wholedisk, wholedisk ? wholedisk->name : ""));
7e786eca 1464
c8487d85
RS
1465 if (sysfs_devname_is_hidden(lsblk->sysroot, name)) {
1466 DBG(DEV, ul_debugobj(dev, "%s: hidden, ignore", name));
1467 return -1;
1468 }
1469
fed34a1e 1470 dev->name = xstrdup(name);
10501add
KZ
1471
1472 if (wholedisk) {
1473 dev->wholedisk = wholedisk;
1474 lsblk_ref_device(wholedisk);
1475 }
2a4c734b 1476
fed34a1e
KZ
1477 dev->filename = get_device_path(dev);
1478 if (!dev->filename) {
3bf2a36c 1479 DBG(DEV, ul_debugobj(dev, "%s: failed to get device path", dev->name));
270e66ec
MB
1480 return -1;
1481 }
3bf2a36c 1482 DBG(DEV, ul_debugobj(dev, "%s: filename=%s", dev->name, dev->filename));
2a4c734b 1483
fed34a1e 1484 devno = __sysfs_devname_to_devno(lsblk->sysroot, dev->name, wholedisk ? wholedisk->name : NULL);
270e66ec 1485 if (!devno) {
3bf2a36c 1486 DBG(DEV, ul_debugobj(dev, "%s: unknown device name", dev->name));
270e66ec
MB
1487 return -1;
1488 }
2a4c734b 1489
0e086f82
KZ
1490 dev->sysfs = ul_new_sysfs_path(devno, wholedisk ? wholedisk->sysfs : NULL, lsblk->sysroot);
1491 if (!dev->sysfs) {
1492 DBG(DEV, ul_debugobj(dev, "%s: failed to initialize sysfs handler", dev->name));
1493 return -1;
270e66ec 1494 }
766ab9b0 1495
fed34a1e
KZ
1496 dev->maj = major(devno);
1497 dev->min = minor(devno);
1498 dev->size = 0;
2a4c734b 1499
fed34a1e
KZ
1500 if (ul_path_read_u64(dev->sysfs, &dev->size, "size") == 0) /* in sectors */
1501 dev->size <<= 9; /* in bytes */
90e9fcda 1502
2a4c734b 1503 /* Ignore devices of zero size */
14bb8e3c 1504 if (!lsblk->all_devices && ignore_empty(dev)) {
3bf2a36c 1505 DBG(DEV, ul_debugobj(dev, "zero size device -- ignore"));
2a4c734b 1506 return -1;
7e786eca 1507 }
fed34a1e
KZ
1508 if (is_dm(dev->name)) {
1509 ul_path_read_string(dev->sysfs, &dev->dm_name, "dm/name");
1510 if (!dev->dm_name) {
3bf2a36c 1511 DBG(DEV, ul_debugobj(dev, "%s: failed to get dm name", dev->name));
270e66ec
MB
1512 return -1;
1513 }
766ab9b0 1514 }
2a4c734b 1515
fed34a1e
KZ
1516 dev->npartitions = sysfs_blkdev_count_partitions(dev->sysfs, dev->name);
1517 dev->nholders = ul_path_count_dirents(dev->sysfs, "holders");
1518 dev->nslaves = ul_path_count_dirents(dev->sysfs, "slaves");
2a4c734b 1519
3bf2a36c 1520 DBG(DEV, ul_debugobj(dev, "%s: npartitions=%d, nholders=%d, nslaves=%d",
fed34a1e 1521 dev->name, dev->npartitions, dev->nholders, dev->nslaves));
7e786eca 1522
28ffc2b7 1523 /* ignore non-SCSI devices */
fed34a1e 1524 if (lsblk->scsi && sysfs_blkdev_scsi_get_hctl(dev->sysfs, NULL, NULL, NULL, NULL)) {
3bf2a36c 1525 DBG(DEV, ul_debugobj(dev, "non-scsi device -- ignore"));
28ffc2b7 1526 return -1;
7e786eca 1527 }
28ffc2b7 1528
97afabf7 1529 /* ignore non-NVMe devices */
1530 if (lsblk->nvme) {
3769f26e 1531 const char *transport = get_transport(dev);
97afabf7 1532
1533 if (!transport || strcmp(transport, "nvme")) {
1534 DBG(DEV, ul_debugobj(dev, "non-nvme device -- ignore"));
1535 return -1;
1536 }
1537 }
1538
1b3e8f5d 1539 /* ignore non-virtio devices */
1540 if (lsblk->virtio) {
3769f26e 1541 const char *transport = get_transport(dev);
1b3e8f5d 1542
1543 if (!transport || strcmp(transport, "virtio")) {
1544 DBG(DEV, ul_debugobj(dev, "non-virtio device -- ignore"));
1545 return -1;
1546 }
1547 }
1548
3bf2a36c 1549 DBG(DEV, ul_debugobj(dev, "%s: context successfully initialized", dev->name));
2a4c734b
MB
1550 return 0;
1551}
1552
4ca3c472 1553static struct lsblk_device *devtree_get_device_or_new(struct lsblk_devtree *tr,
583b6489
KZ
1554 struct lsblk_device *disk,
1555 const char *name)
1556{
1557 struct lsblk_device *dev = lsblk_devtree_get_device(tr, name);
1558
1559 if (!dev) {
10501add 1560 dev = lsblk_new_device();
583b6489
KZ
1561 if (!dev)
1562 err(EXIT_FAILURE, _("failed to allocate device"));
1563
b158bd29 1564 if (initialize_device(dev, disk, name) != 0) {
583b6489
KZ
1565 lsblk_unref_device(dev);
1566 return NULL;
1567 }
1568 lsblk_devtree_add_device(tr, dev);
1569 lsblk_unref_device(dev); /* keep it referenced by devtree only */
1570 } else
1571 DBG(DEV, ul_debugobj(dev, "%s: already processed", name));
1572
1573 return dev;
1574}
a0a76f42 1575
94ebe1d3
KZ
1576static struct lsblk_device *devtree_pktcdvd_get_dep(
1577 struct lsblk_devtree *tr,
1578 struct lsblk_device *dev,
1579 int want_slave)
1580{
1581 char buf[PATH_MAX], *name;
1582 dev_t devno;
1583
1584 devno = lsblk_devtree_pktcdvd_get_mate(tr,
1585 makedev(dev->maj, dev->min), !want_slave);
1586 if (!devno)
1587 return NULL;
1588
1589 name = sysfs_devno_to_devname(devno, buf, sizeof(buf));
1590 if (!name)
1591 return NULL;
1592
1593 return devtree_get_device_or_new(tr, NULL, name);
1594}
1595
4ca3c472
KZ
1596static int process_dependencies(
1597 struct lsblk_devtree *tr,
1598 struct lsblk_device *dev,
1599 int do_partitions);
09a71aa1 1600
2a4c734b 1601/*
4ca3c472 1602 * Read devices from whole-disk device into tree
2a4c734b 1603 */
cf3c1bc7 1604static int process_partitions(struct lsblk_devtree *tr, struct lsblk_device *disk)
2a4c734b
MB
1605{
1606 DIR *dir;
1607 struct dirent *d;
2a4c734b 1608
cf3c1bc7 1609 assert(disk);
f31505fe 1610
09a71aa1
PR
1611 /*
1612 * Do not process further if there are no partitions for
1613 * this device or the device itself is a partition.
1614 */
10501add 1615 if (!disk->npartitions || device_is_partition(disk))
cf3c1bc7 1616 return -EINVAL;
2a4c734b 1617
cf3c1bc7 1618 DBG(DEV, ul_debugobj(disk, "%s: probe whole-disk for partitions", disk->name));
7e786eca 1619
cf3c1bc7 1620 dir = ul_path_opendir(disk->sysfs, NULL);
2a4c734b
MB
1621 if (!dir)
1622 err(EXIT_FAILURE, _("failed to open device directory in sysfs"));
1623
1624 while ((d = xreaddir(dir))) {
4ca3c472 1625 struct lsblk_device *part;
2a4c734b 1626
cf3c1bc7 1627 if (!(sysfs_blkdev_is_partition_dirent(dir, d, disk->name)))
270e66ec 1628 continue;
09a71aa1 1629
cf3c1bc7 1630 DBG(DEV, ul_debugobj(disk, " checking %s", d->d_name));
7e786eca 1631
cf3c1bc7 1632 part = devtree_get_device_or_new(tr, disk, d->d_name);
4ca3c472
KZ
1633 if (!part)
1634 continue;
1635
cf3c1bc7 1636 if (lsblk_device_new_dependence(disk, part) == 0)
4ca3c472 1637 process_dependencies(tr, part, 0);
ff7992d6
KZ
1638
1639 ul_path_close_dirfd(part->sysfs);
2a4c734b 1640 }
09a71aa1 1641
ff7992d6
KZ
1642 /* For partitions we need parental (whole-disk) sysfs directory pretty
1643 * often, so close it now when all is done */
1644 ul_path_close_dirfd(disk->sysfs);
1645
cf3c1bc7 1646 DBG(DEV, ul_debugobj(disk, "probe whole-disk for partitions -- done"));
2a4c734b 1647 closedir(dir);
4ca3c472 1648 return 0;
09a71aa1
PR
1649}
1650
4ca3c472 1651static char *get_wholedisk_from_partition_dirent(DIR *dir, struct dirent *d, char *buf, size_t bufsz)
09a71aa1 1652{
09a71aa1
PR
1653 char *p;
1654 int len;
1655
4ca3c472 1656 if ((len = readlinkat(dirfd(dir), d->d_name, buf, bufsz - 1)) < 0)
09a71aa1 1657 return 0;
540d506f 1658
4ca3c472 1659 buf[len] = '\0';
09a71aa1
PR
1660
1661 /* The path ends with ".../<device>/<partition>" */
4ca3c472 1662 p = strrchr(buf, '/');
540d506f 1663 if (!p)
4ca3c472 1664 return NULL;
540d506f
KZ
1665 *p = '\0';
1666
4ca3c472 1667 p = strrchr(buf, '/');
540d506f 1668 if (!p)
4ca3c472 1669 return NULL;
540d506f 1670 p++;
2a4c734b 1671
4ca3c472 1672 return p;
09a71aa1
PR
1673}
1674
1675/*
b93585e8 1676 * Reads slaves/holders and partitions for specified device into device tree
09a71aa1 1677 */
4ca3c472
KZ
1678static int process_dependencies(
1679 struct lsblk_devtree *tr,
1680 struct lsblk_device *dev,
1681 int do_partitions)
09a71aa1
PR
1682{
1683 DIR *dir;
1684 struct dirent *d;
540d506f 1685 const char *depname;
94ebe1d3 1686 struct lsblk_device *dep = NULL;
09a71aa1 1687
fed34a1e 1688 assert(dev);
09a71aa1 1689
57a06133
KZ
1690 if (lsblk->nodeps)
1691 return 0;
1692
4ca3c472
KZ
1693 /* read all or specified partition */
1694 if (do_partitions && dev->npartitions)
1695 process_partitions(tr, dev);
1696
4ca3c472 1697 DBG(DEV, ul_debugobj(dev, "%s: reading dependencies", dev->name));
7e786eca 1698
4ca3c472
KZ
1699 if (!(lsblk->inverse ? dev->nslaves : dev->nholders)) {
1700 DBG(DEV, ul_debugobj(dev, " ignore (no slaves/holders)"));
94ebe1d3 1701 goto done;
4ca3c472 1702 }
09a71aa1 1703
540d506f 1704 depname = lsblk->inverse ? "slaves" : "holders";
fed34a1e 1705 dir = ul_path_opendir(dev->sysfs, depname);
4ca3c472
KZ
1706 if (!dir) {
1707 DBG(DEV, ul_debugobj(dev, " ignore (no slaves/holders directory)"));
94ebe1d3 1708 goto done;
4ca3c472 1709 }
ff7992d6 1710 ul_path_close_dirfd(dev->sysfs);
2a4c734b 1711
4ca3c472 1712 DBG(DEV, ul_debugobj(dev, " %s: checking for '%s' dependence", dev->name, depname));
7e786eca 1713
2a4c734b 1714 while ((d = xreaddir(dir))) {
ff7992d6 1715 struct lsblk_device *disk = NULL;
4ca3c472 1716
09a71aa1 1717 /* Is the dependency a partition? */
f153614c 1718 if (sysfs_blkdev_is_partition_dirent(dir, d, NULL)) {
4ca3c472
KZ
1719
1720 char buf[PATH_MAX];
1721 char *diskname;
4ca3c472
KZ
1722
1723 DBG(DEV, ul_debugobj(dev, " %s: dependence is partition", d->d_name));
1724
1725 diskname = get_wholedisk_from_partition_dirent(dir, d, buf, sizeof(buf));
1726 if (diskname)
0e086f82 1727 disk = devtree_get_device_or_new(tr, NULL, diskname);
4ca3c472
KZ
1728 if (!disk) {
1729 DBG(DEV, ul_debugobj(dev, " ignore no wholedisk ???"));
ff7992d6 1730 goto next;
7e786eca 1731 }
4ca3c472 1732
0e086f82 1733 dep = devtree_get_device_or_new(tr, disk, d->d_name);
4ca3c472 1734 if (!dep)
ff7992d6 1735 goto next;
4ca3c472
KZ
1736
1737 if (lsblk_device_new_dependence(dev, dep) == 0)
1738 process_dependencies(tr, dep, 1);
1739
1740 if (lsblk->inverse
1741 && lsblk_device_new_dependence(dep, disk) == 0)
1742 process_dependencies(tr, disk, 0);
270e66ec 1743 }
09a71aa1 1744 /* The dependency is a whole device. */
4ca3c472
KZ
1745 else {
1746 DBG(DEV, ul_debugobj(dev, " %s: %s: dependence is whole-disk",
fed34a1e 1747 dev->name, d->d_name));
4ca3c472 1748
0e086f82 1749 dep = devtree_get_device_or_new(tr, NULL, d->d_name);
4ca3c472 1750 if (!dep)
ff7992d6 1751 goto next;
4ca3c472
KZ
1752
1753 if (lsblk_device_new_dependence(dev, dep) == 0)
1754 /* For inverse tree we don't want to show partitions
1755 * if the dependence is on whole-disk */
1756 process_dependencies(tr, dep, lsblk->inverse ? 0 : 1);
7e786eca 1757 }
ff7992d6
KZ
1758next:
1759 if (dep && dep->sysfs)
1760 ul_path_close_dirfd(dep->sysfs);
1761 if (disk && disk->sysfs)
1762 ul_path_close_dirfd(disk->sysfs);
2a4c734b
MB
1763 }
1764 closedir(dir);
94ebe1d3
KZ
1765done:
1766 dep = devtree_pktcdvd_get_dep(tr, dev, lsblk->inverse);
1767
1768 if (dep && lsblk_device_new_dependence(dev, dep) == 0) {
1769 lsblk_devtree_remove_root(tr, dep);
1770 process_dependencies(tr, dep, lsblk->inverse ? 0 : 1);
1771 }
2a4c734b
MB
1772
1773 return 0;
1774}
1775
b93585e8
KZ
1776/*
1777 * Defines the device as root node in the device tree and walks on all dependencies of the device.
1778 */
e7b5353e 1779static int __process_one_device(struct lsblk_devtree *tr, char *devname, dev_t devno)
2a4c734b 1780{
291606fa 1781 struct lsblk_device *dev = NULL;
ff7992d6 1782 struct lsblk_device *disk = NULL;
1539750f 1783 char buf[PATH_MAX + 1], *name = NULL, *diskname = NULL;
1539750f 1784 int real_part = 0, rc = -EINVAL;
2a4c734b 1785
106e6960 1786 if (devno == 0 && devname) {
e7b5353e
KZ
1787 struct stat st;
1788
1789 DBG(DEV, ul_debug("%s: reading alone device", devname));
1790
1791 if (stat(devname, &st) || !S_ISBLK(st.st_mode)) {
1792 warnx(_("%s: not a block device"), devname);
1793 goto leave;
1794 }
1795 devno = st.st_rdev;
106e6960 1796 } else if (devno) {
e7b5353e 1797 DBG(DEV, ul_debug("%d:%d: reading alone device", major(devno), minor(devno)));
106e6960
KZ
1798 } else {
1799 assert(devno || devname);
1800 return -EINVAL;
1801 }
da30cb2a 1802
f153614c
KZ
1803 /* TODO: sysfs_devno_to_devname() internally initializes path_cxt, it
1804 * would be better to use ul_new_sysfs_path() + sysfs_blkdev_get_name()
b158bd29 1805 * and reuse path_cxt for initialize_device()
f153614c 1806 */
e7b5353e 1807 name = sysfs_devno_to_devname(devno, buf, sizeof(buf));
f153614c 1808 if (!name) {
e7b5353e
KZ
1809 if (devname)
1810 warn(_("%s: failed to get sysfs name"), devname);
1539750f 1811 goto leave;
2a4c734b 1812 }
3273ce07 1813 name = xstrdup(name);
da30cb2a
PR
1814
1815 if (!strncmp(name, "dm-", 3)) {
1816 /* dm mapping is never a real partition! */
1817 real_part = 0;
1818 } else {
ff7992d6
KZ
1819 dev_t diskno = 0;
1820
1821 if (blkid_devno_to_wholedisk(devno, buf, sizeof(buf), &diskno)) {
e7b5353e 1822 warn(_("%s: failed to get whole-disk device number"), name);
1539750f 1823 goto leave;
da30cb2a
PR
1824 }
1825 diskname = buf;
ff7992d6 1826 real_part = devno != diskno;
da30cb2a
PR
1827 }
1828
1829 if (!real_part) {
2a4c734b 1830 /*
09a71aa1 1831 * Device is not a partition.
2a4c734b 1832 */
4ca3c472
KZ
1833 DBG(DEV, ul_debug(" non-partition"));
1834
0e086f82 1835 dev = devtree_get_device_or_new(tr, NULL, name);
291606fa 1836 if (!dev)
270e66ec 1837 goto leave;
291606fa 1838
4ca3c472
KZ
1839 lsblk_devtree_add_root(tr, dev);
1840 process_dependencies(tr, dev, !lsblk->inverse);
270e66ec 1841 } else {
2a4c734b 1842 /*
291606fa 1843 * Partition, read sysfs name of the disk device
2a4c734b 1844 */
4ca3c472
KZ
1845 DBG(DEV, ul_debug(" partition"));
1846
0e086f82
KZ
1847 disk = devtree_get_device_or_new(tr, NULL, diskname);
1848 if (!disk)
270e66ec 1849 goto leave;
291606fa 1850
0e086f82 1851 dev = devtree_get_device_or_new(tr, disk, name);
291606fa 1852 if (!dev)
270e66ec 1853 goto leave;
09a71aa1 1854
4ca3c472
KZ
1855 lsblk_devtree_add_root(tr, dev);
1856 process_dependencies(tr, dev, 1);
291606fa 1857
4ca3c472 1858 if (lsblk->inverse
0e086f82
KZ
1859 && lsblk_device_new_dependence(dev, disk) == 0)
1860 process_dependencies(tr, disk, 0);
ff7992d6
KZ
1861 else
1862 ul_path_close_dirfd(disk->sysfs);
2a4c734b
MB
1863 }
1864
1539750f 1865 rc = 0;
270e66ec 1866leave:
ff7992d6
KZ
1867 if (dev && dev->sysfs)
1868 ul_path_close_dirfd(dev->sysfs);
1869 if (disk && disk->sysfs)
1870 ul_path_close_dirfd(disk->sysfs);
da30cb2a 1871 free(name);
1539750f 1872 return rc;
2a4c734b
MB
1873}
1874
e7b5353e
KZ
1875static int process_one_device(struct lsblk_devtree *tr, char *devname)
1876{
106e6960 1877 assert(devname);
e7b5353e
KZ
1878 return __process_one_device(tr, devname, 0);
1879}
1880
1881/*
1882 * The /sys/block contains only root devices, and no partitions. It seems more
1883 * simple to scan /sys/dev/block where are all devices without exceptions to get
1884 * top-level devices for the reverse tree.
1885 */
1886static int process_all_devices_inverse(struct lsblk_devtree *tr)
e15c9e3d
KZ
1887{
1888 DIR *dir;
1889 struct dirent *d;
e7b5353e
KZ
1890 struct path_cxt *pc = ul_new_path(_PATH_SYS_DEVBLOCK);
1891
1892 assert(lsblk->inverse);
e15c9e3d
KZ
1893
1894 if (!pc)
1895 err(EXIT_FAILURE, _("failed to allocate /sys handler"));
1896
1897 ul_path_set_prefix(pc, lsblk->sysroot);
e7b5353e
KZ
1898 dir = ul_path_opendir(pc, NULL);
1899 if (!dir)
1900 goto done;
1901
1902 DBG(DEV, ul_debug("iterate on " _PATH_SYS_DEVBLOCK));
1903
1904 while ((d = xreaddir(dir))) {
1905 dev_t devno;
1906 int maj, min;
e15c9e3d 1907
e7b5353e
KZ
1908 DBG(DEV, ul_debug(" %s dentry", d->d_name));
1909
1910 if (sscanf(d->d_name, "%d:%d", &maj, &min) != 2)
1911 continue;
1912 devno = makedev(maj, min);
1913
1914 if (is_maj_excluded(maj) || !is_maj_included(maj))
1915 continue;
1916 if (ul_path_countf_dirents(pc, "%s/holders", d->d_name) != 0)
1917 continue;
1918 if (sysfs_devno_count_partitions(devno) != 0)
1919 continue;
1920 __process_one_device(tr, NULL, devno);
1921 }
1922
1923 closedir(dir);
1924done:
1925 ul_unref_path(pc);
1926 DBG(DEV, ul_debug("iterate on " _PATH_SYS_DEVBLOCK " -- done"));
1927 return 0;
1928}
1929
b93585e8
KZ
1930/*
1931 * Reads root nodes (devices) from /sys/block into devices tree
1932 */
e7b5353e
KZ
1933static int process_all_devices(struct lsblk_devtree *tr)
1934{
1935 DIR *dir;
1936 struct dirent *d;
1937 struct path_cxt *pc;
1938
1939 assert(lsblk->inverse == 0);
1940
1941 pc = ul_new_path(_PATH_SYS_BLOCK);
1942 if (!pc)
1943 err(EXIT_FAILURE, _("failed to allocate /sys handler"));
1944
1945 ul_path_set_prefix(pc, lsblk->sysroot);
e15c9e3d
KZ
1946 dir = ul_path_opendir(pc, NULL);
1947 if (!dir)
1948 goto done;
1949
e7b5353e 1950 DBG(DEV, ul_debug("iterate on " _PATH_SYS_BLOCK));
e15c9e3d
KZ
1951
1952 while ((d = xreaddir(dir))) {
ff7992d6 1953 struct lsblk_device *dev = NULL;
e15c9e3d
KZ
1954
1955 DBG(DEV, ul_debug(" %s dentry", d->d_name));
e15c9e3d
KZ
1956 dev = devtree_get_device_or_new(tr, NULL, d->d_name);
1957 if (!dev)
ff7992d6 1958 goto next;
e15c9e3d
KZ
1959
1960 /* remove unwanted devices */
1961 if (is_maj_excluded(dev->maj) || !is_maj_included(dev->maj)) {
1962 DBG(DEV, ul_debug(" %s: ignore (by filter)", d->d_name));
1963 lsblk_devtree_remove_device(tr, dev);
f6f8a671 1964 dev = NULL;
ff7992d6 1965 goto next;
e15c9e3d
KZ
1966 }
1967
e7b5353e
KZ
1968 if (dev->nslaves) {
1969 DBG(DEV, ul_debug(" %s: ignore (in-middle)", d->d_name));
ff7992d6 1970 goto next;
e15c9e3d 1971 }
0bd05f5e 1972
e7b5353e
KZ
1973 lsblk_devtree_add_root(tr, dev);
1974 process_dependencies(tr, dev, 1);
ff7992d6
KZ
1975next:
1976 /* Let's be careful with number of open files */
1977 if (dev && dev->sysfs)
1978 ul_path_close_dirfd(dev->sysfs);
e15c9e3d
KZ
1979 }
1980
1981 closedir(dir);
1982done:
1983 ul_unref_path(pc);
1984 DBG(DEV, ul_debug("iterate on " _PATH_SYS_BLOCK " -- done"));
1985 return 0;
1986}
1987
b93585e8
KZ
1988/*
1989 * Parses major numbers as specified on lsblk command line
1990 */
2dc03af7 1991static void parse_excludes(const char *str0)
2a4c734b 1992{
2dc03af7
KZ
1993 const char *str = str0;
1994
2a4c734b
MB
1995 while (str && *str) {
1996 char *end = NULL;
ed34643c 1997 unsigned long n;
2a4c734b
MB
1998
1999 errno = 0;
2000 n = strtoul(str, &end, 10);
2001
2dc03af7
KZ
2002 if (end == str || (end && *end && *end != ','))
2003 errx(EXIT_FAILURE, _("failed to parse list '%s'"), str0);
2004 if (errno != 0 && (n == ULONG_MAX || n == 0))
2005 err(EXIT_FAILURE, _("failed to parse list '%s'"), str0);
2a4c734b
MB
2006 excludes[nexcludes++] = n;
2007
2008 if (nexcludes == ARRAY_SIZE(excludes))
f14f02f5 2009 /* TRANSLATORS: The standard value for %d is 256. */
2a4c734b
MB
2010 errx(EXIT_FAILURE, _("the list of excluded devices is "
2011 "too large (limit is %d devices)"),
2012 (int)ARRAY_SIZE(excludes));
2dc03af7 2013
2a4c734b
MB
2014 str = end && *end ? end + 1 : NULL;
2015 }
2016}
2017
b93585e8
KZ
2018/*
2019 * Parses major numbers as specified on lsblk command line
2020 * (TODO: what about refactor and merge parse_excludes() and parse_includes().)
2021 */
2dc03af7 2022static void parse_includes(const char *str0)
2ef4e8ba 2023{
2dc03af7
KZ
2024 const char *str = str0;
2025
2ef4e8ba
KZ
2026 while (str && *str) {
2027 char *end = NULL;
2028 unsigned long n;
2029
2030 errno = 0;
2031 n = strtoul(str, &end, 10);
2032
2dc03af7
KZ
2033 if (end == str || (end && *end && *end != ','))
2034 errx(EXIT_FAILURE, _("failed to parse list '%s'"), str0);
2035 if (errno != 0 && (n == ULONG_MAX || n == 0))
2036 err(EXIT_FAILURE, _("failed to parse list '%s'"), str0);
2ef4e8ba
KZ
2037 includes[nincludes++] = n;
2038
2039 if (nincludes == ARRAY_SIZE(includes))
2040 /* TRANSLATORS: The standard value for %d is 256. */
2041 errx(EXIT_FAILURE, _("the list of included devices is "
2042 "too large (limit is %d devices)"),
2043 (int)ARRAY_SIZE(includes));
2044 str = end && *end ? end + 1 : NULL;
2045 }
2046}
2047
642048e4 2048/*
b816c845 2049 * see set_rawdata_u64() and columns initialization in main()
642048e4
KZ
2050 */
2051static int cmp_u64_cells(struct libscols_cell *a,
2052 struct libscols_cell *b,
2053 __attribute__((__unused__)) void *data)
2054{
2055 uint64_t *adata = (uint64_t *) scols_cell_get_userdata(a),
2056 *bdata = (uint64_t *) scols_cell_get_userdata(b);
2057
2058 if (adata == NULL && bdata == NULL)
2059 return 0;
2060 if (adata == NULL)
2061 return -1;
2062 if (bdata == NULL)
2063 return 1;
2064 return *adata == *bdata ? 0 : *adata >= *bdata ? 1 : -1;
2065}
2066
af5fa636
KZ
2067static void *get_u64_cell(const struct libscols_column *cl __attribute__((__unused__)),
2068 struct libscols_cell *ce,
2069 void *data __attribute__((__unused__)))
2070{
2071 return scols_cell_get_userdata(ce);
2072}
2073
dc4662f0
KZ
2074static void device_set_dedupkey(
2075 struct lsblk_device *dev,
2076 struct lsblk_device *parent,
2077 int id)
2078{
2079 struct lsblk_iter itr;
2080 struct lsblk_device *child = NULL;
2081
2eab8ab4 2082 dev->dedupkey = device_get_data(dev, parent, id, NULL, NULL);
dc4662f0
KZ
2083 if (dev->dedupkey)
2084 DBG(DEV, ul_debugobj(dev, "%s: de-duplication key: %s", dev->name, dev->dedupkey));
2085
2086 if (dev->npartitions == 0)
2087 /* For partitions we often read from parental whole-disk sysfs,
2088 * otherwise we can close */
2089 ul_path_close_dirfd(dev->sysfs);
2090
2091 lsblk_reset_iter(&itr, LSBLK_ITER_FORWARD);
2092
2093 while (lsblk_device_next_child(dev, &itr, &child) == 0)
2094 device_set_dedupkey(child, dev, id);
2095
2096 /* Let's be careful with number of open files */
2097 ul_path_close_dirfd(dev->sysfs);
2098}
2099
2100static void devtree_set_dedupkeys(struct lsblk_devtree *tr, int id)
2101{
2102 struct lsblk_iter itr;
2103 struct lsblk_device *dev = NULL;
2104
2105 lsblk_reset_iter(&itr, LSBLK_ITER_FORWARD);
2106
2107 while (lsblk_devtree_next_root(tr, &itr, &dev) == 0)
2108 device_set_dedupkey(dev, NULL, id);
2109}
2110
9f55de78 2111static struct libscols_filter *new_filter(const char *query)
3e0b4a82
KZ
2112{
2113 struct libscols_filter *f;
3e0b4a82
KZ
2114
2115 f = scols_new_filter(NULL);
2116 if (!f)
2117 err(EXIT_FAILURE, _("failed to allocate filter"));
0398951e 2118 if (query && scols_filter_parse_string(f, query) != 0)
3e0b4a82
KZ
2119 errx(EXIT_FAILURE, _("failed to parse \"%s\": %s"), query,
2120 scols_filter_get_errmsg(f));
9f55de78
KZ
2121 return f;
2122}
2123
0398951e
KZ
2124static struct libscols_filter *new_counter_filter(const char *query)
2125{
2126 lsblk->ct_filters = xreallocarray(lsblk->ct_filters, lsblk->ncts + 1,
2127 sizeof(struct libscols_filter *));
2128
2129 lsblk->ct_filters[lsblk->ncts] = new_filter(query);
2130 lsblk->ncts++;
2131
2132 return lsblk->ct_filters[lsblk->ncts - 1];
2133}
2134
2135static void set_counter_properties(const char *str0)
2136{
2137 struct libscols_filter *fltr = NULL;
2138 struct libscols_counter *ct;
2139 char *p, *str = xstrdup(str0);
2140 char *name = NULL, *param = NULL, *func = NULL;
2141
2142 for (p = strtok(str, ":"); p != NULL; p = strtok((char *)0, ":")) {
2143 if (!name)
2144 name = p;
2145 else if (!param)
2146 param = p;
2147 else if (!func)
2148 func = p;
2149 else
2150 errx(EXIT_FAILURE, _("unexpected counter specification: %s"), str0);
2151 }
2152
2153 if (!name)
2154 errx(EXIT_FAILURE, _("counter not properly specified"));
2155
2156 /* use the latest counter filter (--ct-filter) or create empty */
2157 if (lsblk->ncts)
2158 fltr = lsblk->ct_filters[lsblk->ncts - 1];
2159 else
2160 fltr = new_counter_filter(NULL);
2161
2162 ct = scols_filter_new_counter(fltr);
2163 if (!ct)
2164 err(EXIT_FAILURE, _("failed to allocate counter"));
2165
2166 scols_counter_set_name(ct, name);
2167 if (param)
2168 scols_counter_set_param(ct, param);
2169 if (func) {
2170 int x;
2171
2172 if (strcmp(func, "max") == 0)
2173 x = SCOLS_COUNTER_MAX;
2174 else if (strcmp(func, "min") == 0)
2175 x = SCOLS_COUNTER_MIN;
2176 else if (strcmp(func, "sum") == 0)
2177 x = SCOLS_COUNTER_SUM;
2178 else if (strcmp(func, "count") == 0)
2179 x = SCOLS_COUNTER_COUNT;
2180 else
2181 errx(EXIT_FAILURE, _("unsupported counter type: %s"), func);
2182
2183 scols_counter_set_func(ct, x);
2184 }
2185 free(str);
2186}
2187
2188static void print_counters(void)
2189{
2190 struct libscols_iter *itr;
2191 size_t i;
2192
2193 fputc('\n', stdout);
2194 fputs(_("Summary:\n"), stdout);
2195
2196 itr = scols_new_iter(SCOLS_ITER_FORWARD);
2197 if (!itr)
2198 err(EXIT_FAILURE, _("failed to allocate iterator"));
2199
2200 for (i = 0; i < lsblk->ncts; i++) {
2201 struct libscols_filter *fltr = lsblk->ct_filters[i];
2202 struct libscols_counter *ct = NULL;
2203
2204 scols_reset_iter(itr, SCOLS_ITER_FORWARD);
2205 while (scols_filter_next_counter(fltr, itr, &ct) == 0) {
2206 printf("%16llu %s\n",
2207 scols_counter_get_result(ct),
2208 scols_counter_get_name(ct));
2209 }
2210 }
2211
2212 scols_free_iter(itr);
2213}
2214
f7c9b28a
KZ
2215static void set_column_type(const struct colinfo *ci, struct libscols_column *cl, int fl)
2216{
2217 switch (ci->type) {
2218 case COLTYPE_SIZE:
af5fa636 2219 /* See init_scols_filter(), it may overwrite the type */
f7c9b28a
KZ
2220 if (!lsblk->bytes)
2221 break;
2222 /* fallthrough */
2223 case COLTYPE_NUM:
2224 scols_column_set_json_type(cl, SCOLS_JSON_NUMBER);
af5fa636
KZ
2225 scols_column_set_data_type(cl, SCOLS_DATA_U64);
2226 return;
f7c9b28a
KZ
2227 case COLTYPE_BOOL:
2228 scols_column_set_json_type(cl, SCOLS_JSON_BOOLEAN);
af5fa636
KZ
2229 scols_column_set_data_type(cl, SCOLS_DATA_BOOLEAN);
2230 return;
f7c9b28a 2231 default:
f7c9b28a
KZ
2232 break;
2233 }
af5fa636
KZ
2234
2235 /* default */
2236 if (fl & SCOLS_FL_WRAP)
2237 scols_column_set_json_type(cl, SCOLS_JSON_ARRAY_STRING);
2238 else
2239 scols_column_set_json_type(cl, SCOLS_JSON_STRING);
2240
2241 scols_column_set_data_type(cl, SCOLS_DATA_STRING);
f7c9b28a
KZ
2242}
2243
9f55de78
KZ
2244static void init_scols_filter(struct libscols_table *tb, struct libscols_filter *f)
2245{
2246 struct libscols_iter *itr;
2247 const char *name = NULL;
15a07bc7 2248 int nerrs = 0;
3e0b4a82
KZ
2249
2250 itr = scols_new_iter(SCOLS_ITER_FORWARD);
2251 if (!itr)
2252 err(EXIT_FAILURE, _("failed to allocate iterator"));
2253
2254 while (scols_filter_next_holder(f, itr, &name, 0) == 0) {
2255 struct libscols_column *col = scols_table_get_column_by_name(tb, name);
af5fa636
KZ
2256 int id = column_name_to_id(name, strlen(name));
2257 const struct colinfo *ci = id >= 0 ? &infos[id] : NULL;
3e0b4a82 2258
15a07bc7
KZ
2259 if (!ci) {
2260 nerrs++;
2261 continue; /* report all unknown columns */
2262 }
af5fa636 2263 if (!col) {
3e0b4a82 2264 add_column(id);
0d6a0a6e 2265 col = scols_table_new_column(tb, ci->name,
3e0b4a82
KZ
2266 ci->whint, SCOLS_FL_HIDDEN);
2267 if (!col)
2268 err(EXIT_FAILURE,_("failed to allocate output column"));
f7c9b28a
KZ
2269
2270 set_column_type(ci, col, ci->flags);
3e0b4a82 2271 }
af5fa636
KZ
2272
2273 /* For sizes use rawdata (u64) rather than strings from table */
2274 if (ci->type == COLTYPE_SIZE
2275 && !lsblk->bytes
b0df377d 2276 && !scols_column_has_data_func(col)) {
af5fa636
KZ
2277
2278 scols_column_set_data_type(col, SCOLS_DATA_U64);
b0df377d 2279 scols_column_set_data_func(col, get_u64_cell, NULL);
af5fa636
KZ
2280 lsblk->rawdata = 1;
2281 }
2282
3e0b4a82
KZ
2283 scols_filter_assign_column(f, itr, name, col);
2284 }
2285
2286 scols_free_iter(itr);
15a07bc7
KZ
2287
2288 if (!nerrs)
2289 return;
2290
3e0b4a82
KZ
2291 errx(EXIT_FAILURE, _("failed to initialize filter"));
2292}
2293
0398951e 2294
86be6a32 2295static void __attribute__((__noreturn__)) usage(void)
2a4c734b 2296{
86be6a32 2297 FILE *out = stdout;
2a4c734b 2298
39b232c1
SK
2299 fputs(USAGE_HEADER, out);
2300 fprintf(out, _(" %s [options] [<device> ...]\n"), program_invocation_short_name);
451dbcfa
BS
2301
2302 fputs(USAGE_SEPARATOR, out);
2303 fputs(_("List information about block devices.\n"), out);
2304
39b232c1 2305 fputs(USAGE_OPTIONS, out);
1e090fdc 2306 fputs(_(" -A, --noempty don't print empty devices\n"), out);
0bd05f5e
KZ
2307 fputs(_(" -D, --discard print discard capabilities\n"), out);
2308 fputs(_(" -E, --dedup <column> de-duplicate output by <column>\n"), out);
2309 fputs(_(" -I, --include <list> show only devices with specified major numbers\n"), out);
2310 fputs(_(" -J, --json use JSON output format\n"), out);
1e090fdc 2311 fputs(_(" -M, --merge group parents of sub-trees (usable for RAIDs, Multi-path)\n"), out);
0bd05f5e
KZ
2312 fputs(_(" -O, --output-all output all columns\n"), out);
2313 fputs(_(" -P, --pairs use key=\"value\" output format\n"), out);
c1b1b22e
KZ
2314 fputs(_(" -Q, --filter <expr> print only lines maching the expression\n"), out);
2315 fputs(_(" --highlight <expr> colorize lines maching the expression\n"), out);
2316 fputs(_(" --ct-filter <expr> restrict the next counter\n"), out);
2317 fputs(_(" --ct <name>[:<param>[:<func>]] define a custom counter\n"), out);
0bd05f5e 2318 fputs(_(" -S, --scsi output info about SCSI devices\n"), out);
97afabf7 2319 fputs(_(" -N, --nvme output info about NVMe devices\n"), out);
1b3e8f5d 2320 fputs(_(" -v, --virtio output info about virtio devices\n"), out);
b9c088f2 2321 fputs(_(" -T, --tree[=<column>] use tree format output\n"), out);
39b232c1
SK
2322 fputs(_(" -a, --all print all devices\n"), out);
2323 fputs(_(" -b, --bytes print SIZE in bytes rather than in human readable format\n"), out);
2324 fputs(_(" -d, --nodeps don't print slaves or holders\n"), out);
39b232c1 2325 fputs(_(" -e, --exclude <list> exclude devices by major number (default: RAM disks)\n"), out);
39b232c1 2326 fputs(_(" -f, --fs output info about filesystems\n"), out);
39b232c1 2327 fputs(_(" -i, --ascii use ascii characters only\n"), out);
39b232c1 2328 fputs(_(" -l, --list use list format output\n"), out);
3e59c481 2329 fputs(_(" -m, --perms output info about permissions\n"), out);
39b232c1 2330 fputs(_(" -n, --noheadings don't print headings\n"), out);
ed02618d 2331 fputs(_(" -o, --output <list> output columns (see --list-columns)\n"), out);
ef75bc88 2332 fputs(_(" -p, --paths print complete device path\n"), out);
39b232c1
SK
2333 fputs(_(" -r, --raw use raw output format\n"), out);
2334 fputs(_(" -s, --inverse inverse dependencies\n"), out);
3e59c481 2335 fputs(_(" -t, --topology output info about topology\n"), out);
7636d906 2336 fputs(_(" -w, --width <num> specifies output width as number of characters\n"), out);
b5af3ee8 2337 fputs(_(" -x, --sort <column> sort output by <column>\n"), out);
0f843ab6 2338 fputs(_(" -y, --shell use column names to be usable as shell variable identifiers\n"), out);
e7252840 2339 fputs(_(" -z, --zoned print zone related information\n"), out);
498d1486 2340 fputs(_(" --sysroot <dir> use specified directory as system root\n"), out);
ed02618d 2341
39b232c1 2342 fputs(USAGE_SEPARATOR, out);
ed02618d 2343 fputs(_(" -H, --list-columns list the available columns\n"), out);
bad4c729 2344 fprintf(out, USAGE_HELP_OPTIONS(22));
2a4c734b 2345
ed02618d 2346 fprintf(out, USAGE_MAN_TAIL("lsblk(8)"));
2a4c734b 2347
ed02618d
KZ
2348 exit(EXIT_SUCCESS);
2349}
2a4c734b 2350
ed02618d
KZ
2351
2352static void __attribute__((__noreturn__)) list_colunms(void)
2353{
2354 size_t i;
2355 struct libscols_table *tb = xcolumn_list_table_new("lsblk-columns", stdout,
2356 lsblk->flags & LSBLK_RAW,
2357 lsblk->flags & LSBLK_JSON);
2358
2359 for (i = 0; i < ARRAY_SIZE(infos); i++) {
2360 const struct colinfo *ci = &infos[i];
2361
2362 xcolumn_list_table_append_line(tb, ci->name,
2363 ci->type == COLTYPE_SIZE ? -1 :
2364 ci->type == COLTYPE_NUM ? SCOLS_JSON_NUMBER :
2365 ci->type == COLTYPE_BOOL ? SCOLS_JSON_BOOLEAN :
2366 ci->flags & SCOLS_FL_WRAP ? SCOLS_JSON_ARRAY_STRING :
2367 SCOLS_JSON_STRING,
2368 ci->type == COLTYPE_SIZE ? "<string|number>" : NULL,
2369 _(ci->help));
2370 }
2371
2372
2373 scols_print_table(tb);
2374 scols_unref_table(tb);
2a4c734b 2375
86be6a32 2376 exit(EXIT_SUCCESS);
2a4c734b
MB
2377}
2378
f65b3bb6
SK
2379static void check_sysdevblock(void)
2380{
2381 if (access(_PATH_SYS_DEVBLOCK, R_OK) != 0)
2382 err(EXIT_FAILURE, _("failed to access sysfs directory: %s"),
2383 _PATH_SYS_DEVBLOCK);
2384}
2385
2a4c734b
MB
2386int main(int argc, char *argv[])
2387{
dc4662f0
KZ
2388 struct lsblk _ls = {
2389 .sort_id = -1,
2390 .dedup_id = -1,
b9c088f2
KZ
2391 .flags = LSBLK_TREE,
2392 .tree_id = COL_NAME
dc4662f0 2393 };
57502dd3 2394 struct lsblk_devtree *tr = NULL;
ed02618d 2395 int c, status = EXIT_FAILURE, collist = 0;
43bca827 2396 char *outarg = NULL;
40b17508 2397 size_t i;
7636d906 2398 unsigned int width = 0;
b9c088f2 2399 int force_tree = 0, has_tree_col = 0;
2a4c734b 2400
ffc27623 2401 enum {
0398951e
KZ
2402 OPT_SYSROOT = CHAR_MAX + 1,
2403 OPT_COUNTER_FILTER,
ed02618d
KZ
2404 OPT_COUNTER,
2405 OPT_HIGHLIGHT,
ffc27623
KZ
2406 };
2407
6c7d5ae9 2408 static const struct option longopts[] = {
87918040
SK
2409 { "all", no_argument, NULL, 'a' },
2410 { "bytes", no_argument, NULL, 'b' },
2411 { "nodeps", no_argument, NULL, 'd' },
554e866b 2412 { "noempty", no_argument, NULL, 'A' },
87918040 2413 { "discard", no_argument, NULL, 'D' },
0bd05f5e 2414 { "dedup", required_argument, NULL, 'E' },
f2df4365 2415 { "zoned", no_argument, NULL, 'z' },
87918040
SK
2416 { "help", no_argument, NULL, 'h' },
2417 { "json", no_argument, NULL, 'J' },
2418 { "output", required_argument, NULL, 'o' },
2419 { "output-all", no_argument, NULL, 'O' },
3e0b4a82 2420 { "filter", required_argument, NULL, 'Q' },
ed02618d 2421 { "highlight", required_argument, NULL, OPT_HIGHLIGHT },
0bd05f5e 2422 { "merge", no_argument, NULL, 'M' },
87918040
SK
2423 { "perms", no_argument, NULL, 'm' },
2424 { "noheadings", no_argument, NULL, 'n' },
2425 { "list", no_argument, NULL, 'l' },
2426 { "ascii", no_argument, NULL, 'i' },
2427 { "raw", no_argument, NULL, 'r' },
2428 { "inverse", no_argument, NULL, 's' },
2429 { "fs", no_argument, NULL, 'f' },
2430 { "exclude", required_argument, NULL, 'e' },
2431 { "include", required_argument, NULL, 'I' },
2432 { "topology", no_argument, NULL, 't' },
2433 { "paths", no_argument, NULL, 'p' },
2434 { "pairs", no_argument, NULL, 'P' },
2435 { "scsi", no_argument, NULL, 'S' },
97afabf7 2436 { "nvme", no_argument, NULL, 'N' },
1b3e8f5d 2437 { "virtio", no_argument, NULL, 'v' },
87918040 2438 { "sort", required_argument, NULL, 'x' },
ffc27623 2439 { "sysroot", required_argument, NULL, OPT_SYSROOT },
25fb0638 2440 { "shell", no_argument, NULL, 'y' },
b9c088f2 2441 { "tree", optional_argument, NULL, 'T' },
87918040 2442 { "version", no_argument, NULL, 'V' },
7636d906 2443 { "width", required_argument, NULL, 'w' },
0398951e
KZ
2444 { "ct-filter", required_argument, NULL, OPT_COUNTER_FILTER },
2445 { "ct", required_argument, NULL, OPT_COUNTER },
ed02618d 2446 { "list-columns", no_argument, NULL, 'H' },
87918040 2447 { NULL, 0, NULL, 0 },
2a4c734b
MB
2448 };
2449
a7349ee3 2450 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
1b4d2a4a 2451 { 'D','O' },
98f2dc7a 2452 { 'I','e' },
4a102a48 2453 { 'J', 'P', 'r' },
1b4d2a4a
MY
2454 { 'O','S' },
2455 { 'O','f' },
2456 { 'O','m' },
a44cd891 2457 { 'O','o' },
1b4d2a4a 2458 { 'O','t' },
1d9e35cc 2459 { 'P','T', 'l','r' },
98f2dc7a
KZ
2460 { 0 }
2461 };
2462 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
2463
2a4c734b
MB
2464 setlocale(LC_ALL, "");
2465 bindtextdomain(PACKAGE, LOCALEDIR);
2466 textdomain(PACKAGE);
2c308875 2467 close_stdout_atexit();
2a4c734b
MB
2468
2469 lsblk = &_ls;
2a4c734b 2470
7e786eca 2471 lsblk_init_debug();
0398951e 2472 scols_init_debug(0);
7e786eca 2473
98f2dc7a 2474 while((c = getopt_long(argc, argv,
ed02618d 2475 "AabdDzE:e:fHhJlNnMmo:OpPQ:iI:rstVvST::w:x:y",
554e866b 2476 longopts, NULL)) != -1) {
98f2dc7a
KZ
2477
2478 err_exclusive_options(c, longopts, excl, excl_st);
2479
2a4c734b 2480 switch(c) {
554e866b
KZ
2481 case 'A':
2482 lsblk->noempty = 1;
2483 break;
2a4c734b
MB
2484 case 'a':
2485 lsblk->all_devices = 1;
2486 break;
2487 case 'b':
2488 lsblk->bytes = 1;
2489 break;
f31505fe
KZ
2490 case 'd':
2491 lsblk->nodeps = 1;
2492 break;
2d232246 2493 case 'D':
be685b98
KZ
2494 add_uniq_column(COL_NAME);
2495 add_uniq_column(COL_DALIGN);
2496 add_uniq_column(COL_DGRAN);
2497 add_uniq_column(COL_DMAX);
2498 add_uniq_column(COL_DZERO);
2d232246 2499 break;
f2df4365 2500 case 'z':
be685b98
KZ
2501 add_uniq_column(COL_NAME);
2502 add_uniq_column(COL_ZONED);
e7252840
NA
2503 add_uniq_column(COL_ZONE_SZ);
2504 add_uniq_column(COL_ZONE_NR);
2505 add_uniq_column(COL_ZONE_AMAX);
2506 add_uniq_column(COL_ZONE_OMAX);
2507 add_uniq_column(COL_ZONE_APP);
2508 add_uniq_column(COL_ZONE_WGRAN);
f2df4365 2509 break;
2a4c734b
MB
2510 case 'e':
2511 parse_excludes(optarg);
2512 break;
4a102a48 2513 case 'J':
341c4ae2 2514 lsblk->flags |= LSBLK_JSON;
4a102a48 2515 break;
2a4c734b 2516 case 'l':
341c4ae2 2517 lsblk->flags &= ~LSBLK_TREE; /* disable the default */
2a4c734b 2518 break;
0bd05f5e
KZ
2519 case 'M':
2520 lsblk->merge = 1;
2521 break;
2a4c734b 2522 case 'n':
341c4ae2 2523 lsblk->flags |= LSBLK_NOHEADINGS;
2a4c734b
MB
2524 break;
2525 case 'o':
43bca827 2526 outarg = optarg;
2a4c734b 2527 break;
1b4d2a4a 2528 case 'O':
40b17508 2529 for (ncolumns = 0 ; ncolumns < ARRAY_SIZE(infos); ncolumns++)
1b4d2a4a
MY
2530 columns[ncolumns] = ncolumns;
2531 break;
c7e76cd1
KZ
2532 case 'p':
2533 lsblk->paths = 1;
2534 break;
6ea1bdd3 2535 case 'P':
341c4ae2
KZ
2536 lsblk->flags |= LSBLK_EXPORT;
2537 lsblk->flags &= ~LSBLK_TREE; /* disable the default */
6ea1bdd3 2538 break;
3e0b4a82 2539 case 'Q':
9f55de78 2540 lsblk->filter = new_filter(optarg);
3e0b4a82
KZ
2541 lsblk->flags &= ~LSBLK_TREE; /* disable the default */
2542 break;
25fb0638
KZ
2543 case 'y':
2544 lsblk->flags |= LSBLK_SHELLVAR;
2545 break;
2a4c734b 2546 case 'i':
341c4ae2 2547 lsblk->flags |= LSBLK_ASCII;
2a4c734b 2548 break;
2ef4e8ba 2549 case 'I':
2ef4e8ba
KZ
2550 parse_includes(optarg);
2551 break;
2a4c734b 2552 case 'r':
341c4ae2
KZ
2553 lsblk->flags &= ~LSBLK_TREE; /* disable the default */
2554 lsblk->flags |= LSBLK_RAW; /* enable raw */
2a4c734b 2555 break;
09a71aa1
PR
2556 case 's':
2557 lsblk->inverse = 1;
2558 break;
2a4c734b 2559 case 'f':
be685b98
KZ
2560 add_uniq_column(COL_NAME);
2561 add_uniq_column(COL_FSTYPE);
4631edae 2562 add_uniq_column(COL_FSVERSION);
be685b98
KZ
2563 add_uniq_column(COL_LABEL);
2564 add_uniq_column(COL_UUID);
bc405d64
KZ
2565 add_uniq_column(COL_FSAVAIL);
2566 add_uniq_column(COL_FSUSEPERC);
62df5395 2567 add_uniq_column(COL_TARGETS);
2a4c734b
MB
2568 break;
2569 case 'm':
be685b98
KZ
2570 add_uniq_column(COL_NAME);
2571 add_uniq_column(COL_SIZE);
2572 add_uniq_column(COL_OWNER);
2573 add_uniq_column(COL_GROUP);
2574 add_uniq_column(COL_MODE);
2a4c734b
MB
2575 break;
2576 case 't':
be685b98
KZ
2577 add_uniq_column(COL_NAME);
2578 add_uniq_column(COL_ALIOFF);
2579 add_uniq_column(COL_MINIO);
2580 add_uniq_column(COL_OPTIO);
2581 add_uniq_column(COL_PHYSEC);
2582 add_uniq_column(COL_LOGSEC);
2583 add_uniq_column(COL_ROTA);
2584 add_uniq_column(COL_SCHED);
2585 add_uniq_column(COL_RQ_SIZE);
2586 add_uniq_column(COL_RA);
2587 add_uniq_column(COL_WSAME);
2a4c734b 2588 break;
28ffc2b7
MB
2589 case 'S':
2590 lsblk->nodeps = 1;
2591 lsblk->scsi = 1;
be685b98
KZ
2592 add_uniq_column(COL_NAME);
2593 add_uniq_column(COL_HCTL);
2594 add_uniq_column(COL_TYPE);
2595 add_uniq_column(COL_VENDOR);
2596 add_uniq_column(COL_MODEL);
2597 add_uniq_column(COL_REV);
e19e522d 2598 add_uniq_column(COL_SERIAL);
be685b98 2599 add_uniq_column(COL_TRANSPORT);
28ffc2b7 2600 break;
97afabf7 2601 case 'N':
2602 lsblk->nodeps = 1;
2603 lsblk->nvme = 1;
2604 add_uniq_column(COL_NAME);
2605 add_uniq_column(COL_TYPE);
2606 add_uniq_column(COL_MODEL);
2607 add_uniq_column(COL_SERIAL);
39a9b2c9 2608 add_uniq_column(COL_REV);
97afabf7 2609 add_uniq_column(COL_TRANSPORT);
2610 add_uniq_column(COL_RQ_SIZE);
f1a714d8 2611 add_uniq_column(COL_MQ);
97afabf7 2612 break;
1b3e8f5d 2613 case 'v':
2614 lsblk->nodeps = 1;
2615 lsblk->virtio = 1;
2616 add_uniq_column(COL_NAME);
2617 add_uniq_column(COL_TYPE);
2618 add_uniq_column(COL_TRANSPORT);
2619 add_uniq_column(COL_SIZE);
2620 add_uniq_column(COL_RQ_SIZE);
f1a714d8 2621 add_uniq_column(COL_MQ);
1b3e8f5d 2622 break;
1d9e35cc
KZ
2623 case 'T':
2624 force_tree = 1;
6932a110
KZ
2625 if (optarg) {
2626 if (*optarg == '=')
2627 optarg++;
b9c088f2 2628 lsblk->tree_id = column_name_to_id(optarg, strlen(optarg));
6932a110 2629 }
1d9e35cc 2630 break;
ffc27623
KZ
2631 case OPT_SYSROOT:
2632 lsblk->sysroot = optarg;
2633 break;
0bd05f5e 2634 case 'E':
dc4662f0
KZ
2635 lsblk->dedup_id = column_name_to_id(optarg, strlen(optarg));
2636 if (lsblk->dedup_id >= 0)
2637 break;
2638 errtryhelp(EXIT_FAILURE);
2639 break;
7636d906
KZ
2640 case 'w':
2641 width = strtou32_or_err(optarg, _("invalid output width number argument"));
2642 break;
642048e4 2643 case 'x':
341c4ae2 2644 lsblk->flags &= ~LSBLK_TREE; /* disable the default */
642048e4
KZ
2645 lsblk->sort_id = column_name_to_id(optarg, strlen(optarg));
2646 if (lsblk->sort_id >= 0)
2647 break;
2c308875
KZ
2648 errtryhelp(EXIT_FAILURE);
2649 break;
2650
0398951e
KZ
2651 case OPT_COUNTER_FILTER:
2652 new_counter_filter(optarg);
2653 break;
2654 case OPT_COUNTER:
2655 set_counter_properties(optarg);
2656 break;
ed02618d
KZ
2657 case OPT_HIGHLIGHT:
2658 lsblk->hlighter = new_filter(optarg);
2659 break;
0398951e 2660
ed02618d
KZ
2661 case 'H':
2662 collist = 1;
2663 break;
2c308875
KZ
2664 case 'h':
2665 usage();
2666 case 'V':
2667 print_version(EXIT_SUCCESS);
2a4c734b 2668 default:
193b3239 2669 errtryhelp(EXIT_FAILURE);
2a4c734b
MB
2670 }
2671 }
2672
ed02618d
KZ
2673 if (collist)
2674 list_colunms(); /* print end exit */
2675
1d9e35cc
KZ
2676 if (force_tree)
2677 lsblk->flags |= LSBLK_TREE;
2678
f65b3bb6
SK
2679 check_sysdevblock();
2680
2a4c734b 2681 if (!ncolumns) {
be685b98
KZ
2682 add_column(COL_NAME);
2683 add_column(COL_MAJMIN);
2684 add_column(COL_RM);
2685 add_column(COL_SIZE);
2686 add_column(COL_RO);
2687 add_column(COL_TYPE);
3c9d13da 2688 add_column(COL_TARGETS);
2a4c734b
MB
2689 }
2690
43bca827
MB
2691 if (outarg && string_add_to_idarray(outarg, columns, ARRAY_SIZE(columns),
2692 &ncolumns, column_name_to_id) < 0)
2693 return EXIT_FAILURE;
2694
b9dd5721 2695 if (lsblk->all_devices == 0 && nexcludes == 0 && nincludes == 0)
2a4c734b 2696 excludes[nexcludes++] = 1; /* default: ignore RAM disks */
9554f7ab 2697
5e2305ca
KZ
2698 if (lsblk->sort_id < 0)
2699 /* Since Linux 4.8 we have sort devices by default, because
2700 * /sys is no more sorted */
2701 lsblk->sort_id = COL_MAJMIN;
2702
91b636b5
KZ
2703 /* For --{inverse,raw,pairs} --list we still follow parent->child relation */
2704 if (!(lsblk->flags & LSBLK_TREE)
2705 && (lsblk->inverse || lsblk->flags & LSBLK_EXPORT || lsblk->flags & LSBLK_RAW))
091683a8
KZ
2706 lsblk->force_tree_order = 1;
2707
a6dc8dcd
KZ
2708 if (lsblk->sort_id >= 0 && column_id_to_number(lsblk->sort_id) < 0) {
2709 /* the sort column is not between output columns -- add as hidden */
be685b98 2710 add_column(lsblk->sort_id);
a6dc8dcd
KZ
2711 lsblk->sort_hidden = 1;
2712 }
642048e4 2713
dc4662f0
KZ
2714 if (lsblk->dedup_id >= 0 && column_id_to_number(lsblk->dedup_id) < 0) {
2715 /* the deduplication column is not between output columns -- add as hidden */
2716 add_column(lsblk->dedup_id);
2717 lsblk->dedup_hidden = 1;
2718 }
2719
e0c016f1 2720 lsblk_mnt_init();
5a89aa99 2721 ul_path_init_debug();
9554f7ab 2722
2a4c734b
MB
2723 /*
2724 * initialize output columns
2725 */
0925a9dd 2726 if (!(lsblk->table = scols_new_table()))
780ce22c 2727 errx(EXIT_FAILURE, _("failed to allocate output table"));
341c4ae2
KZ
2728 scols_table_enable_raw(lsblk->table, !!(lsblk->flags & LSBLK_RAW));
2729 scols_table_enable_export(lsblk->table, !!(lsblk->flags & LSBLK_EXPORT));
25fb0638 2730 scols_table_enable_shellvar(lsblk->table, !!(lsblk->flags & LSBLK_SHELLVAR));
341c4ae2
KZ
2731 scols_table_enable_ascii(lsblk->table, !!(lsblk->flags & LSBLK_ASCII));
2732 scols_table_enable_json(lsblk->table, !!(lsblk->flags & LSBLK_JSON));
2733 scols_table_enable_noheadings(lsblk->table, !!(lsblk->flags & LSBLK_NOHEADINGS));
2a4c734b 2734
341c4ae2 2735 if (lsblk->flags & LSBLK_JSON)
4a102a48 2736 scols_table_set_name(lsblk->table, "blockdevices");
7636d906
KZ
2737 if (width) {
2738 scols_table_set_termwidth(lsblk->table, width);
2739 scols_table_set_termforce(lsblk->table, SCOLS_TERMFORCE_ALWAYS);
2740 }
4a102a48 2741
2a4c734b 2742 for (i = 0; i < ncolumns; i++) {
37b2b3fa 2743 const struct colinfo *ci = get_column_info(i);
642048e4
KZ
2744 struct libscols_column *cl;
2745 int id = get_column_id(i), fl = ci->flags;
2a4c734b 2746
b9c088f2
KZ
2747 if ((lsblk->flags & LSBLK_TREE)
2748 && has_tree_col == 0
2749 && id == lsblk->tree_id) {
2750 fl |= SCOLS_FL_TREE;
2751 fl &= ~SCOLS_FL_RIGHT;
2752 has_tree_col = 1;
2753 }
2754
a6dc8dcd
KZ
2755 if (lsblk->sort_hidden && lsblk->sort_id == id)
2756 fl |= SCOLS_FL_HIDDEN;
dc4662f0
KZ
2757 if (lsblk->dedup_hidden && lsblk->dedup_id == id)
2758 fl |= SCOLS_FL_HIDDEN;
2a4c734b 2759
fb376180
KZ
2760 if (force_tree
2761 && lsblk->flags & LSBLK_JSON
2762 && has_tree_col == 0
2763 && i + 1 == ncolumns)
2764 /* The "--tree --json" specified, but no column with
2765 * SCOLS_FL_TREE yet; force it for the last column
2766 */
2767 fl |= SCOLS_FL_TREE;
2768
642048e4
KZ
2769 cl = scols_table_new_column(lsblk->table, ci->name, ci->whint, fl);
2770 if (!cl) {
780ce22c 2771 warn(_("failed to allocate output column"));
2a4c734b
MB
2772 goto leave;
2773 }
642048e4
KZ
2774 if (!lsblk->sort_col && lsblk->sort_id == id) {
2775 lsblk->sort_col = cl;
af5fa636 2776 lsblk->rawdata = 1;
642048e4 2777 scols_column_set_cmpfunc(cl,
407e0ea6
KZ
2778 ci->type == COLTYPE_NUM ? cmp_u64_cells :
2779 ci->type == COLTYPE_SIZE ? cmp_u64_cells :
2780 ci->type == COLTYPE_SORTNUM ? cmp_u64_cells : scols_cmpstr_cells,
2781 NULL);
2782 }
c6648d2d 2783 /* multi-line cells (now used for MOUNTPOINTS) */
2eab8ab4
KZ
2784 if (fl & SCOLS_FL_WRAP)
2785 scols_column_set_wrapfunc(cl, NULL, scols_wrapzero_nextchunk, NULL);
c6648d2d 2786
f7c9b28a 2787 set_column_type(ci, cl, fl);
2a4c734b
MB
2788 }
2789
9f55de78
KZ
2790 if (lsblk->filter)
2791 init_scols_filter(lsblk->table, lsblk->filter);
3e0b4a82 2792
9f55de78 2793 if (lsblk->hlighter && colors_init(UL_COLORMODE_AUTO, "lsblk") > 0) {
c3317cdc
KZ
2794 lsblk->hlighter_seq = color_scheme_get_sequence("highlight-line", UL_COLOR_RED);
2795 scols_table_enable_colors(lsblk->table, 1);
9f55de78 2796 init_scols_filter(lsblk->table, lsblk->hlighter);
c3317cdc 2797 }
0398951e
KZ
2798 for (i = 0; i < lsblk->ncts; i++)
2799 init_scols_filter(lsblk->table, lsblk->ct_filters[i]);
c3317cdc 2800
f43b8297
KZ
2801 tr = lsblk_new_devtree();
2802 if (!tr)
2803 err(EXIT_FAILURE, _("failed to allocate device tree"));
2804
e7b5353e
KZ
2805 if (optind == argc) {
2806 int rc = lsblk->inverse ?
2807 process_all_devices_inverse(tr) :
2808 process_all_devices(tr);
2809
2810 status = rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
2811 } else {
1539750f
KZ
2812 int cnt = 0, cnt_err = 0;
2813
2814 while (optind < argc) {
291606fa 2815 if (process_one_device(tr, argv[optind++]) != 0)
1539750f
KZ
2816 cnt_err++;
2817 cnt++;
2818 }
2819 status = cnt == 0 ? EXIT_FAILURE : /* nothing */
2820 cnt == cnt_err ? LSBLK_EXIT_ALLFAILED :/* all failed */
2821 cnt_err ? LSBLK_EXIT_SOMEOK : /* some ok */
2822 EXIT_SUCCESS; /* all success */
2823 }
2a4c734b 2824
dc4662f0
KZ
2825 if (lsblk->dedup_id > -1) {
2826 devtree_set_dedupkeys(tr, lsblk->dedup_id);
2827 lsblk_devtree_deduplicate_devices(tr);
2828 }
2829
f43b8297
KZ
2830 devtree_to_scols(tr, lsblk->table);
2831
642048e4
KZ
2832 if (lsblk->sort_col)
2833 scols_sort_table(lsblk->table, lsblk->sort_col);
091683a8
KZ
2834 if (lsblk->force_tree_order)
2835 scols_sort_table_by_tree(lsblk->table);
642048e4 2836
9bd4e5c0 2837 scols_print_table(lsblk->table);
2a4c734b 2838
0398951e
KZ
2839 if (lsblk->ncts)
2840 print_counters();
2a4c734b 2841leave:
af5fa636 2842 if (lsblk->rawdata)
1aca4989 2843 unref_table_rawdata(lsblk->table);
642048e4 2844
9bd4e5c0 2845 scols_unref_table(lsblk->table);
9f55de78
KZ
2846 scols_unref_filter(lsblk->filter);
2847 scols_unref_filter(lsblk->hlighter);
50fccba1 2848
0398951e
KZ
2849 for (i = 0; i < lsblk->ncts; i++)
2850 scols_unref_filter(lsblk->ct_filters[i]);
2851 free(lsblk->ct_filters);
50fccba1 2852
e0c016f1 2853 lsblk_mnt_deinit();
ccafadb7 2854 lsblk_properties_deinit();
57502dd3 2855 lsblk_unref_devtree(tr);
ccafadb7 2856
2a4c734b
MB
2857 return status;
2858}