summaryrefslogtreecommitdiffstats
path: root/misc-utils
diff options
context:
space:
mode:
authorKarel Zak2018-10-17 13:48:43 +0200
committerKarel Zak2018-12-07 12:32:58 +0100
commit81a8936c56b25192f23f82798da7f6a9ddf76feb (patch)
tree8d6efc7bb8b13ad7a4a958ec6d01fe0bcfbed492 /misc-utils
parentlsblk: don't keep sysfs dirs open (diff)
downloadkernel-qcow2-util-linux-81a8936c56b25192f23f82798da7f6a9ddf76feb.tar.gz
kernel-qcow2-util-linux-81a8936c56b25192f23f82798da7f6a9ddf76feb.tar.xz
kernel-qcow2-util-linux-81a8936c56b25192f23f82798da7f6a9ddf76feb.zip
lsblk: reuse 'removable' flag from parent
It's used in the default output, let's make it a little bit more effective. Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'misc-utils')
-rw-r--r--misc-utils/lsblk-devtree.c2
-rw-r--r--misc-utils/lsblk.c34
-rw-r--r--misc-utils/lsblk.h1
3 files changed, 31 insertions, 6 deletions
diff --git a/misc-utils/lsblk-devtree.c b/misc-utils/lsblk-devtree.c
index 5bef44be0..efc165f54 100644
--- a/misc-utils/lsblk-devtree.c
+++ b/misc-utils/lsblk-devtree.c
@@ -21,7 +21,7 @@ struct lsblk_device *lsblk_new_device(struct lsblk_devtree *tree)
return NULL;
dev->refcount = 1;
-
+ dev->removable = -1;
dev->tree = tree;
INIT_LIST_HEAD(&dev->deps);
diff --git a/misc-utils/lsblk.c b/misc-utils/lsblk.c
index 6a6781966..aacc0e560 100644
--- a/misc-utils/lsblk.c
+++ b/misc-utils/lsblk.c
@@ -674,6 +674,34 @@ static struct stat *device_get_stat(struct lsblk_device *dev)
return &dev->st;
}
+static int is_removable_device(struct lsblk_device *dev, struct lsblk_device *parent)
+{
+ struct path_cxt *pc;
+
+ if (dev->removable != -1)
+ goto done;
+ if (ul_path_scanf(dev->sysfs, "removable", "%d", &dev->removable) == 1)
+ goto done;
+
+ if (parent) {
+ pc = sysfs_blkdev_get_parent(dev->sysfs);
+ if (!pc)
+ goto done;
+
+ if (pc == parent->sysfs)
+ /* dev is partition and parent is whole-disk */
+ dev->removable = is_removable_device(parent, NULL);
+ else
+ /* parent is something else, use sysfs parent */
+ ul_path_scanf(pc, "removable", "%d", &dev->removable);
+ }
+
+done:
+ if (dev->removable == -1)
+ dev->removable = 0;
+ return dev->removable;
+}
+
/*
* Generates data (string) for column specified by column ID for specified device
*/
@@ -807,11 +835,7 @@ static void set_scols_data(
str = xstrdup(is_readonly_device(dev) ? "1" : "0");
break;
case COL_RM:
- 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");
+ str = xstrdup(is_removable_device(dev, parent) ? "1" : "0");
break;
case COL_HOTPLUG:
str = sysfs_blkdev_is_hotpluggable(dev->sysfs) ? xstrdup("1") : xstrdup("0");
diff --git a/misc-utils/lsblk.h b/misc-utils/lsblk.h
index f8e10c7ce..7cb4e3eee 100644
--- a/misc-utils/lsblk.h
+++ b/misc-utils/lsblk.h
@@ -109,6 +109,7 @@ struct lsblk_device {
int discard; /* supports discard */
uint64_t size; /* device size */
+ int removable; /* unknown:-1, yes:1, not:0 */
unsigned int is_mounted : 1,
is_swap : 1,