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