exporting info via sysfs
RFC 1/1 exporting info via sysfs
Hi all,
the enclosed patch is a second attempt to export some information from btrfs filesystem through the sysfs interface.
At the moment there are two kinds of information available. The ones related to the devices and the ones related to the filesystems.
Before explaining the sysfs filesystem layout, I have to refresh to everybody
that btrfs manages the devices in "two step".
The first step is when the command "btrfs device scan" is started. The command find all the suitable devices, and passes the devices name (/dev/sda1 for example) to the btrfs subsystem. Then btrfs opens the device, reads the super block, and *closes* the device.
The second step is the mount of a filesystem. From the device passed by the mount command, the uuid is extracted. Then btrfs searches
from the list of the devices scanned the devices linked to the filesystem.
It must be noted that between the two steps, the devices exist in btrfs but are not opened. At this thime the only thing know is the path of the device.
The info are under /sys/fs/btrfs, where two directories are created:
- devices
- filesystems
Under devices/ there are the devices used by btrfs. Under filesystem there are the filesystem mounted.
Every device is identified by its uuid (look at the UUID_SUB tag in the output of blkid). The info exported are:
- major, minor -> the major/minor of the exported device (only if the device
is used, otherwise these are set to "none")
- name -> path of the block device (valid only if the device is not used)
- state -> status of the device (online or offline)
- devid -> devid of the device (an index of the device)
- fsid -> uuid of the filesystem
- writeable, total_byte, |-> fields of the struct btrfs_device (I can't
- disk_total_byte, byte_used |-> explain a more formal description )
Every filesystem is identified by its uuid (look at the UUID tag in the output of blkid). The info exported are:
- label -> filesystem label
- total_devices -> total number of device
- open_devices -> number of opened devices (total_devices - missing devices) - num_devices -> ?? - rw_devices -> number of rw devices
- block_used, total_block -> space used / space available [raid is not considered]
Using sysfs to export info from btrfs, can resolve a lot of issues that the using of ioctl raises.
The code is onaly a proof of concpet, use at your risk (I use it under linux user mode). Comments are welcome.
Regards
Goffredo
Example:
$ (cd /sys/fs/btrfs/; for i in */*/*; do echo -e "$i:\t$(cat $i)"; done ) devices/494d030e-d392-4275-985a-643ae0986e5b/bytes_used: 199229440 devices/494d030e-d392-4275-985a-643ae0986e5b/devid: 1 devices/494d030e-d392-4275-985a-643ae0986e5b/disk_total_bytes: 555745280 devices/494d030e-d392-4275-985a-643ae0986e5b/fsid: df7db53b-4f62-4c91-a275-2928dcad06b7 devices/494d030e-d392-4275-985a-643ae0986e5b/major: 98 devices/494d030e-d392-4275-985a-643ae0986e5b/minor: 16 devices/494d030e-d392-4275-985a-643ae0986e5b/name: 98/16 devices/494d030e-d392-4275-985a-643ae0986e5b/status: online devices/494d030e-d392-4275-985a-643ae0986e5b/total_bytes: 555745280 devices/494d030e-d392-4275-985a-643ae0986e5b/writeable: 1 devices/666a6b42-f813-4104-a705-d795fc3d7dd1/bytes_used: 75497472 devices/666a6b42-f813-4104-a705-d795fc3d7dd1/devid: 4 devices/666a6b42-f813-4104-a705-d795fc3d7dd1/disk_total_bytes: 555745280 devices/666a6b42-f813-4104-a705-d795fc3d7dd1/fsid: df7db53b-4f62-4c91-a275-2928dcad06b7 devices/666a6b42-f813-4104-a705-d795fc3d7dd1/major: 98 devices/666a6b42-f813-4104-a705-d795fc3d7dd1/minor: 64 devices/666a6b42-f813-4104-a705-d795fc3d7dd1/name: 98/64 devices/666a6b42-f813-4104-a705-d795fc3d7dd1/status: online devices/666a6b42-f813-4104-a705-d795fc3d7dd1/total_bytes: 555745280 devices/666a6b42-f813-4104-a705-d795fc3d7dd1/writeable: 1 devices/cd4ca632-06f1-4935-8799-3f1bc8caf0e4/bytes_used: 178257920 devices/cd4ca632-06f1-4935-8799-3f1bc8caf0e4/devid: 2 devices/cd4ca632-06f1-4935-8799-3f1bc8caf0e4/disk_total_bytes: 555745280 devices/cd4ca632-06f1-4935-8799-3f1bc8caf0e4/fsid: df7db53b-4f62-4c91-a275-2928dcad06b7 devices/cd4ca632-06f1-4935-8799-3f1bc8caf0e4/major: 98 devices/cd4ca632-06f1-4935-8799-3f1bc8caf0e4/minor: 32 devices/cd4ca632-06f1-4935-8799-3f1bc8caf0e4/name: 98/32 devices/cd4ca632-06f1-4935-8799-3f1bc8caf0e4/status: online devices/cd4ca632-06f1-4935-8799-3f1bc8caf0e4/total_bytes: 555745280 devices/cd4ca632-06f1-4935-8799-3f1bc8caf0e4/writeable: 1 devices/d1e2fb24-851d-4a0e-bc82-0ae36866f70c/bytes_used: 75497472 devices/d1e2fb24-851d-4a0e-bc82-0ae36866f70c/devid: 3 devices/d1e2fb24-851d-4a0e-bc82-0ae36866f70c/disk_total_bytes: 555745280 devices/d1e2fb24-851d-4a0e-bc82-0ae36866f70c/fsid: df7db53b-4f62-4c91-a275-2928dcad06b7 devices/d1e2fb24-851d-4a0e-bc82-0ae36866f70c/major: 98 devices/d1e2fb24-851d-4a0e-bc82-0ae36866f70c/minor: 48 devices/d1e2fb24-851d-4a0e-bc82-0ae36866f70c/name: 98/48 devices/d1e2fb24-851d-4a0e-bc82-0ae36866f70c/status: online devices/d1e2fb24-851d-4a0e-bc82-0ae36866f70c/total_bytes: 555745280 devices/d1e2fb24-851d-4a0e-bc82-0ae36866f70c/writeable: 1 filesystems/df7db53b-4f62-4c91-a275-2928dcad06b7/blocks_used: 28672 filesystems/df7db53b-4f62-4c91-a275-2928dcad06b7/blocksize: 4096 filesystems/df7db53b-4f62-4c91-a275-2928dcad06b7/label: btrfs-example filesystems/df7db53b-4f62-4c91-a275-2928dcad06b7/num_devices: 4 filesystems/df7db53b-4f62-4c91-a275-2928dcad06b7/open_devices: 4 filesystems/df7db53b-4f62-4c91-a275-2928dcad06b7/rw_devices: 4 filesystems/df7db53b-4f62-4c91-a275-2928dcad06b7/total_blocks: 2222981120 filesystems/df7db53b-4f62-4c91-a275-2928dcad06b7/total_devices: 4
--------------------
fs/btrfs/super.c +2 -0
@@ -51,6 +51,7 @@#include "version.h" #include "export.h" #include "compression.h" +#include "sysfs.h" static const struct super_operations btrfs_super_ops;
@@ -59,6 +60,7 @@ static void btrfs_put_super(struct super_block *sb)struct btrfs_root *root =btrfs_sb (sb);int ret; +btrfs_sysfs_del_super (root->fs_info); ret =close_ctree (root); sb->s_fs_info =NULL ;
@@ -585,6 +587,7 @@ static struct dentry *btrfs_mount(struct file_system_type *fs_type, int
flags,
char *subvol_name = NULL;
u64 subvol_objectid = 0;
int error = 0;
+ struct btrfs_root *broot;
if (!(flags & MS_RDONLY))
mode |= FMODE_WRITE;
@@ -668,6 +671,10 @@ static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
root = new_root;
}
+
+ broot = s->s_fs_info;
+ btrfs_sysfs_add_super(broot->fs_info); /* FIXME: check error */
+
kfree(subvol_name);
return root;
@@ -677,6 +684,7 @@ error_close_devices:
btrfs_close_devices(fs_devices);
error_free_subvol_name:
kfree(subvol_name);
+/*error:*/
return ERR_PTR(error);
}
fs/btrfs/sysfs.c +252 -108
443 lines not on this page · raw message
fs/btrfs/volumes.c +5 -0
@@ -31,6 +31,7 @@#include "print-tree.h" #include "volumes.h" #include "async-thread.h" +#include "sysfs.h" struct map_lookup {u64 type;
@@ -82,6 +83,7 @@ static void free_fs_devices(struct btrfs_fs_devices *fs_devices) device =list_entry (fs_devices->devices.next, struct btrfs_device, dev_list);list_del (&device->dev_list); +btrfs_sysfs_del_device (device);kfree (device->name);kfree (device); }
@@ -412,6 +414,9 @@ static noinline int device_list_add(const char *path, device->fs_devices = fs_devices; fs_devices->num_devices++; + +btrfs_sysfs_add_device (device); + }else if (strcmp (device->name, path)) { name =kstrdup (path,GFP_NOFS );if (!name)
@@ -468,6 +473,12 @@ static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices
*orig)
list_add(&device->dev_list, &fs_devices->devices); device->fs_devices = fs_devices; fs_devices->num_devices++;
+
+ /* TODO: these code means that may exists two devices with the
+ * same UUID ?
+ */
+ btrfs_sysfs_add_device(device);
+
}
mutex_unlock(&orig->device_list_mutex);
return fs_devices;
@@ -488,6 +499,8 @@ again:
if (device->in_fs_metadata)
continue;
+
+ btrfs_sysfs_del_device(device);
if (device->bdev) {
close_bdev_exclusive(device->bdev, device->mode); device->bdev = NULL;
@@ -1224,7 +1237,7 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path)
goto error_brelse;
device->in_fs_metadata = 0;
-
+ btrfs_sysfs_del_device(device);
/*
* the device list mutex makes sure that we don't change
* the device list while someone else is writing out all
@@ -1561,6 +1574,7 @@ int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
ret = btrfs_relocate_sys_chunks(root);
BUG_ON(ret);
}
+ btrfs_sysfs_add_device(device); /* FIXME: check the error */
out:
mutex_unlock(&root->fs_info->volume_mutex);
return ret;
@@ -3084,6 +3098,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;
}
fs/btrfs/volumes.h +4 -0
@@ -82,6 +82,10 @@ struct btrfs_device {u8 uuid[BTRFS_UUID_SIZE ];struct btrfs_work work; + +/* sysfs stuff */ +struct kobject device_kobj; +struct completion kobj_unregister; };struct btrfs_fs_devices {