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