Show bcache device as mount point of registered backing device (#183)

A bcache device provides accelerated access to a backing device in a one
to one relationship.  Multiple bcache backing devices can be attached to
and accelerated by the same cache device.  Extending the setup from the
previous commit, create an additional backing device and attach it to
the same cache.
    # bcache make -B /dev/sdb2
    # bcache attach /dev/sdc1 /dev/sdb2
    # bcache show
    Name        Type        State            Bname     AttachToDev
    /dev/sdb2   1 (data)    clean(running)   bcache1   /dev/sdc1
    /dev/sdb1   1 (data)    clean(running)   bcache0   /dev/sdc1
    /dev/sdc1   3 (cache)   active           N/A       N/A

List a couple of bcache specific sysfs files which identify registered
(active) bcache devices (components).
    # ls -l /sys/block/sd?/sd??/bcache/{dev,set}
    lrwxrwxrwx. 1 root root 0 Jan  7 10:08 /sys/block/sdb/sdb1/bcache/dev -> ../../../../../../../../../../virtual/block/bcache0
    lrwxrwxrwx. 1 root root 0 Jan  7 11:53 /sys/block/sdb/sdb2/bcache/dev -> ../../../../../../../../../../virtual/block/bcache1
    lrwxrwxrwx. 1 root root 0 Jan  7 11:53 /sys/block/sdc/sdc1/bcache/set -> ../../../../../../../../../../../fs/bcache/9945e165-0604-4f29-94bd-b155d01080ad

As was done with previous software block devices [1][2][3][4] show the
bcache (access) device as the mount point of a backing device
(component).  Use the /sys/block/DEV[/PTN]/bcache/dev sysfs symlinks to
provide the bcache device names.  Bcache cache devices (components)
don't get mount points because they aren't accessible.

[1] commit 8083f11d84
    Display LVM2 VGNAME as the PV's mount point (#160787)
[2] commit f6c2f00df7
    Populate member mount point with SWRaid array device (#756829)
[3] commit 538c866d09
    Display array device as mount point of mdadm started ATARAID members
    (#75)
[4] commit 538c866d09
    Display array device as mount point of mdadm started ATARAID members
    (#75)

Closes #183 - Basic support for bcache
This commit is contained in:
Mike Fleetwood 2022-01-07 17:34:49 +00:00 committed by Curtis Gedak
parent fd8c7857da
commit 013c992428
3 changed files with 42 additions and 5 deletions

View File

@ -36,6 +36,11 @@ class BCache_Info
{ {
public: public:
static bool is_active(const Glib::ustring& device_path, const Glib::ustring& partition_path); static bool is_active(const Glib::ustring& device_path, const Glib::ustring& partition_path);
static Glib::ustring get_bcache_device(const Glib::ustring& device_path, const Glib::ustring& partition_path);
private:
static Glib::ustring get_sysfs_bcache_path(const Glib::ustring& device_path,
const Glib::ustring& partition_path);
}; };

View File

@ -17,6 +17,8 @@
#include "BCache_Info.h" #include "BCache_Info.h"
#include <string.h> // GNU version of basename()
#include <unistd.h>
#include <glibmm/ustring.h> #include <glibmm/ustring.h>
#include <glibmm/fileutils.h> #include <glibmm/fileutils.h>
@ -29,22 +31,45 @@ namespace GParted
// otherwise. Equivalent to does the directory /sys/block/DEV[/PTN]/bcache exist? // 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) bool BCache_Info::is_active(const Glib::ustring& device_path, const Glib::ustring& partition_path)
{ {
Glib::ustring bcache_path; return file_test(get_sysfs_bcache_path(device_path, partition_path), Glib::FILE_TEST_IS_DIR);
}
// Return the bcache device name for a registered bcache backing device (active
// component), or an empty string.
// E.g.: ("/dev/sdb", "/dev/sdb1") -> "/dev/bcache0"
Glib::ustring BCache_Info::get_bcache_device(const Glib::ustring& device_path, const Glib::ustring& partition_path)
{
Glib::ustring sysfs_path = get_sysfs_bcache_path(device_path, partition_path) + "/dev";
char buf[128]; // Large enough for link target
// "../../../../../../../../../../virtual/block/bcache0".
ssize_t len = readlink(sysfs_path.c_str(), buf, sizeof(buf)-1);
if (len < 0)
return Glib::ustring("");
buf[len] = '\0';
return "/dev/" + Glib::ustring(basename(buf));
}
// Private methods
Glib::ustring BCache_Info::get_sysfs_bcache_path(const Glib::ustring& device_path, const Glib::ustring& partition_path)
{
Glib::ustring dev_name = device_path.substr(5); // Remove leading "/dev/". Glib::ustring dev_name = device_path.substr(5); // Remove leading "/dev/".
if (device_path == partition_path) if (device_path == partition_path)
{ {
// Whole drive // Whole drive
bcache_path = "/sys/block/" + dev_name + "/bcache"; return "/sys/block/" + dev_name + "/bcache";
} }
else else
{ {
// Partition on drive // Partition on drive
Glib::ustring ptn_name = partition_path.substr(5); // Remove leading "/dev/". Glib::ustring ptn_name = partition_path.substr(5); // Remove leading "/dev/".
bcache_path = "/sys/block/" + dev_name + "/" + ptn_name + "/bcache"; return "/sys/block/" + dev_name + "/" + ptn_name + "/bcache";
} }
return file_test(bcache_path, Glib::FILE_TEST_IS_DIR);
} }

View File

@ -1491,6 +1491,13 @@ void GParted_Core::set_mountpoints( Partition & partition )
partition.add_mountpoint(array_path_2); partition.add_mountpoint(array_path_2);
} }
} }
else if (partition.fstype == FS_BCACHE)
{
const Glib::ustring bcache_path = BCache_Info::get_bcache_device(partition.device_path,
partition.get_path());
if (! bcache_path.empty())
partition.add_mountpoint(bcache_path);
}
else if (partition.fstype == FS_LUKS) else if (partition.fstype == FS_LUKS)
{ {
LUKS_Mapping mapping = LUKS_Info::get_cache_entry( partition.get_path() ); LUKS_Mapping mapping = LUKS_Info::get_cache_entry( partition.get_path() );