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