From 5d86c616a849a67492bc3cdc917a737aa63ee132 Mon Sep 17 00:00:00 2001 From: Mike Fleetwood Date: Fri, 7 Jan 2022 10:08:20 +0000 Subject: [PATCH] Report busy status of bcache (#183) Make (format as) bcache backing device (-B) and cache device (-C) and implicitly attach the backing device to the cache to enable caching, all in one. # bcache make -B /dev/sdb1 -C /dev/sdc1 # bcache show Name Type State Bname AttachToDev /dev/sdb1 1 (data) clean(running) bcache0 /dev/sdc1 /dev/sdc1 3 (cache) active N/A N/A After experimenting with 'bcache unregister', 'bcache register' and stracing 'bcache show' the bcache kernel module creates the sysfs directory /sys/block/DEV[/PTN]/bcache and it's contents only when the bcache device is registered with the kernel (bcache component is active). Use this to identify whether any bcache device (component) should be displayed as active or not in GParted. # ls -ld /sys/block/sd?/sd?1/bcache drwxr-xr-x. 6 root root 0 Jan 7 10:08 /sys/block/sdb/sdb1/bcache drwxr-xr-x. 2 root root 0 Jan 7 10:08 /sys/block/sdc/sdc1/bcache Closes #183 - Basic support for bcache --- include/BCache_Info.h | 44 +++++++++++++++++++++++++++++++ include/Makefile.am | 1 + po/POTFILES.in | 1 + src/BCache_Info.cc | 51 ++++++++++++++++++++++++++++++++++++ src/Dialog_Partition_Info.cc | 6 +++-- src/GParted_Core.cc | 5 +++- src/Makefile.am | 1 + tests/Makefile.am | 1 + 8 files changed, 107 insertions(+), 3 deletions(-) create mode 100644 include/BCache_Info.h create mode 100644 src/BCache_Info.cc diff --git a/include/BCache_Info.h b/include/BCache_Info.h new file mode 100644 index 00000000..5265a547 --- /dev/null +++ b/include/BCache_Info.h @@ -0,0 +1,44 @@ +/* Copyright (C) 2022 Mike Fleetwood + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + + +/* BCache_Info + * + * Simple module to query very basic information about bcache devices + * (components). No caching is performed by this module. + */ + +#ifndef GPARTED_BCACHE_INFO_H +#define GPARTED_BCACHE_INFO_H + + +#include + + +namespace GParted +{ + + +class BCache_Info +{ +public: + static bool is_active(const Glib::ustring& device_path, const Glib::ustring& partition_path); +}; + + +} //GParted + +#endif /* GPARTED_BCACHE_INFO_H */ diff --git a/include/Makefile.am b/include/Makefile.am index 423e2f7f..4a2d0dfc 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -1,6 +1,7 @@ gparted_includedir = $(pkgincludedir) EXTRA_DIST = \ + BCache_Info.h \ BlockSpecial.h \ CopyBlocks.h \ DMRaid.h \ diff --git a/po/POTFILES.in b/po/POTFILES.in index d2392c25..c6fe9a1b 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -4,6 +4,7 @@ gparted.appdata.xml.in gparted.desktop.in.in org.gnome.gparted.policy.in.in include/Utils.h +src/BCache_Info.cc src/BlockSpecial.cc src/CopyBlocks.cc src/DialogPasswordEntry.cc diff --git a/src/BCache_Info.cc b/src/BCache_Info.cc new file mode 100644 index 00000000..ff9bd105 --- /dev/null +++ b/src/BCache_Info.cc @@ -0,0 +1,51 @@ +/* Copyright (C) 2022 Mike Fleetwood + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + + +#include "BCache_Info.h" + +#include +#include + + +namespace GParted +{ + + +// Return true if this device or partition contains an active bcache component, false +// otherwise. Equivalent to does the directory /sys/block/DEV[/PTN]/bcache exist? +bool BCache_Info::is_active(const Glib::ustring& device_path, const Glib::ustring& partition_path) +{ + Glib::ustring bcache_path; + Glib::ustring dev_name = device_path.substr(5); // Remove leading "/dev/". + + if (device_path == partition_path) + { + // Whole drive + bcache_path = "/sys/block/" + dev_name + "/bcache"; + } + else + { + // Partition on drive + Glib::ustring ptn_name = partition_path.substr(5); // Remove leading "/dev/". + bcache_path = "/sys/block/" + dev_name + "/" + ptn_name + "/bcache"; + } + + return file_test(bcache_path, Glib::FILE_TEST_IS_DIR); +} + + +} //GParted diff --git a/src/Dialog_Partition_Info.cc b/src/Dialog_Partition_Info.cc index 0cbb5390..b98ef102 100644 --- a/src/Dialog_Partition_Info.cc +++ b/src/Dialog_Partition_Info.cc @@ -340,7 +340,8 @@ void Dialog_Partition_Info::Display_Info() else if (filesystem_ptn.fstype == FS_LINUX_SWAP || filesystem_ptn.fstype == FS_LINUX_SWRAID || filesystem_ptn.fstype == FS_ATARAID || - filesystem_ptn.fstype == FS_LVM2_PV ) + filesystem_ptn.fstype == FS_LVM2_PV || + filesystem_ptn.fstype == FS_BCACHE ) { /* TO TRANSLATORS: Active * means that this linux swap, linux software raid partition, or @@ -374,7 +375,8 @@ void Dialog_Partition_Info::Display_Info() } else if (filesystem_ptn.fstype == FS_LINUX_SWAP || filesystem_ptn.fstype == FS_LINUX_SWRAID || - filesystem_ptn.fstype == FS_ATARAID ) + filesystem_ptn.fstype == FS_ATARAID || + filesystem_ptn.fstype == FS_BCACHE ) { /* TO TRANSLATORS: Not active * means that this linux swap or linux software raid partition diff --git a/src/GParted_Core.cc b/src/GParted_Core.cc index c881f062..7dae661d 100644 --- a/src/GParted_Core.cc +++ b/src/GParted_Core.cc @@ -15,9 +15,11 @@ * along with this program; if not, see . */ + #include "GParted_Core.h" -#include "CopyBlocks.h" +#include "BCache_Info.h" #include "BlockSpecial.h" +#include "CopyBlocks.h" #include "Device.h" #include "DMRaid.h" #include "FileSystem.h" @@ -1595,6 +1597,7 @@ bool GParted_Core::is_busy(const Glib::ustring& device_path, FSType fstype, cons busy |= (fstype == FS_LINUX_SWRAID && SWRaid_Info::is_member_active(partition_path)); busy |= (fstype == FS_ATARAID && (SWRaid_Info::is_member_active(partition_path) || dmraid.is_member_active(partition_path) )); + busy |= (fstype == FS_BCACHE && BCache_Info::is_active(device_path, partition_path)); } return busy ; diff --git a/src/Makefile.am b/src/Makefile.am index 1234a4a4..cb359e89 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -11,6 +11,7 @@ AM_CXXFLAGS = -Wall libexec_PROGRAMS = gpartedbin gpartedbin_SOURCES = \ + BCache_Info.cc \ BlockSpecial.cc \ CopyBlocks.cc \ DMRaid.cc \ diff --git a/tests/Makefile.am b/tests/Makefile.am index bbd0ba8e..931c1aef 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -24,6 +24,7 @@ test_dummy_SOURCES = test_dummy.cc test_SupportedFileSystems_SOURCES = test_SupportedFileSystems.cc test_SupportedFileSystems_LDADD = \ + $(top_builddir)/src/BCache_Info.$(OBJEXT) \ $(top_builddir)/src/BlockSpecial.$(OBJEXT) \ $(top_builddir)/src/CopyBlocks.$(OBJEXT) \ $(top_builddir)/src/DMRaid.$(OBJEXT) \