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