]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - misc-utils/lsblk.c
lsblk: remove unused reset_device()
[thirdparty/util-linux.git] / misc-utils / lsblk.c
index d797f6295e952aac25cd17d75e7894a1829f47d1..4f13e8b2a117e4df856176780b6477786e0c547f 100644 (file)
 #include <getopt.h>
 #include <stdlib.h>
 #include <unistd.h>
-#include <stdint.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <dirent.h>
 #include <fcntl.h>
 #include <string.h>
 #include <sys/ioctl.h>
-#include <inttypes.h>
 #include <stdarg.h>
 #include <locale.h>
 #include <pwd.h>
 #include <grp.h>
 #include <ctype.h>
+#include <assert.h>
 
 #include <blkid.h>
-#include <libmount.h>
-#include <libsmartcols.h>
-
-#ifdef HAVE_LIBUDEV
-#include <libudev.h>
-#endif
-
-#include <assert.h>
 
 #include "c.h"
 #include "pathnames.h"
 #include "strutils.h"
 #include "sysfs.h"
 #include "closestream.h"
-#include "mangle.h"
 #include "optutils.h"
 
-#include "debug.h"
+#include "lsblk.h"
 
-static UL_DEBUG_DEFINE_MASK(lsblk);
+UL_DEBUG_DEFINE_MASK(lsblk);
 UL_DEBUG_DEFINE_MASKNAMES(lsblk) = UL_DEBUG_EMPTY_MASKNAMES;
 
-#define LSBLK_DEBUG_INIT       (1 << 1)
-#define LSBLK_DEBUG_FILTER     (1 << 2)
-#define LSBLK_DEBUG_DEV                (1 << 3)
-#define LSBLK_DEBUG_CXT                (1 << 4)
-#define LSBLK_DEBUG_ALL                0xFFFF
-
-#define DBG(m, x)       __UL_DBG(lsblk, LSBLK_DEBUG_, m, x)
-#define ON_DBG(m, x)    __UL_DBG_CALL(lsblk, LSBLK_DEBUG_, m, x)
-
-#define UL_DEBUG_CURRENT_MASK  UL_DEBUG_MASK(lsblk)
-#include "debugobj.h"
-
 #define LSBLK_EXIT_SOMEOK 64
 #define LSBLK_EXIT_ALLFAILED 32
 
@@ -87,8 +65,13 @@ static int column_id_to_number(int id);
 enum {
        COL_NAME = 0,
        COL_KNAME,
+       COL_PATH,
        COL_MAJMIN,
+       COL_FSAVAIL,
+       COL_FSSIZE,
        COL_FSTYPE,
+       COL_FSUSED,
+       COL_FSUSEPERC,
        COL_TARGET,
        COL_LABEL,
        COL_UUID,
@@ -167,9 +150,16 @@ struct colinfo {
 static struct colinfo infos[] = {
        [COL_NAME]   = { "NAME",    0.25, SCOLS_FL_TREE | SCOLS_FL_NOEXTREMES, N_("device name") },
        [COL_KNAME]  = { "KNAME",   0.3, 0, N_("internal kernel device name") },
-       [COL_PKNAME] = { "PKNAME",   0.3, 0, N_("internal parent kernel device name") },
+       [COL_PKNAME] = { "PKNAME",  0.3, 0, N_("internal parent kernel device name") },
+       [COL_PATH]   = { "PATH",    0.3,  0, N_("path to the device node") },
        [COL_MAJMIN] = { "MAJ:MIN", 6, 0, N_("major:minor device number"), COLTYPE_SORTNUM },
-       [COL_FSTYPE] = { "FSTYPE",  0.1, SCOLS_FL_TRUNC, N_("filesystem type") },
+
+       [COL_FSAVAIL]   = { "FSAVAIL", 5, SCOLS_FL_RIGHT, N_("filesystem size available") },
+       [COL_FSSIZE]    = { "FSSIZE", 5, SCOLS_FL_RIGHT, N_("filesystem size") },
+       [COL_FSTYPE]    = { "FSTYPE", 0.1, SCOLS_FL_TRUNC, N_("filesystem type") },
+       [COL_FSUSED]    = { "FSUSED", 5, SCOLS_FL_RIGHT, N_("filesystem size used") },
+       [COL_FSUSEPERC] = { "FSUSE%", 3, SCOLS_FL_RIGHT, N_("filesystem use percentage") },
+
        [COL_TARGET] = { "MOUNTPOINT", 0.10, SCOLS_FL_TRUNC, N_("where the device is mounted") },
        [COL_LABEL]  = { "LABEL",   0.1, 0, N_("filesystem LABEL") },
        [COL_UUID]   = { "UUID",    36,  0, N_("filesystem UUID") },
@@ -217,25 +207,7 @@ static struct colinfo infos[] = {
        [COL_ZONED]  = { "ZONED", 0.3, 0, N_("zone model") },
 };
 
-struct lsblk {
-       struct libscols_table *table;   /* output table */
-       struct libscols_column *sort_col;/* sort output by this column */
-       int sort_id;
-
-       const char *sysroot;
-       int flags;                      /* LSBLK_* */
-
-       unsigned int all_devices:1;     /* print all devices, including empty */
-       unsigned int bytes:1;           /* print SIZE in bytes */
-       unsigned int inverse:1;         /* print inverse dependencies */
-       unsigned int nodeps:1;          /* don't print slaves/holders */
-       unsigned int scsi:1;            /* print only device with HCTL (SCSI) */
-       unsigned int paths:1;           /* print devnames with "/dev" prefix */
-       unsigned int sort_hidden:1;     /* sort column not between output columns */
-       unsigned int force_tree_order:1;/* sort lines by parent->tree relation */
-};
-
-static struct lsblk *lsblk;    /* global handler */
+struct lsblk *lsblk;   /* global handler */
 
 /* columns[] array specifies all currently wanted output column. The columns
  * are defined by infos[] array and you can specify (on command line) each
@@ -266,51 +238,6 @@ static size_t nexcludes;
 static int includes[256];
 static size_t nincludes;
 
-static struct libmnt_table *mtab, *swaps;
-static struct libmnt_cache *mntcache;
-
-#ifdef HAVE_LIBUDEV
-static struct udev *udev;
-#endif
-
-struct blkdev_cxt {
-       struct blkdev_cxt *parent;
-
-       struct libscols_line *scols_line;
-       struct stat     st;
-
-       char *name;             /* kernel name in /sys/block */
-       char *dm_name;          /* DM name (dm/block) */
-
-       char *filename;         /* path to device node */
-
-       struct path_cxt *sysfs;
-
-       int partition;          /* is partition? TRUE/FALSE */
-
-       int probed;             /* already probed */
-       char *fstype;           /* detected fs, NULL or "?" if cannot detect */
-       char *uuid;             /* filesystem UUID (or stack uuid) */
-       char *ptuuid;           /* partition table UUID */
-       char *pttype;           /* partition table type */
-       char *label;            /* filesystem label */
-       char *parttype;         /* partition type UUID */
-       char *partuuid;         /* partition UUID */
-       char *partlabel;        /* partition label */
-       char *partflags;        /* partition flags */
-       char *wwn;              /* storage WWN */
-       char *serial;           /* disk serial number */
-
-       int npartitions;        /* # of partitions this device has */
-       int nholders;           /* # of devices mapped directly to this device
-                                * /sys/block/.../holders */
-       int nslaves;            /* # of devices this device maps to */
-       int maj, min;           /* devno */
-       int discard;            /* supports discard */
-
-       uint64_t size;          /* device size */
-};
-
 static void lsblk_init_debug(void)
 {
        __UL_INIT_DEBUG_FROM_ENV(lsblk, LSBLK_DEBUG_, 0, LSBLK_DEBUG);
@@ -390,32 +317,6 @@ static int column_id_to_number(int id)
        return -1;
 }
 
-static void reset_blkdev_cxt(struct blkdev_cxt *cxt)
-{
-       if (!cxt)
-               return;
-
-       DBG(CXT, ul_debugobj(cxt, "reset"));
-
-       free(cxt->name);
-       free(cxt->dm_name);
-       free(cxt->filename);
-       free(cxt->fstype);
-       free(cxt->uuid);
-       free(cxt->ptuuid);
-       free(cxt->pttype);
-       free(cxt->label);
-       free(cxt->parttype);
-       free(cxt->partuuid);
-       free(cxt->partlabel);
-       free(cxt->wwn);
-       free(cxt->serial);
-
-       ul_unref_path(cxt->sysfs);
-
-       memset(cxt, 0, sizeof(*cxt));
-}
-
 static int is_dm(const char *name)
 {
        return strncmp(name, "dm-", 3) ? 0 : 1;
@@ -438,247 +339,30 @@ static struct dirent *xreaddir(DIR *dp)
        return d;
 }
 
-static char *get_device_path(struct blkdev_cxt *cxt)
+static char *get_device_path(struct lsblk_device *dev)
 {
        char path[PATH_MAX];
 
-       assert(cxt);
-       assert(cxt->name);
+       assert(dev);
+       assert(dev->name);
 
-       if (is_dm(cxt->name))
-               return __canonicalize_dm_name(lsblk->sysroot, cxt->name);
+       if (is_dm(dev->name))
+               return __canonicalize_dm_name(lsblk->sysroot, dev->name);
 
-       snprintf(path, sizeof(path), "/dev/%s", cxt->name);
+       snprintf(path, sizeof(path), "/dev/%s", dev->name);
        sysfs_devname_sys_to_dev(path);
        return xstrdup(path);
 }
 
-static int table_parser_errcb(struct libmnt_table *tb __attribute__((__unused__)),
-                       const char *filename, int line)
-{
-       if (filename)
-               warnx(_("%s: parse error at line %d -- ignored"), filename, line);
-       return 1;
-}
-
-static int is_active_swap(const char *filename)
-{
-       if (!swaps) {
-               swaps = mnt_new_table();
-               if (!swaps)
-                       return 0;
-               if (!mntcache)
-                       mntcache = mnt_new_cache();
-
-               mnt_table_set_parser_errcb(swaps, table_parser_errcb);
-               mnt_table_set_cache(swaps, mntcache);
-
-               if (!lsblk->sysroot)
-                       mnt_table_parse_swaps(swaps, NULL);
-               else {
-                       char buf[PATH_MAX];
-                       snprintf(buf, sizeof(buf), "%s" _PATH_PROC_SWAPS, lsblk->sysroot);
-                       mnt_table_parse_swaps(swaps, buf);
-               }
-       }
-
-       return mnt_table_find_srcpath(swaps, filename, MNT_ITER_BACKWARD) != NULL;
-}
-
-static char *get_device_mountpoint(struct blkdev_cxt *cxt)
-{
-       struct libmnt_fs *fs;
-       const char *fsroot;
-
-       assert(cxt);
-       assert(cxt->filename);
-
-       if (!mtab) {
-               mtab = mnt_new_table();
-               if (!mtab)
-                       return NULL;
-               if (!mntcache)
-                       mntcache = mnt_new_cache();
-
-               mnt_table_set_parser_errcb(mtab, table_parser_errcb);
-               mnt_table_set_cache(mtab, mntcache);
-
-               if (!lsblk->sysroot)
-                       mnt_table_parse_mtab(mtab, NULL);
-               else {
-                       char buf[PATH_MAX];
-                       snprintf(buf, sizeof(buf), "%s" _PATH_PROC_MOUNTINFO, lsblk->sysroot);
-                       mnt_table_parse_mtab(mtab, buf);
-               }
-       }
-
-       /* Note that maj:min in /proc/self/mountinfo does not have to match with
-        * devno as returned by stat(), so we have to try devname too
-        */
-       fs = mnt_table_find_devno(mtab, makedev(cxt->maj, cxt->min), MNT_ITER_BACKWARD);
-       if (!fs)
-               fs = mnt_table_find_srcpath(mtab, cxt->filename, MNT_ITER_BACKWARD);
-       if (!fs)
-               return is_active_swap(cxt->filename) ? xstrdup("[SWAP]") : NULL;
-
-       /* found */
-       fsroot = mnt_fs_get_root(fs);
-       if (fsroot && strcmp(fsroot, "/") != 0) {
-               /* hmm.. we found bind mount or btrfs subvolume, let's try to
-                * get real FS root mountpoint */
-               struct libmnt_fs *rfs;
-               struct libmnt_iter *itr = mnt_new_iter(MNT_ITER_BACKWARD);
-
-               mnt_table_set_iter(mtab, itr, fs);
-               while (mnt_table_next_fs(mtab, itr, &rfs) == 0) {
-                       fsroot = mnt_fs_get_root(rfs);
-                       if ((!fsroot || strcmp(fsroot, "/") == 0)
-                           && mnt_fs_match_source(rfs, cxt->filename, mntcache)) {
-                               fs = rfs;
-                               break;
-                       }
-               }
-               mnt_free_iter(itr);
-       }
-
-       DBG(DEV, ul_debugobj(cxt, "mountpoint: %s", mnt_fs_get_target(fs)));
-       return xstrdup(mnt_fs_get_target(fs));
-}
-
-#ifndef HAVE_LIBUDEV
-static int get_udev_properties(struct blkdev_cxt *cxt
-                               __attribute__((__unused__)))
-{
-       return -1;
-}
-#else
-static int get_udev_properties(struct blkdev_cxt *cxt)
-{
-       struct udev_device *dev;
-
-       if (cxt->probed)
-               return 0;               /* already done */
-
-       if (!udev)
-               udev = udev_new();
-       if (!udev)
-               return -1;
-
-       dev = udev_device_new_from_subsystem_sysname(udev, "block", cxt->name);
-       if (dev) {
-               const char *data;
-
-               if ((data = udev_device_get_property_value(dev, "ID_FS_LABEL_ENC"))) {
-                       cxt->label = xstrdup(data);
-                       unhexmangle_string(cxt->label);
-               }
-               if ((data = udev_device_get_property_value(dev, "ID_FS_UUID_ENC"))) {
-                       cxt->uuid = xstrdup(data);
-                       unhexmangle_string(cxt->uuid);
-               }
-               if ((data = udev_device_get_property_value(dev, "ID_PART_TABLE_UUID")))
-                       cxt->ptuuid = xstrdup(data);
-               if ((data = udev_device_get_property_value(dev, "ID_PART_TABLE_TYPE")))
-                       cxt->pttype = xstrdup(data);
-               if ((data = udev_device_get_property_value(dev, "ID_PART_ENTRY_NAME"))) {
-                       cxt->partlabel = xstrdup(data);
-                       unhexmangle_string(cxt->partlabel);
-               }
-               if ((data = udev_device_get_property_value(dev, "ID_FS_TYPE")))
-                       cxt->fstype = xstrdup(data);
-               if ((data = udev_device_get_property_value(dev, "ID_PART_ENTRY_TYPE")))
-                       cxt->parttype = xstrdup(data);
-               if ((data = udev_device_get_property_value(dev, "ID_PART_ENTRY_UUID")))
-                       cxt->partuuid = xstrdup(data);
-               if ((data = udev_device_get_property_value(dev, "ID_PART_ENTRY_FLAGS")))
-                       cxt->partflags = xstrdup(data);
-
-               data = udev_device_get_property_value(dev, "ID_WWN_WITH_EXTENSION");
-               if (!data)
-                       data = udev_device_get_property_value(dev, "ID_WWN");
-               if (data)
-                       cxt->wwn = xstrdup(data);
-
-               if ((data = udev_device_get_property_value(dev, "ID_SERIAL_SHORT")))
-                       cxt->serial = xstrdup(data);
-               udev_device_unref(dev);
-               cxt->probed = 1;
-               DBG(DEV, ul_debugobj(cxt, "%s: found udev properties", cxt->name));
-       }
-
-       return cxt->probed == 1 ? 0 : -1;
-
-}
-#endif /* HAVE_LIBUDEV */
-
-static void probe_device(struct blkdev_cxt *cxt)
-{
-       blkid_probe pr = NULL;
-
-       if (cxt->probed)
-               return;
-
-       if (!cxt->size)
-               return;
-
-       /* try udev DB */
-       if (get_udev_properties(cxt) == 0)
-               return;                         /* success */
-
-       cxt->probed = 1;
-
-       /* try libblkid (fallback) */
-       if (getuid() != 0)
-               return;                         /* no permissions to read from the device */
-
-       pr = blkid_new_probe_from_filename(cxt->filename);
-       if (!pr)
-               return;
-
-       blkid_probe_enable_superblocks(pr, 1);
-       blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_LABEL |
-                                             BLKID_SUBLKS_UUID |
-                                             BLKID_SUBLKS_TYPE);
-       blkid_probe_enable_partitions(pr, 1);
-       blkid_probe_set_partitions_flags(pr, BLKID_PARTS_ENTRY_DETAILS);
-
-       if (!blkid_do_safeprobe(pr)) {
-               const char *data = NULL;
-
-               if (!blkid_probe_lookup_value(pr, "TYPE", &data, NULL))
-                       cxt->fstype = xstrdup(data);
-               if (!blkid_probe_lookup_value(pr, "UUID", &data, NULL))
-                       cxt->uuid = xstrdup(data);
-               if (!blkid_probe_lookup_value(pr, "PTUUID", &data, NULL))
-                       cxt->ptuuid = xstrdup(data);
-               if (!blkid_probe_lookup_value(pr, "PTTYPE", &data, NULL))
-                       cxt->pttype = xstrdup(data);
-               if (!blkid_probe_lookup_value(pr, "LABEL", &data, NULL))
-                       cxt->label = xstrdup(data);
-               if (!blkid_probe_lookup_value(pr, "PART_ENTRY_TYPE", &data, NULL))
-                       cxt->parttype = xstrdup(data);
-               if (!blkid_probe_lookup_value(pr, "PART_ENTRY_UUID", &data, NULL))
-                       cxt->partuuid = xstrdup(data);
-               if (!blkid_probe_lookup_value(pr, "PART_ENTRY_NAME", &data, NULL))
-                       cxt->partlabel = xstrdup(data);
-               if (!blkid_probe_lookup_value(pr, "PART_ENTRY_FLAGS", &data, NULL))
-                       cxt->partflags = xstrdup(data);
-               DBG(DEV, ul_debugobj(cxt, "%s: found blkid properties", cxt->name));
-       }
-
-       blkid_free_probe(pr);
-       return;
-}
-
-static int is_readonly_device(struct blkdev_cxt *cxt)
+static int is_readonly_device(struct lsblk_device *dev)
 {
        int fd, ro = 0;
 
-       if (ul_path_scanf(cxt->sysfs, "ro", "%d", &ro) == 1)
+       if (ul_path_scanf(dev->sysfs, "ro", "%d", &ro) == 1)
                return ro;
 
        /* fallback if "ro" attribute does not exist */
-       fd = open(cxt->filename, O_RDONLY);
+       fd = open(dev->filename, O_RDONLY);
        if (fd != -1) {
                if (ioctl(fd, BLKROGET, &ro) != 0)
                        ro = 0;
@@ -687,12 +371,12 @@ static int is_readonly_device(struct blkdev_cxt *cxt)
        return ro;
 }
 
-static char *get_scheduler(struct blkdev_cxt *cxt)
+static char *get_scheduler(struct lsblk_device *dev)
 {
        char buf[128];
        char *p, *res = NULL;
 
-       if (ul_path_read_buffer(cxt->sysfs, buf, sizeof(buf), "queue/scheduler"))
+       if (ul_path_read_buffer(dev->sysfs, buf, sizeof(buf), "queue/scheduler") == 0)
                return NULL;
        p = strchr(buf, '[');
        if (p) {
@@ -707,16 +391,19 @@ static char *get_scheduler(struct blkdev_cxt *cxt)
        return res;
 }
 
-static char *get_type(struct blkdev_cxt *cxt)
+static char *get_type(struct lsblk_device *dev)
 {
        char *res = NULL, *p;
 
-       if (is_dm(cxt->name)) {
+       if (cxt->partition)
+               return xstrdup("part");
+
+       if (is_dm(dev->name)) {
                char *dm_uuid = NULL;
 
                /* The DM_UUID prefix should be set to subsystem owning
                 * the device - LVM, CRYPT, DMRAID, MPATH, PART */
-               if (ul_path_read_string(cxt->sysfs, &dm_uuid, "dm/uuid") == 0
+               if (ul_path_read_string(dev->sysfs, &dm_uuid, "dm/uuid") > 0
                    && dm_uuid) {
                        char *tmp = dm_uuid;
                        char *dm_uuid_prefix = strsep(&tmp, "-");
@@ -735,23 +422,23 @@ static char *get_type(struct blkdev_cxt *cxt)
                        /* No UUID or no prefix - just mark it as DM device */
                        res = xstrdup("dm");
 
-       } else if (!strncmp(cxt->name, "loop", 4)) {
+       } else if (!strncmp(dev->name, "loop", 4)) {
                res = xstrdup("loop");
 
-       } else if (!strncmp(cxt->name, "md", 2)) {
+       } else if (!strncmp(dev->name, "md", 2)) {
                char *md_level = NULL;
 
-               ul_path_read_string(cxt->sysfs, &md_level, "md/level");
+               ul_path_read_string(dev->sysfs, &md_level, "md/level");
                res = md_level ? md_level : xstrdup("md");
 
        } else {
                const char *type = NULL;
                int x = 0;
 
-               if (ul_path_read_s32(cxt->sysfs, &x, "device/type") == 0)
+               if (ul_path_read_s32(dev->sysfs, &x, "device/type") == 0)
                        type = blkdev_scsi_type_to_name(x);
                if (!type)
-                       type = cxt->partition ? "part" : "disk";
+                       type = "disk";
                res = xstrdup(type);
        }
 
@@ -761,9 +448,9 @@ static char *get_type(struct blkdev_cxt *cxt)
 }
 
 /* Thanks to lsscsi code for idea of detection logic used here */
-static char *get_transport(struct blkdev_cxt *cxt)
+static char *get_transport(struct lsblk_device *dev)
 {
-       struct path_cxt *sysfs = cxt->sysfs;
+       struct path_cxt *sysfs = dev->sysfs;
        char *attr = NULL;
        const char *trans = NULL;
 
@@ -810,23 +497,23 @@ static char *get_transport(struct blkdev_cxt *cxt)
                        trans = "ata";
                free(attr);
 
-       } else if (strncmp(cxt->name, "nvme", 4) == 0)
+       } else if (strncmp(dev->name, "nvme", 4) == 0)
                trans = "nvme";
 
        return trans ? xstrdup(trans) : NULL;
 }
 
-static char *get_subsystems(struct blkdev_cxt *cxt)
+static char *get_subsystems(struct lsblk_device *dev)
 {
        char path[PATH_MAX];
        char *sub, *chain, *res = NULL;
        size_t len = 0, last = 0;
 
-       chain = sysfs_blkdev_get_devchain(cxt->sysfs, path, sizeof(path));
+       chain = sysfs_blkdev_get_devchain(dev->sysfs, path, sizeof(path));
        if (!chain)
                return NULL;
 
-       while (sysfs_blkdev_next_subsystem(cxt->sysfs, chain, &sub) == 0) {
+       while (sysfs_blkdev_next_subsystem(dev->sysfs, chain, &sub) == 0) {
                size_t sz;
 
                /* don't create "block:scsi:scsi", but "block:scsi" */
@@ -925,230 +612,295 @@ static void unref_sortdata(struct libscols_table *tb)
        scols_free_iter(itr);
 }
 
-static void set_scols_data(struct blkdev_cxt *cxt, int col, int id, struct libscols_line *ln)
+static char *get_vfs_attribute(struct lsblk_device *dev, int id)
 {
-       int sort = 0, st_rc = 0;
-       char *str = NULL;
+       char *sizestr;
+       uint64_t vfs_attr = 0;
+       char *mnt;
+
+       if (!dev->fsstat.f_blocks) {
+               mnt = lsblk_device_get_mountpoint(dev);
+               if (!mnt || dev->is_swap)
+                       return NULL;
+               if (statvfs(mnt, &dev->fsstat) != 0)
+                       return NULL;
+       }
+
+       switch(id) {
+       case COL_FSSIZE:
+               vfs_attr = dev->fsstat.f_frsize * dev->fsstat.f_blocks;
+               break;
+       case COL_FSAVAIL:
+               vfs_attr = dev->fsstat.f_frsize * dev->fsstat.f_bavail;
+               break;
+       case COL_FSUSED:
+               vfs_attr = dev->fsstat.f_frsize * (dev->fsstat.f_blocks - dev->fsstat.f_bfree);
+               break;
+       case COL_FSUSEPERC:
+               if (dev->fsstat.f_blocks == 0)
+                       return xstrdup("-");
+
+               xasprintf(&sizestr, "%.0f%%",
+                               (double)(dev->fsstat.f_blocks - dev->fsstat.f_bfree) /
+                               dev->fsstat.f_blocks * 100);
+               return sizestr;
+       }
+
+       if (!vfs_attr)
+               sizestr = xstrdup("0");
+       else if (lsblk->bytes)
+               xasprintf(&sizestr, "%ju", vfs_attr);
+       else
+               sizestr = size_to_human_string(SIZE_SUFFIX_1LETTER, vfs_attr);
+
+       return sizestr;
+}
 
-       if (!cxt->st.st_rdev && (id == COL_OWNER || id == COL_GROUP ||
-                                id == COL_MODE))
-               st_rc = stat(cxt->filename, &cxt->st);
+static struct stat *device_get_stat(struct lsblk_device *dev)
+{
+       if (!dev->st.st_rdev)
+               stat(dev->filename, &dev->st);
+
+       return &dev->st;
+}
+
+static void set_scols_data(struct lsblk_device *dev, int col, int id, struct libscols_line *ln)
+{
+       struct lsblk_devprop *prop;
+       int sort = 0;
+       char *str = NULL;
 
        if (lsblk->sort_id == id)
                sort = 1;
 
        switch(id) {
        case COL_NAME:
-               str = cxt->dm_name ? mk_dm_name(cxt->dm_name) : mk_name(cxt->name);
+               str = dev->dm_name ? mk_dm_name(dev->dm_name) : mk_name(dev->name);
                break;
        case COL_KNAME:
-               str = mk_name(cxt->name);
+               str = mk_name(dev->name);
                break;
        case COL_PKNAME:
-               if (cxt->parent)
-                       str = mk_name(cxt->parent->name);
+               if (dev->parent)
+                       str = mk_name(dev->parent->name);
+               break;
+       case COL_PATH:
+               if (dev->filename)
+                       str = xstrdup(dev->filename);
                break;
        case COL_OWNER:
        {
-               struct passwd *pw = st_rc ? NULL : getpwuid(cxt->st.st_uid);
+               struct stat *st = device_get_stat(dev);
+               struct passwd *pw = st ? NULL : getpwuid(st->st_uid);
                if (pw)
                        str = xstrdup(pw->pw_name);
                break;
        }
        case COL_GROUP:
        {
-               struct group *gr = st_rc ? NULL : getgrgid(cxt->st.st_gid);
+               struct stat *st = device_get_stat(dev);
+               struct group *gr = st ? NULL : getgrgid(st->st_gid);
                if (gr)
                        str = xstrdup(gr->gr_name);
                break;
        }
        case COL_MODE:
        {
-               char md[11];
+               struct stat *st = device_get_stat(dev);
+               char md[11] = { '\0' };
 
-               if (!st_rc) {
-                       xstrmode(cxt->st.st_mode, md);
-                       str = xstrdup(md);
-               }
+               if (st)
+                       str = xstrdup(xstrmode(st->st_mode, md));
                break;
        }
        case COL_MAJMIN:
                if (is_parsable(lsblk))
-                       xasprintf(&str, "%u:%u", cxt->maj, cxt->min);
+                       xasprintf(&str, "%u:%u", dev->maj, dev->min);
                else
-                       xasprintf(&str, "%3u:%-3u", cxt->maj, cxt->min);
+                       xasprintf(&str, "%3u:%-3u", dev->maj, dev->min);
                if (sort)
-                       set_sortdata_u64(ln, col, makedev(cxt->maj, cxt->min));
+                       set_sortdata_u64(ln, col, makedev(dev->maj, dev->min));
                break;
        case COL_FSTYPE:
-               probe_device(cxt);
-               if (cxt->fstype)
-                       str = xstrdup(cxt->fstype);
+               prop = lsblk_device_get_properties(dev);
+               if (prop && prop->fstype)
+                       str = xstrdup(prop->fstype);
+               break;
+       case COL_FSSIZE:
+       case COL_FSAVAIL:
+       case COL_FSUSED:
+       case COL_FSUSEPERC:
+               str = get_vfs_attribute(dev, id);
                break;
        case COL_TARGET:
-               str = get_device_mountpoint(cxt);
+               str = xstrdup(lsblk_device_get_mountpoint(dev));
                break;
        case COL_LABEL:
-               probe_device(cxt);
-               if (cxt->label)
-                       str = xstrdup(cxt->label);
+               prop = lsblk_device_get_properties(dev);
+               if (prop && prop->label)
+                       str = xstrdup(prop->label);
                break;
        case COL_UUID:
-               probe_device(cxt);
-               if (cxt->uuid)
-                       str = xstrdup(cxt->uuid);
+               prop = lsblk_device_get_properties(dev);
+               if (prop && prop->uuid)
+                       str = xstrdup(prop->uuid);
                break;
        case COL_PTUUID:
-               probe_device(cxt);
-               if (cxt->ptuuid)
-                       str = xstrdup(cxt->ptuuid);
+               prop = lsblk_device_get_properties(dev);
+               if (prop && prop->ptuuid)
+                       str = xstrdup(prop->ptuuid);
                break;
        case COL_PTTYPE:
-               probe_device(cxt);
-               if (cxt->pttype)
-                       str = xstrdup(cxt->pttype);
+               prop = lsblk_device_get_properties(dev);
+               if (prop && prop->pttype)
+                       str = xstrdup(prop->pttype);
                break;
        case COL_PARTTYPE:
-               probe_device(cxt);
-               if (cxt->parttype)
-                       str = xstrdup(cxt->parttype);
+               prop = lsblk_device_get_properties(dev);
+               if (prop && prop->parttype)
+                       str = xstrdup(prop->parttype);
                break;
        case COL_PARTLABEL:
-               probe_device(cxt);
-               if (cxt->partlabel)
-                       str = xstrdup(cxt->partlabel);
+               prop = lsblk_device_get_properties(dev);
+               if (prop && prop->partlabel)
+                       str = xstrdup(prop->partlabel);
                break;
        case COL_PARTUUID:
-               probe_device(cxt);
-               if (cxt->partuuid)
-                       str = xstrdup(cxt->partuuid);
+               prop = lsblk_device_get_properties(dev);
+               if (prop && prop->partuuid)
+                       str = xstrdup(prop->partuuid);
                break;
        case COL_PARTFLAGS:
-               probe_device(cxt);
-               if (cxt->partflags)
-                       str = xstrdup(cxt->partflags);
+               prop = lsblk_device_get_properties(dev);
+               if (prop && prop->partflags)
+                       str = xstrdup(prop->partflags);
                break;
        case COL_WWN:
-               get_udev_properties(cxt);
-               if (cxt->wwn)
-                       str = xstrdup(cxt->wwn);
+               prop = lsblk_device_get_properties(dev);
+               if (prop && prop->wwn)
+                       str = xstrdup(prop->wwn);
                break;
        case COL_RA:
-               ul_path_read_string(cxt->sysfs, &str, "queue/read_ahead_kb");
+               ul_path_read_string(dev->sysfs, &str, "queue/read_ahead_kb");
                if (sort)
                        set_sortdata_u64_from_string(ln, col, str);
                break;
        case COL_RO:
-               str = xstrdup(is_readonly_device(cxt) ? "1" : "0");
+               str = xstrdup(is_readonly_device(dev) ? "1" : "0");
                break;
        case COL_RM:
-               ul_path_read_string(cxt->sysfs, &str, "removable");
-               if (!str && sysfs_blkdev_get_parent(cxt->sysfs))
-                       ul_path_read_string(sysfs_blkdev_get_parent(cxt->sysfs),
+               ul_path_read_string(dev->sysfs, &str, "removable");
+               if (!str && sysfs_blkdev_get_parent(dev->sysfs))
+                       ul_path_read_string(sysfs_blkdev_get_parent(dev->sysfs),
                                            &str,
                                            "removable");
                break;
        case COL_HOTPLUG:
-               str = sysfs_blkdev_is_hotpluggable(cxt->sysfs) ? xstrdup("1") : xstrdup("0");
+               str = sysfs_blkdev_is_hotpluggable(dev->sysfs) ? xstrdup("1") : xstrdup("0");
                break;
        case COL_ROTA:
-               ul_path_read_string(cxt->sysfs, &str, "queue/rotational");
+               ul_path_read_string(dev->sysfs, &str, "queue/rotational");
                break;
        case COL_RAND:
-               ul_path_read_string(cxt->sysfs, &str, "queue/add_random");
+               ul_path_read_string(dev->sysfs, &str, "queue/add_random");
                break;
        case COL_MODEL:
-               if (!cxt->partition && cxt->nslaves == 0)
-                       ul_path_read_string(cxt->sysfs, &str, "device/model");
+               if (!dev->partition && dev->nslaves == 0) {
+                       prop = lsblk_device_get_properties(dev);
+                       if (prop && prop->model)
+                               str = xstrdup(prop->model);
+                       else
+                               ul_path_read_string(dev->sysfs, &str, "device/model");
+               }
                break;
        case COL_SERIAL:
-               if (!cxt->partition && cxt->nslaves == 0) {
-                       get_udev_properties(cxt);
-                       if (cxt->serial)
-                               str = xstrdup(cxt->serial);
+               if (!dev->partition && dev->nslaves == 0) {
+                       prop = lsblk_device_get_properties(dev);
+                       if (prop && prop->serial)
+                               str = xstrdup(prop->serial);
                        else
-                               ul_path_read_string(cxt->sysfs, &str, "device/serial");
+                               ul_path_read_string(dev->sysfs, &str, "device/serial");
                }
                break;
        case COL_REV:
-               if (!cxt->partition && cxt->nslaves == 0)
-                       ul_path_read_string(cxt->sysfs, &str, "device/rev");
+               if (!dev->partition && dev->nslaves == 0)
+                       ul_path_read_string(dev->sysfs, &str, "device/rev");
                break;
        case COL_VENDOR:
-               if (!cxt->partition && cxt->nslaves == 0)
-                       ul_path_read_string(cxt->sysfs, &str, "device/vendor");
+               if (!dev->partition && dev->nslaves == 0)
+                       ul_path_read_string(dev->sysfs, &str, "device/vendor");
                break;
        case COL_SIZE:
-               if (!cxt->size)
+               if (!dev->size)
                        break;
                if (lsblk->bytes)
-                       xasprintf(&str, "%ju", cxt->size);
+                       xasprintf(&str, "%ju", dev->size);
                else
-                       str = size_to_human_string(SIZE_SUFFIX_1LETTER, cxt->size);
+                       str = size_to_human_string(SIZE_SUFFIX_1LETTER, dev->size);
                if (sort)
-                       set_sortdata_u64(ln, col, cxt->size);
+                       set_sortdata_u64(ln, col, dev->size);
                break;
        case COL_STATE:
-               if (!cxt->partition && !cxt->dm_name)
-                       ul_path_read_string(cxt->sysfs, &str, "device/state");
-               else if (cxt->dm_name) {
+               if (!dev->partition && !dev->dm_name)
+                       ul_path_read_string(dev->sysfs, &str, "device/state");
+               else if (dev->dm_name) {
                        int x = 0;
-                       if (ul_path_read_s32(cxt->sysfs, &x, "dm/suspended") == 0)
+                       if (ul_path_read_s32(dev->sysfs, &x, "dm/suspended") == 0)
                                str = xstrdup(x ? "suspended" : "running");
                }
                break;
        case COL_ALIOFF:
-               ul_path_read_string(cxt->sysfs, &str, "alignment_offset");
+               ul_path_read_string(dev->sysfs, &str, "alignment_offset");
                if (sort)
                        set_sortdata_u64_from_string(ln, col, str);
                break;
        case COL_MINIO:
-               ul_path_read_string(cxt->sysfs, &str, "queue/minimum_io_size");
+               ul_path_read_string(dev->sysfs, &str, "queue/minimum_io_size");
                if (sort)
                        set_sortdata_u64_from_string(ln, col, str);
                break;
        case COL_OPTIO:
-               ul_path_read_string(cxt->sysfs, &str, "queue/optimal_io_size");
+               ul_path_read_string(dev->sysfs, &str, "queue/optimal_io_size");
                if (sort)
                        set_sortdata_u64_from_string(ln, col, str);
                break;
        case COL_PHYSEC:
-               ul_path_read_string(cxt->sysfs, &str, "queue/physical_block_size");
+               ul_path_read_string(dev->sysfs, &str, "queue/physical_block_size");
                if (sort)
                        set_sortdata_u64_from_string(ln, col, str);
                break;
        case COL_LOGSEC:
-               ul_path_read_string(cxt->sysfs, &str, "queue/logical_block_size");
+               ul_path_read_string(dev->sysfs, &str, "queue/logical_block_size");
                if (sort)
                        set_sortdata_u64_from_string(ln, col, str);
                break;
        case COL_SCHED:
-               str = get_scheduler(cxt);
+               str = get_scheduler(dev);
                break;
        case COL_RQ_SIZE:
-               ul_path_read_string(cxt->sysfs, &str, "queue/nr_requests");
+               ul_path_read_string(dev->sysfs, &str, "queue/nr_requests");
                if (sort)
                        set_sortdata_u64_from_string(ln, col, str);
                break;
        case COL_TYPE:
-               str = get_type(cxt);
+               str = get_type(dev);
                break;
        case COL_HCTL:
        {
                int h, c, t, l;
-               if (sysfs_blkdev_scsi_get_hctl(cxt->sysfs, &h, &c, &t, &l) == 0)
+               if (sysfs_blkdev_scsi_get_hctl(dev->sysfs, &h, &c, &t, &l) == 0)
                        xasprintf(&str, "%d:%d:%d:%d", h, c, t, l);
                break;
        }
        case COL_TRANSPORT:
-               str = get_transport(cxt);
+               str = get_transport(dev);
                break;
        case COL_SUBSYS:
-               str = get_subsystems(cxt);
+               str = get_subsystems(dev);
                break;
        case COL_DALIGN:
-               if (cxt->discard)
-                       ul_path_read_string(cxt->sysfs, &str, "discard_alignment");
+               if (dev->discard)
+                       ul_path_read_string(dev->sysfs, &str, "discard_alignment");
                if (!str)
                        str = xstrdup("0");
                if (sort)
@@ -1156,12 +908,12 @@ static void set_scols_data(struct blkdev_cxt *cxt, int col, int id, struct libsc
                break;
        case COL_DGRAN:
                if (lsblk->bytes) {
-                       ul_path_read_string(cxt->sysfs, &str, "queue/discard_granularity");
+                       ul_path_read_string(dev->sysfs, &str, "queue/discard_granularity");
                        if (sort)
                                set_sortdata_u64_from_string(ln, col, str);
                } else {
                        uint64_t x;
-                       if (ul_path_read_u64(cxt->sysfs, &x, "queue/discard_granularity") == 0) {
+                       if (ul_path_read_u64(dev->sysfs, &x, "queue/discard_granularity") == 0) {
                                str = size_to_human_string(SIZE_SUFFIX_1LETTER, x);
                                if (sort)
                                        set_sortdata_u64(ln, col, x);
@@ -1170,12 +922,12 @@ static void set_scols_data(struct blkdev_cxt *cxt, int col, int id, struct libsc
                break;
        case COL_DMAX:
                if (lsblk->bytes) {
-                       ul_path_read_string(cxt->sysfs, &str, "queue/discard_max_bytes");
+                       ul_path_read_string(dev->sysfs, &str, "queue/discard_max_bytes");
                        if (sort)
                                set_sortdata_u64_from_string(ln, col, str);
                } else {
                        uint64_t x;
-                       if (ul_path_read_u64(cxt->sysfs, &x, "queue/discard_max_bytes") == 0) {
+                       if (ul_path_read_u64(dev->sysfs, &x, "queue/discard_max_bytes") == 0) {
                                str = size_to_human_string(SIZE_SUFFIX_1LETTER, x);
                                if (sort)
                                        set_sortdata_u64(ln, col, x);
@@ -1183,20 +935,20 @@ static void set_scols_data(struct blkdev_cxt *cxt, int col, int id, struct libsc
                }
                break;
        case COL_DZERO:
-               if (cxt->discard)
-                       ul_path_read_string(cxt->sysfs, &str, "queue/discard_zeroes_data");
+               if (dev->discard)
+                       ul_path_read_string(dev->sysfs, &str, "queue/discard_zeroes_data");
                if (!str)
                        str = xstrdup("0");
                break;
        case COL_WSAME:
                if (lsblk->bytes) {
-                       ul_path_read_string(cxt->sysfs, &str, "queue/write_same_max_bytes");
+                       ul_path_read_string(dev->sysfs, &str, "queue/write_same_max_bytes");
                        if (sort)
                                set_sortdata_u64_from_string(ln, col, str);
                } else {
                        uint64_t x;
 
-                       if (ul_path_read_u64(cxt->sysfs, &x, "queue/write_same_max_bytes") == 0) {
+                       if (ul_path_read_u64(dev->sysfs, &x, "queue/write_same_max_bytes") == 0) {
                                str = size_to_human_string(SIZE_SUFFIX_1LETTER, x);
                                if (sort)
                                        set_sortdata_u64(ln, col, x);
@@ -1206,7 +958,7 @@ static void set_scols_data(struct blkdev_cxt *cxt, int col, int id, struct libsc
                        str = xstrdup("0");
                break;
        case COL_ZONED:
-               ul_path_read_string(cxt->sysfs, &str, "queue/zoned");
+               ul_path_read_string(dev->sysfs, &str, "queue/zoned");
                break;
        };
 
@@ -1214,129 +966,148 @@ static void set_scols_data(struct blkdev_cxt *cxt, int col, int id, struct libsc
                err(EXIT_FAILURE, _("failed to add output data"));
 }
 
-static void fill_table_line(struct blkdev_cxt *cxt, struct libscols_line *scols_parent)
+static void device_to_scols(struct lsblk_device *dev, struct lsblk_device *parent, struct libscols_table *tab)
 {
        size_t i;
+       struct lsblk_iter itr;
+       struct lsblk_device *child = NULL;
 
-       cxt->scols_line = scols_table_new_line(lsblk->table, scols_parent);
-       if (!cxt->scols_line)
+       dev->scols_line = scols_table_new_line(tab, parent ? parent->scols_line : NULL);
+       if (!dev->scols_line)
                err(EXIT_FAILURE, _("failed to allocate output line"));
 
        for (i = 0; i < ncolumns; i++)
-               set_scols_data(cxt, i, get_column_id(i), cxt->scols_line);
+               set_scols_data(dev, i, get_column_id(i), dev->scols_line);
+
+       lsblk_reset_iter(&itr, LSBLK_ITER_FORWARD);
+
+       while (lsblk_device_next_child(dev, &itr, &child) == 0)
+               device_to_scols(child, dev, tab);
+}
+
+static void devtree_to_scols(struct lsblk_devtree *tr, struct libscols_table *tab)
+{
+       struct lsblk_iter itr;
+       struct lsblk_device *dev = NULL;
+
+       lsblk_reset_iter(&itr, LSBLK_ITER_FORWARD);
+
+       while (lsblk_devtree_next_root(tr, &itr, &dev) == 0)
+               device_to_scols(dev, NULL, tab);
 }
 
-static int set_cxt(struct blkdev_cxt *cxt,
-                   struct blkdev_cxt *parent,
-                   struct blkdev_cxt *wholedisk,
+static int set_device(struct lsblk_device *dev,
+                   struct lsblk_device *parent,
+                   struct lsblk_device *wholedisk,
                    const char *name)
 {
        dev_t devno;
 
-       DBG(CXT, ul_debugobj(cxt, "setting context for %s [parent=%p, wholedisk=%p]",
+       DBG(DEV, ul_debugobj(dev, "setting context for %s [parent=%p, wholedisk=%p]",
                                name, parent, wholedisk));
 
-       cxt->parent = parent;
-       cxt->name = xstrdup(name);
-       cxt->partition = wholedisk != NULL;
+       dev->parent = parent;
+       dev->name = xstrdup(name);
+       dev->partition = wholedisk != NULL;
 
-       cxt->filename = get_device_path(cxt);
-       if (!cxt->filename) {
-               DBG(CXT, ul_debugobj(cxt, "%s: failed to get device path", cxt->name));
+       dev->filename = get_device_path(dev);
+       if (!dev->filename) {
+               DBG(DEV, ul_debugobj(dev, "%s: failed to get device path", dev->name));
                return -1;
        }
-       DBG(CXT, ul_debugobj(cxt, "%s: filename=%s", cxt->name, cxt->filename));
+       DBG(DEV, ul_debugobj(dev, "%s: filename=%s", dev->name, dev->filename));
 
-       devno = __sysfs_devname_to_devno(lsblk->sysroot, cxt->name, wholedisk ? wholedisk->name : NULL);
+       devno = __sysfs_devname_to_devno(lsblk->sysroot, dev->name, wholedisk ? wholedisk->name : NULL);
 
        if (!devno) {
-               DBG(CXT, ul_debugobj(cxt, "%s: unknown device name", cxt->name));
+               DBG(DEV, ul_debugobj(dev, "%s: unknown device name", dev->name));
                return -1;
        }
 
        if (lsblk->inverse) {
-               cxt->sysfs = ul_new_sysfs_path(devno, wholedisk ? wholedisk->sysfs : NULL, lsblk->sysroot);
-               if (!cxt->sysfs) {
-                       DBG(CXT, ul_debugobj(cxt, "%s: failed to initialize sysfs handler", cxt->name));
+               dev->sysfs = ul_new_sysfs_path(devno, wholedisk ? wholedisk->sysfs : NULL, lsblk->sysroot);
+               if (!dev->sysfs) {
+                       DBG(DEV, ul_debugobj(dev, "%s: failed to initialize sysfs handler", dev->name));
                        return -1;
                }
                if (parent)
-                       sysfs_blkdev_set_parent(parent->sysfs, cxt->sysfs);
+                       sysfs_blkdev_set_parent(parent->sysfs, dev->sysfs);
        } else {
-               cxt->sysfs = ul_new_sysfs_path(devno, parent ? parent->sysfs : NULL, lsblk->sysroot);
-               if (!cxt->sysfs) {
-                       DBG(CXT, ul_debugobj(cxt, "%s: failed to initialize sysfs handler", cxt->name));
+               dev->sysfs = ul_new_sysfs_path(devno, parent ? parent->sysfs : NULL, lsblk->sysroot);
+               if (!dev->sysfs) {
+                       DBG(DEV, ul_debugobj(dev, "%s: failed to initialize sysfs handler", dev->name));
                        return -1;
                }
        }
 
-       cxt->maj = major(devno);
-       cxt->min = minor(devno);
-       cxt->size = 0;
+       dev->maj = major(devno);
+       dev->min = minor(devno);
+       dev->size = 0;
 
-       if (ul_path_read_u64(cxt->sysfs, &cxt->size, "size") == 0)      /* in sectors */
-               cxt->size <<= 9;                                        /* in bytes */
+       if (ul_path_read_u64(dev->sysfs, &dev->size, "size") == 0)      /* in sectors */
+               dev->size <<= 9;                                        /* in bytes */
 
-       if (ul_path_read_s32(cxt->sysfs, &cxt->discard,
+       if (ul_path_read_s32(dev->sysfs, &dev->discard,
                           "queue/discard_granularity") != 0)
-               cxt->discard = 0;
+               dev->discard = 0;
 
        /* Ignore devices of zero size */
-       if (!lsblk->all_devices && cxt->size == 0) {
-               DBG(CXT, ul_debugobj(cxt, "zero size device -- ignore"));
+       if (!lsblk->all_devices && dev->size == 0) {
+               DBG(DEV, ul_debugobj(dev, "zero size device -- ignore"));
                return -1;
        }
-       if (is_dm(cxt->name)) {
-               ul_path_read_string(cxt->sysfs, &cxt->dm_name, "dm/name");
-               if (!cxt->dm_name) {
-                       DBG(CXT, ul_debugobj(cxt, "%s: failed to get dm name", cxt->name));
+       if (is_dm(dev->name)) {
+               ul_path_read_string(dev->sysfs, &dev->dm_name, "dm/name");
+               if (!dev->dm_name) {
+                       DBG(DEV, ul_debugobj(dev, "%s: failed to get dm name", dev->name));
                        return -1;
                }
        }
 
-       cxt->npartitions = sysfs_blkdev_count_partitions(cxt->sysfs, cxt->name);
-       cxt->nholders = ul_path_count_dirents(cxt->sysfs, "holders");
-       cxt->nslaves = ul_path_count_dirents(cxt->sysfs, "slaves");
+       dev->npartitions = sysfs_blkdev_count_partitions(dev->sysfs, dev->name);
+       dev->nholders = ul_path_count_dirents(dev->sysfs, "holders");
+       dev->nslaves = ul_path_count_dirents(dev->sysfs, "slaves");
 
-       DBG(CXT, ul_debugobj(cxt, "%s: npartitions=%d, nholders=%d, nslaves=%d",
-                       cxt->name, cxt->npartitions, cxt->nholders, cxt->nslaves));
+       DBG(DEV, ul_debugobj(dev, "%s: npartitions=%d, nholders=%d, nslaves=%d",
+                       dev->name, dev->npartitions, dev->nholders, dev->nslaves));
 
        /* ignore non-SCSI devices */
-       if (lsblk->scsi && sysfs_blkdev_scsi_get_hctl(cxt->sysfs, NULL, NULL, NULL, NULL)) {
-               DBG(CXT, ul_debugobj(cxt, "non-scsi device -- ignore"));
+       if (lsblk->scsi && sysfs_blkdev_scsi_get_hctl(dev->sysfs, NULL, NULL, NULL, NULL)) {
+               DBG(DEV, ul_debugobj(dev, "non-scsi device -- ignore"));
                return -1;
        }
 
-       DBG(CXT, ul_debugobj(cxt, "%s: context successfully initialized", cxt->name));
+       DBG(DEV, ul_debugobj(dev, "%s: context successfully initialized", dev->name));
        return 0;
 }
 
-static int process_blkdev(struct blkdev_cxt *cxt, struct blkdev_cxt *parent,
+
+static int process_blkdev(struct lsblk_device *dev, struct lsblk_device *parent,
                          int do_partitions, const char *part_name);
 
 /*
  * List device partitions if any.
  */
-static int list_partitions(struct blkdev_cxt *wholedisk_cxt, struct blkdev_cxt *parent_cxt,
+static int list_partitions(struct lsblk_device *wholedisk_dev, struct lsblk_device *parent_dev,
                           const char *part_name)
 {
        DIR *dir;
        struct dirent *d;
-       struct blkdev_cxt part_cxt = { NULL };
+       struct lsblk_device part_dev = { .parent = NULL };
        int r = -1;
 
-       assert(wholedisk_cxt);
+       assert(wholedisk_dev);
 
        /*
         * Do not process further if there are no partitions for
         * this device or the device itself is a partition.
         */
-       if (!wholedisk_cxt->npartitions || wholedisk_cxt->partition)
+       if (!wholedisk_dev->npartitions || wholedisk_dev->partition)
                return -1;
 
-       DBG(CXT, ul_debugobj(wholedisk_cxt, "probe whole-disk for partitions"));
+       DBG(DEV, ul_debugobj(wholedisk_dev, "probe whole-disk for partitions"));
 
-       dir = ul_path_opendir(wholedisk_cxt->sysfs, NULL);
+       dir = ul_path_opendir(wholedisk_dev->sysfs, NULL);
        if (!dir)
                err(EXIT_FAILURE, _("failed to open device directory in sysfs"));
 
@@ -1345,55 +1116,50 @@ static int list_partitions(struct blkdev_cxt *wholedisk_cxt, struct blkdev_cxt *
                if (part_name && strcmp(part_name, d->d_name))
                        continue;
 
-               if (!(sysfs_blkdev_is_partition_dirent(dir, d, wholedisk_cxt->name)))
+               if (!(sysfs_blkdev_is_partition_dirent(dir, d, wholedisk_dev->name)))
                        continue;
 
-               DBG(CXT, ul_debugobj(wholedisk_cxt, "  checking %s", d->d_name));
+               DBG(DEV, ul_debugobj(wholedisk_dev, "  checking %s", d->d_name));
 
                if (lsblk->inverse) {
                        /*
-                        * <parent_cxt>
-                        * `-<part_cxt>
-                        *   `-<wholedisk_cxt>
+                        * <parent_dev>
+                        * `-<part_dev>
+                        *   `-<wholedisk_dev>
                         *    `-...
                         */
-                       if (set_cxt(&part_cxt, parent_cxt, wholedisk_cxt, d->d_name))
+                       if (set_device(&part_dev, parent_dev, wholedisk_dev, d->d_name))
                                goto next;
 
-                       if (!parent_cxt && part_cxt.nholders)
+                       if (!parent_dev && part_dev.nholders)
                                goto next;
 
-                       wholedisk_cxt->parent = &part_cxt;
-                       fill_table_line(&part_cxt, parent_cxt ? parent_cxt->scols_line : NULL);
+                       wholedisk_dev->parent = &part_dev;
                        if (!lsblk->nodeps)
-                               process_blkdev(wholedisk_cxt, &part_cxt, 0, NULL);
+                               process_blkdev(wholedisk_dev, &part_dev, 0, NULL);
                } else {
                        /*
-                        * <parent_cxt>
-                        * `-<wholedisk_cxt>
-                        *   `-<part_cxt>
+                        * <parent_dev>
+                        * `-<wholedisk_dev>
+                        *   `-<part_dev>
                         *    `-...
                         */
-                       int ps = set_cxt(&part_cxt, wholedisk_cxt, wholedisk_cxt, d->d_name);
+                       int ps = set_device(&part_dev, wholedisk_dev, wholedisk_dev, d->d_name);
 
-                       /* Print whole disk only once */
-                       if (r)
-                               fill_table_line(wholedisk_cxt, parent_cxt ? parent_cxt->scols_line : NULL);
                        if (ps == 0 && !lsblk->nodeps)
-                               process_blkdev(&part_cxt, wholedisk_cxt, 0, NULL);
+                               process_blkdev(&part_dev, wholedisk_dev, 0, NULL);
                }
        next:
-               reset_blkdev_cxt(&part_cxt);
                r = 0;
        }
 
-       DBG(CXT, ul_debugobj(wholedisk_cxt, "probe whole-disk for partitions -- done"));
+       DBG(DEV, ul_debugobj(wholedisk_dev, "probe whole-disk for partitions -- done"));
        closedir(dir);
        return r;
 }
 
 static int get_wholedisk_from_partition_dirent(DIR *dir,
-                               struct dirent *d, struct blkdev_cxt *cxt)
+                               struct dirent *d, struct lsblk_device *dev)
 {
        char path[PATH_MAX];
        char *p;
@@ -1415,78 +1181,77 @@ static int get_wholedisk_from_partition_dirent(DIR *dir,
                return 0;
        p++;
 
-       return set_cxt(cxt, NULL, NULL, p);
+       return set_device(dev, NULL, NULL, p);
 }
 
 /*
  * List device dependencies: partitions, holders (inverse = 0) or slaves (inverse = 1).
  */
-static int list_deps(struct blkdev_cxt *cxt)
+static int list_deps(struct lsblk_device *dev)
 {
        DIR *dir;
        struct dirent *d;
-       struct blkdev_cxt dep = { NULL };
+       struct lsblk_device dep = { .parent = NULL };
        const char *depname;
 
-       assert(cxt);
+       assert(dev);
 
        if (lsblk->nodeps)
                return 0;
 
-       DBG(CXT, ul_debugobj(cxt, "%s: list dependencies", cxt->name));
+       DBG(DEV, ul_debugobj(dev, "%s: list dependencies", dev->name));
 
-       if (!(lsblk->inverse ? cxt->nslaves : cxt->nholders))
+       if (!(lsblk->inverse ? dev->nslaves : dev->nholders))
                return 0;
 
        depname = lsblk->inverse ? "slaves" : "holders";
-       dir = ul_path_opendir(cxt->sysfs, depname);
+       dir = ul_path_opendir(dev->sysfs, depname);
        if (!dir)
                return 0;
 
-       DBG(CXT, ul_debugobj(cxt, "%s: checking for '%s' dependence", cxt->name, depname));
+       DBG(DEV, ul_debugobj(dev, "%s: checking for '%s' dependence", dev->name, depname));
 
        while ((d = xreaddir(dir))) {
                /* Is the dependency a partition? */
                if (sysfs_blkdev_is_partition_dirent(dir, d, NULL)) {
                        if (!get_wholedisk_from_partition_dirent(dir, d, &dep)) {
-                               DBG(CXT, ul_debugobj(cxt, "%s: %s: dependence is partition",
-                                                               cxt->name, d->d_name));
-                               process_blkdev(&dep, cxt, 1, d->d_name);
+                               DBG(DEV, ul_debugobj(dev, "%s: %s: dependence is partition",
+                                                               dev->name, d->d_name));
+                               process_blkdev(&dep, dev, 1, d->d_name);
                        }
                }
                /* The dependency is a whole device. */
-               else if (!set_cxt(&dep, cxt, NULL, d->d_name)) {
-                       DBG(CXT, ul_debugobj(cxt, "%s: %s: dependence is whole-disk",
-                                                               cxt->name, d->d_name));
+               else if (!set_device(&dep, dev, NULL, d->d_name)) {
+                       DBG(DEV, ul_debugobj(dev, "%s: %s: dependence is whole-disk",
+                                                               dev->name, d->d_name));
                        /* For inverse tree we don't want to show partitions
                         * if the dependence is on whole-disk */
-                       process_blkdev(&dep, cxt, lsblk->inverse ? 0 : 1, NULL);
+                       process_blkdev(&dep, dev, lsblk->inverse ? 0 : 1, NULL);
                }
-               reset_blkdev_cxt(&dep);
        }
        closedir(dir);
 
-       DBG(CXT, ul_debugobj(cxt, "%s: checking for '%s' -- done", cxt->name, depname));
+       DBG(DEV, ul_debugobj(dev, "%s: checking for '%s' -- done", dev->name, depname));
        return 0;
 }
 
-static int process_blkdev(struct blkdev_cxt *cxt, struct blkdev_cxt *parent,
+static int process_blkdev(struct lsblk_device *dev, struct lsblk_device *parent,
                          int do_partitions, const char *part_name)
 {
-       if (do_partitions && cxt->npartitions)
-               list_partitions(cxt, parent, part_name);                /* partitions + whole-disk */
-       else
-               fill_table_line(cxt, parent ? parent->scols_line : NULL); /* whole-disk only */
+#ifdef SUCK
+       if (do_partitions && dev->npartitions)
+               list_partitions(dev, parent, part_name);                /* partitions + whole-disk */
 
-       return list_deps(cxt);
+       return list_deps(dev);
+#endif
+       return 0;
 }
 
 /* Iterate devices in sysfs */
-static int iterate_block_devices(void)
+static int iterate_block_devices(struct lsblk_devtree *tr)
 {
        DIR *dir;
        struct dirent *d;
-       struct blkdev_cxt cxt = { NULL };
        struct path_cxt *pc = ul_new_path(_PATH_SYS_BLOCK);
 
        if (!pc)
@@ -1494,7 +1259,7 @@ static int iterate_block_devices(void)
 
        ul_path_set_prefix(pc, lsblk->sysroot);
 
-       /* TODO: reuse @pc in set_cxt(), etc. */
+       /* TODO: reuse @pc in set_device(), etc. */
        dir = ul_path_opendir(pc, NULL);
        if (!dir)
                goto done;
@@ -1502,22 +1267,35 @@ static int iterate_block_devices(void)
        DBG(DEV, ul_debug("iterate on " _PATH_SYS_BLOCK));
 
        while ((d = xreaddir(dir))) {
+               struct lsblk_device *dev;
 
                DBG(DEV, ul_debug(" %s dentry", d->d_name));
 
-               if (set_cxt(&cxt, NULL, NULL, d->d_name))
-                       goto next;
-
-               if (is_maj_excluded(cxt.maj) || !is_maj_included(cxt.maj))
-                       goto next;
+               dev = lsblk_devtree_get_device(tr, d->d_name);
+               if (!dev) {
+                       dev = lsblk_new_device(tr);
+                       if (!dev)
+                               err(EXIT_FAILURE, _("failed to allocate device"));
+                       if (set_device(dev, NULL, NULL, d->d_name) != 0) {
+                               lsblk_unref_device(dev);
+                               continue;
+                       }
+                       lsblk_devtree_add_device(tr, dev);
+                       lsblk_unref_device(dev);                /* keep it referenced by devtree only */
+               } else
+                       DBG(DEV, ul_debug(" %s: already processed", d->d_name));
 
-               /* Skip devices in the middle of dependency tree. */
-               if ((lsblk->inverse ? cxt.nholders : cxt.nslaves) > 0)
-                       goto next;
+               /* remove unwanted devices */
+               if (is_maj_excluded(dev->maj) || !is_maj_included(dev->maj)) {
+                       DBG(DEV, ul_debug(" %s: ignore (by filter)", d->d_name));
+                       lsblk_devtree_remove_device(tr, dev);
+                       continue;
+               }
 
-               process_blkdev(&cxt, NULL, 1, NULL);
-       next:
-               reset_blkdev_cxt(&cxt);
+               /* process dependencies for top level devices */
+               if ((lsblk->inverse ? dev->nholders : dev->nslaves) <= 0
+                   && process_blkdev(dev, NULL, 1, NULL) == 0)
+                       lsblk_devtree_add_root(tr, dev);
        }
 
        closedir(dir);
@@ -1529,7 +1307,7 @@ done:
 
 static int process_one_device(char *devname)
 {
-       struct blkdev_cxt parent = { NULL }, cxt = { NULL };
+       struct lsblk_device parent = { .parent = NULL }, dev = { .parent = NULL };
        struct stat st;
        char buf[PATH_MAX + 1], *name = NULL, *diskname = NULL;
        dev_t disk = 0;
@@ -1542,13 +1320,14 @@ static int process_one_device(char *devname)
 
        /* TODO: sysfs_devno_to_devname() internally initializes path_cxt, it
         * would be better to use ul_new_sysfs_path() + sysfs_blkdev_get_name()
-        * and reuse path_cxt for set_cxt()
+        * and reuse path_cxt for set_device()
         */
        name = sysfs_devno_to_devname(st.st_rdev, buf, sizeof(buf));
        if (!name) {
                warn(_("%s: failed to get sysfs name"), devname);
                goto leave;
        }
+       name = xstrdup(name);
 
        if (!strncmp(name, "dm-", 3)) {
                /* dm mapping is never a real partition! */
@@ -1566,32 +1345,27 @@ static int process_one_device(char *devname)
                /*
                 * Device is not a partition.
                 */
-               if (set_cxt(&cxt, NULL, NULL, name))
+               if (set_device(&dev, NULL, NULL, name))
                        goto leave;
-               process_blkdev(&cxt, NULL, !lsblk->inverse, NULL);
+               process_blkdev(&dev, NULL, !lsblk->inverse, NULL);
        } else {
                /*
                 * Partition, read sysfs name of the device.
                 */
-               if (set_cxt(&parent, NULL, NULL, diskname))
+               if (set_device(&parent, NULL, NULL, diskname))
                        goto leave;
-               if (set_cxt(&cxt, &parent, &parent, name))
+               if (set_device(&dev, &parent, &parent, name))
                        goto leave;
 
                if (lsblk->inverse)
-                       process_blkdev(&parent, &cxt, 1, cxt.name);
+                       process_blkdev(&parent, &dev, 1, dev.name);
                else
-                       process_blkdev(&cxt, &parent, 1, NULL);
+                       process_blkdev(&dev, &parent, 1, NULL);
        }
 
        rc = 0;
 leave:
        free(name);
-       reset_blkdev_cxt(&cxt);
-
-       if (real_part)
-               reset_blkdev_cxt(&parent);
-
        return rc;
 }
 
@@ -1702,6 +1476,7 @@ static void __attribute__((__noreturn__)) usage(void)
        fputs(_(" -S, --scsi           output info about SCSI devices\n"), out);
        fputs(_(" -t, --topology       output info about topology\n"), out);
        fputs(_(" -x, --sort <column>  sort output by <column>\n"), out);
+       fputs(_("     --sysroot <dir>  use specified directory as system root\n"), out);
        fputs(USAGE_SEPARATOR, out);
        printf(USAGE_HELP_OPTIONS(22));
 
@@ -1725,6 +1500,7 @@ static void check_sysdevblock(void)
 int main(int argc, char *argv[])
 {
        struct lsblk _ls = { .sort_id = -1, .flags = LSBLK_TREE };
+       struct lsblk_devtree *tr = NULL;
        int c, status = EXIT_FAILURE;
        char *outarg = NULL;
        size_t i;
@@ -1859,6 +1635,8 @@ int main(int argc, char *argv[])
                        add_uniq_column(COL_FSTYPE);
                        add_uniq_column(COL_LABEL);
                        add_uniq_column(COL_UUID);
+                       add_uniq_column(COL_FSAVAIL);
+                       add_uniq_column(COL_FSUSEPERC);
                        add_uniq_column(COL_TARGET);
                        break;
                case 'm':
@@ -1950,7 +1728,7 @@ int main(int argc, char *argv[])
                lsblk->sort_hidden = 1;
        }
 
-       mnt_init_debug(0);
+       lsblk_mnt_init();
        scols_init_debug(0);
        ul_path_init_debug();
 
@@ -2010,8 +1788,12 @@ int main(int argc, char *argv[])
                }
        }
 
+       tr = lsblk_new_devtree();
+       if (!tr)
+               err(EXIT_FAILURE, _("failed to allocate device tree"));
+
        if (optind == argc)
-               status = iterate_block_devices() == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
+               status = iterate_block_devices(tr) == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
        else {
                int cnt = 0, cnt_err = 0;
 
@@ -2026,6 +1808,8 @@ int main(int argc, char *argv[])
                                          EXIT_SUCCESS;         /* all success */
        }
 
+       devtree_to_scols(tr, lsblk->table);
+
        if (lsblk->sort_col)
                scols_sort_table(lsblk->table, lsblk->sort_col);
        if (lsblk->force_tree_order)
@@ -2039,11 +1823,9 @@ leave:
 
        scols_unref_table(lsblk->table);
 
-       mnt_unref_table(mtab);
-       mnt_unref_table(swaps);
-       mnt_unref_cache(mntcache);
-#ifdef HAVE_LIBUDEV
-       udev_unref(udev);
-#endif
+       lsblk_mnt_deinit();
+       lsblk_properties_deinit();
+       lsblk_unref_devtree(tr);
+
        return status;
 }