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