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