Skip to contentSkip to conversation

BTRFS sysfs support

PATCH In review Goffredo Baroncelli 2 participants 6 messages Aug 23, 2012

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover BTRFS sysfs support

Hi all,
The following series restores the BTRFS sysfs support. My idea is to exporting some information via sysfs to get rid of some ioctl(s) which are particular solution of a more general problem: exporting information from kernel space to user space.

These patches export basically the fields of the following structure:
- btrfs_device
- btrfs_fs_info
- btrfs_fs_devices

You can read more about which information are exported reading the patch (the last one) which contains an update of the btrfs.txt file.

The goal of this first attempt is to collect comments relating to which information we should export and which not or the sysfs structure.

For now these information are read-only. In the future we could consider the idea to make some action: for example to change the label of a filesystem.

Comments are welcome.
BR
G.Baroncelli

---

Goffredo Baroncelli (4):
Add support for sysfs to btrfs.
Add hook for sysfs to btrfs.
Add a new Kconfig section to enable or disable the sysfs. Btrfs sysfs support documentation.

 fs/btrfs/Kconfig   |    9 +
 fs/btrfs/super.c   |    4 
 fs/btrfs/sysfs.c   |  933 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 fs/btrfs/sysfs.h   |   31 ++
 fs/btrfs/volumes.c |   15 +
 5 files changed, 986 insertions(+), 6 deletions(-)
 create mode 100644 fs/btrfs/sysfs.h
#1 lore raw

PATCH 1/4 Add support for sysfs to btrfs.

Export via sysfs some information about the btrfs devices and filesystem.

---
 fs/btrfs/super.c |    4 
 fs/btrfs/sysfs.c |  933 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 fs/btrfs/sysfs.h |   31 ++
 3 files changed, 963 insertions(+), 5 deletions(-)
 create mode 100644 fs/btrfs/sysfs.h
fs/btrfs/super.c +4 -0
index e239915..8927674 100644
@@ -55,6 +55,7 @@
 #include "export.h"
 #include "compression.h"
 #include "rcu-string.h"
+#include "sysfs.h"
 
 #define CREATE_TRACE_POINTS
 #include <trace/events/btrfs.h>
@@ -1093,6 +1094,8 @@ static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
 	if (IS_ERR(root))
 		deactivate_locked_super(s);
 
+	btrfs_sysfs_add_filesystem(fs_info);
+
 	return root;
 
 error_close_devices:
@@ -1422,6 +1425,7 @@ static void btrfs_kill_super(struct super_block *sb)
 {
 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
 	kill_anon_super(sb);
+	btrfs_sysfs_remove_filesystem(fs_info);
 	free_fs_info(fs_info);
 }
 
fs/btrfs/sysfs.c +928 -5

951 lines not on this page · raw message

fs/btrfs/sysfs.h new file +31 -0
new file mode 100644
index 0000000..92b6a9f
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2012 G.Baroncelli.  All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License v2 as published by the Free Software Foundation.
+ *
+ * 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, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 021110-1307, USA.
+ */
+ 
+#include "ctree.h"
+ 
+/* register new device */
+void btrfs_sysfs_add_device(struct btrfs_device *dev);
+
+/* unregister a device */
+void btrfs_sysfs_remove_device(struct btrfs_device *dev);
+
+/* register new filesystem */
+void btrfs_sysfs_add_filesystem(struct btrfs_fs_info *fs_info);
+
+/* unregister a filesystem */
+void btrfs_sysfs_remove_filesystem(struct btrfs_fs_info *fs_info);
#2 lore raw

Re: [PATCH v0 1/4] Add support for sysfs to btrfs.

Quoted 8 lines · Goffredo Baroncelli

Export via sysfs some information about the btrfs devices and filesystem.

---
 fs/btrfs/super.c |    4 
 fs/btrfs/sysfs.c |  933 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 fs/btrfs/sysfs.h |   31 ++
 3 files changed, 963 insertions(+), 5 deletions(-)
 create mode 100644 fs/btrfs/sysfs.h

Hi Goffredo,

The Signed-off-by line is missing, it is mandatory.

[...]

fs/btrfs/sysfs.c line 66

Quoted 5 lines
fs/btrfs/sysfs.c +5 -0
+struct btrfs_sysfs_fsid {
+		struct kobject			*kobj;
+		struct list_head		list;
+		u8						fsid[BTRFS_FSID_SIZE];
+};

Tabsize needs to be 8.

Quoted 15 lines
fs/btrfs/sysfs.c +15 -0
+static struct list_head btrfs_sysfs_device_list;
+static struct list_head btrfs_sysfs_fs_devices_list;
+static struct list_head btrfs_sysfs_filesystem_list;
+static struct list_head btrfs_sysfs_fsid_list;
+
+
+void uuid_unparse( u8 *uuid, char *out ){
+	static char *i2x = "0123456789abcdef";
+	static int 	lengths[] = {4,2,2,2,6,0};
+	int 		i;
+	
+	for(i=0; ; i++){
+		int j;
+		for(j=0; j < lengths[i] ; j++, uuid++){
+			*out++ = i2x[*uuid >> 4];

fs/btrfs/sysfs.c line 92

Quoted 10 lines
fs/btrfs/sysfs.c +10 -0
+			*out++ = i2x[*uuid & 0x0f];
+		}
+		if( !lengths[i+1] ){
+			*out = 0;
+			break;
+		}else{
+			*out++ = '-';
+		}
+	}
+}

All the missing spaces and all the spaces that are too much... One line contains a trailing TAB character before the end of the line. A newline is missing before the opening curly bracket of the function body. An empty line between the 'int j;' and the code is common, like the one after the 'int i;'.

[more lines deleted...]

Please refer to:
Documentation/SubmittingPatches
Documentation/SubmitChecklist
Documentation/CodingStyle

And apply:
scripts/checkpatch.pl

I did not look at the patch itself after noticing the style issues.

#3 lore raw

PATCH 2/4 Add hook for sysfs to btrfs.

In reply to Goffredo Baroncelli

Add the hook in the btrfs filesystem code to supporting sysfs.

---
 fs/btrfs/volumes.c |   15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
fs/btrfs/volumes.c +14 -1
index ecaad40..e1075ab 100644
@@ -36,6 +36,7 @@
 #include "async-thread.h"
 #include "check-integrity.h"
 #include "rcu-string.h"
+#include "sysfs.h"
 
 static int init_first_rw_device(struct btrfs_trans_handle *trans,
 				struct btrfs_root *root,
@@ -405,6 +406,8 @@ static noinline int device_list_add(const char *path,
 		}
 	}
 
+	btrfs_sysfs_add_device(device);
+
 	if (found_transid > fs_devices->latest_trans) {
 		fs_devices->latest_devid = devid;
 		fs_devices->latest_trans = found_transid;
@@ -458,6 +461,9 @@ static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
 		INIT_LIST_HEAD(&device->dev_alloc_list);
 
 		list_add(&device->dev_list, &fs_devices->devices);
+		
+		btrfs_sysfs_add_device(device);
+		
 		device->fs_devices = fs_devices;
 		fs_devices->num_devices++;
 	}
@@ -576,7 +582,7 @@ static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
 		new_device->in_fs_metadata = 0;
 		new_device->can_discard = 0;
 		list_replace_rcu(&device->dev_list, &new_device->dev_list);
-
+		//btrfs_sysfs_remove_device(device);
 		call_rcu(&device->rcu, free_device);
 	}
 	mutex_unlock(&fs_devices->device_list_mutex);
@@ -1430,6 +1436,9 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path)
 
 	cur_devices = device->fs_devices;
 	mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
+	
+	btrfs_sysfs_remove_device(device);
+	
 	list_del_rcu(&device->dev_list);
 
 	device->fs_devices->num_devices--;
@@ -1744,6 +1753,7 @@ int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
 	 */
 	mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
 	list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
+	btrfs_sysfs_add_device(device);
 	list_add(&device->dev_alloc_list,
 		 &root->fs_info->fs_devices->alloc_list);
 	root->fs_info->fs_devices->num_devices++;
@@ -4306,6 +4316,9 @@ static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
 	spin_lock_init(&device->io_lock);
 	INIT_LIST_HEAD(&device->dev_alloc_list);
 	memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
+	
+	btrfs_sysfs_add_device(device);
+	
 	return device;
 }
 
#4 lore raw

PATCH 3/4 Add a new Kconfig section to enable or disable the sysfs.

In reply to Goffredo Baroncelli

Add support for sysfs to BTRFS. This patch add the Kconfig part to enable or disable the exporting of teh BTRFS information via sysfs.

---
 fs/btrfs/Kconfig |    9 +++++++++
 1 file changed, 9 insertions(+)
fs/btrfs/Kconfig +9 -0
index d33f01c..187e057 100644
@@ -50,3 +50,12 @@ config BTRFS_FS_CHECK_INTEGRITY
 	  In most cases, unless you are a btrfs developer who needs
 	  to verify the integrity of (super)-block write requests
 	  during the run of a regression test, say N
+
+config BTRFS_FS_SYSFS
+	bool "Btrfs sysfs support"
+	depends on BTRFS_FS
+	help
+	  This options allow to export some btrfs filesystems/devices
+	  properties via the sysfs interface.
+
+
#5 lore raw

PATCH 4/4 Btrfs sysfs support documentation.

In reply to Goffredo Baroncelli

Update of the Documentation/filesystes/btrfs.txt file.
---

 0 files changed
Documentation/filesystems/btrfs.txt +75 -0
index 7671352..35840ab 100644
@@ -89,3 +89,78 @@ btrfsck: do a limited check of the FS extent trees.
 btrfs-debug-tree: print all of the FS metadata in text form.  Example:
 
 	btrfs-debug-tree /dev/sda2 >& big_output_file
+
+
+
+	SYSFS SUPPORT
+	=============
+
+If the option CONFIG_BTRFS_FS_SYSFS is enabled, BTRFS exports some 
+information via the sysfs filesystem.
+Todate these information are read-only.
+
+The information exported are related to both the devices and the filesystems.
+When a devices is registred (for example via the btrfs filesystem scan 
+command), BTRFS creates the following entries under /sys/fs/btrfs:
+
+/sys/fs/btrfs:
+
+	- devices			-> information per device basis
+	- filesystem			-> information per filesystem basis
+
+/sys/fs/btrfs/devices/<device-UUID>:
+
+	- bytes_used
+	- devid
+	- disk_total_bytes
+	- fsid				-> fielsystem UUID
+	- generation
+	- major				[*]
+	- minor				[*]
+	- missing
+	- name				-> device name (eg: /dev/sda)
+	- total_bytes
+	- write_errors			-> error info
+	- corruption_errors		-> error info
+	- read_errors			-> error info
+	- generation_errors		-> error info
+	- flush_errors			-> error info
+	- writeable
+
+[*] These information are available only after the mounting of the
+filesystem.
+
+/sys/fs/btrfs/filesystem/<filesystem-UUID>/fs_info:
+
+	- balance_cancel_req		-> balance status information
+	- balance_pause_req		-> balance status information
+	- balance_running		-> balance status information
+	- fs_state
+	- generation
+	- label				-> label of the filesystem
+	- last_trans_committed
+	- scrub_cancel_req		-> scrub status information
+	- scrub_pause_req		-> scrub status information
+	- scrubs_paused			-> scrub status information
+	- scrubs_running		-> scrub status information
+
+/sys/fs/btrfs/filesystem/<filesystem-UUID>/fs_devices:
+
+	- latest_devid
+	- latest_trans
+	- missing_devices
+	- num_can_discard
+	- open_devices
+	- opened
+	- rw_devices
+	- seeding
+	- total_rw_bytes
+
+The filesystems information are split in two section (fs_info and fs_devices)
+because the fs_devices entries are available after the scan of the device(s),
+but the fs_info entries are available only after the mount of the 
+filesystem.
+
+The link between the filesystem and the device(s) is the fsid == 
+filesystem-UUID.
+