diff mbox series

[2/2] btrfs: sysfs add devinfo/fsid to retrieve fsid from the device

Message ID 7df68f272490c55349b44a33dd7ac19ccf87560f.1634598572.git.anand.jain@oracle.com (mailing list archive)
State New, archived
Headers show
Series provide fsid in sysfs devinfo | expand

Commit Message

Anand Jain Oct. 19, 2021, 12:22 a.m. UTC
In the case of the seed device, the fsid can be different from the mounted
sprout fsid.  The userland has to read the device superblock to know the
fsid but, that idea fails if the device is missing. So add a sysfs
interface devinfo/<devid>/fsid to show the fsid of the device.

For example:
 $ cd /sys/fs/btrfs/b10b02a5-f9de-4276-b9e8-2bfd09a578a8

 $ cat devinfo/1/fsid
 c44d771f-639d-4df3-99ec-5bc7ad2af93b
 $ cat  devinfo/3/fsid
 b10b02a5-f9de-4276-b9e8-2bfd09a578a8

Signed-off-by: Anand Jain <anand.jain@oracle.com>
---
 fs/btrfs/sysfs.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

Comments

David Sterba Oct. 20, 2021, 6:59 p.m. UTC | #1
On Tue, Oct 19, 2021 at 08:22:10AM +0800, Anand Jain wrote:
> In the case of the seed device, the fsid can be different from the mounted
> sprout fsid.  The userland has to read the device superblock to know the
> fsid but, that idea fails if the device is missing. So add a sysfs
> interface devinfo/<devid>/fsid to show the fsid of the device.
> 
> For example:
>  $ cd /sys/fs/btrfs/b10b02a5-f9de-4276-b9e8-2bfd09a578a8
> 
>  $ cat devinfo/1/fsid
>  c44d771f-639d-4df3-99ec-5bc7ad2af93b
>  $ cat  devinfo/3/fsid
>  b10b02a5-f9de-4276-b9e8-2bfd09a578a8

How do you create such setup? I can't reproduce it.

The simplest seeding:

  mkfs.btrfs /dev/sdx
  btrfstune -S 1 /dev/sdx
  mount /dev/sdx mnt
  ... the device has the same FSID as is the sysfs directory name

With a new device and removed the seeding one:

  btrfs device add /dev/sdy mnt
  mount -o remount,rw mnt
  btrfs device delete /dev/sdx mnt
  ... both devices have the same fsid as well
Anand Jain Oct. 21, 2021, 4:03 a.m. UTC | #2
On 21/10/2021 02:59, David Sterba wrote:
> On Tue, Oct 19, 2021 at 08:22:10AM +0800, Anand Jain wrote:
>> In the case of the seed device, the fsid can be different from the mounted
>> sprout fsid.  The userland has to read the device superblock to know the
>> fsid but, that idea fails if the device is missing. So add a sysfs
>> interface devinfo/<devid>/fsid to show the fsid of the device.
>>
>> For example:
>>   $ cd /sys/fs/btrfs/b10b02a5-f9de-4276-b9e8-2bfd09a578a8
>>
>>   $ cat devinfo/1/fsid
>>   c44d771f-639d-4df3-99ec-5bc7ad2af93b
>>   $ cat  devinfo/3/fsid
>>   b10b02a5-f9de-4276-b9e8-2bfd09a578a8
> 
> How do you create such setup? I can't reproduce it.
> 
> The simplest seeding:
> 
>    mkfs.btrfs /dev/sdx
>    btrfstune -S 1 /dev/sdx
>    mount /dev/sdx mnt
>    ... the device has the same FSID as is the sysfs directory name
> 
> With a new device and removed the seeding one:
> 
>    btrfs device add /dev/sdy mnt

At this step, we generate a new fsid for the writeable FS. Let's call
it sprout-fsid. (If you check the sys-fs fsid, you will have two fsid
here).

Also, at this step,
the
     fs_info->fs_devices->fsid
changes from seed-fsid to the sprout-fsid.
So, we should make the <mnt> also a writeable without the need to call
'remount,rw' explicitly IMO. What do you think?

>    mount -o remount,rw mnt
>    btrfs device delete /dev/sdx mnt
>    ... both devices have the same fsid as well 


Thanks, Anand
Anand Jain Oct. 21, 2021, 8:55 a.m. UTC | #3
On 21/10/2021 12:03, Anand Jain wrote:
> On 21/10/2021 02:59, David Sterba wrote:
>> On Tue, Oct 19, 2021 at 08:22:10AM +0800, Anand Jain wrote:
>>> In the case of the seed device, the fsid can be different from the 
>>> mounted
>>> sprout fsid.  The userland has to read the device superblock to know the
>>> fsid but, that idea fails if the device is missing. So add a sysfs
>>> interface devinfo/<devid>/fsid to show the fsid of the device.
>>>
>>> For example:
>>>   $ cd /sys/fs/btrfs/b10b02a5-f9de-4276-b9e8-2bfd09a578a8
>>>
>>>   $ cat devinfo/1/fsid
>>>   c44d771f-639d-4df3-99ec-5bc7ad2af93b
>>>   $ cat  devinfo/3/fsid
>>>   b10b02a5-f9de-4276-b9e8-2bfd09a578a8
>>
>> How do you create such setup? I can't reproduce it.
>>
>> The simplest seeding:
>>
>>    mkfs.btrfs /dev/sdx
>>    btrfstune -S 1 /dev/sdx
>>    mount /dev/sdx mnt
>>    ... the device has the same FSID as is the sysfs directory name
>>
>> With a new device and removed the seeding one:
>>
>>    btrfs device add /dev/sdy mnt
> 
> At this step, we generate a new fsid for the writeable FS. Let's call
> it sprout-fsid. (If you check the sys-fs fsid, you will have two fsid
> here).
> 
> Also, at this step,
> the
>      fs_info->fs_devices->fsid
> changes from seed-fsid to the sprout-fsid.
> So, we should make the <mnt> also a writeable without the need to call
> 'remount,rw' explicitly IMO. What do you think?
> 

More importantly, the fs_info->super_copy is of sprout device.
So there isn't any reason that we should maintain the new
sprout fs as readonly after the device add.


>>    mount -o remount,rw mnt
>>    btrfs device delete /dev/sdx mnt
>>    ... both devices have the same fsid as well 
> 
> 
> Thanks, Anand
> 
> 
> 
>
diff mbox series

Patch

diff --git a/fs/btrfs/sysfs.c b/fs/btrfs/sysfs.c
index c0ea7269ff57..c10fc39cb677 100644
--- a/fs/btrfs/sysfs.c
+++ b/fs/btrfs/sysfs.c
@@ -1538,6 +1538,16 @@  static ssize_t btrfs_devinfo_writeable_show(struct kobject *kobj,
 }
 BTRFS_ATTR(devid, writeable, btrfs_devinfo_writeable_show);
 
+static ssize_t btrfs_devinfo_fsid_show(struct kobject *kobj,
+				       struct kobj_attribute *a, char *buf)
+{
+	struct btrfs_device *device = container_of(kobj, struct btrfs_device,
+						   devid_kobj);
+
+	return sysfs_emit(buf, "%pU\n", device->fs_devices->fsid);
+}
+BTRFS_ATTR(devid, fsid, btrfs_devinfo_fsid_show);
+
 static ssize_t btrfs_devinfo_error_stats_show(struct kobject *kobj,
 		struct kobj_attribute *a, char *buf)
 {
@@ -1578,6 +1588,7 @@  static struct attribute *devid_attrs[] = {
 	BTRFS_ATTR_PTR(devid, replace_target),
 	BTRFS_ATTR_PTR(devid, scrub_speed_max),
 	BTRFS_ATTR_PTR(devid, writeable),
+	BTRFS_ATTR_PTR(devid, fsid),
 	NULL
 };
 ATTRIBUTE_GROUPS(devid);