Exploring BTRFS filesystem internals (userspace + kernel)

Starting with a design documentation:
A good intro writeup:
Lots of technical details:
Elaborating on the COW mechanism in BTRFS (similar to ZFS):
Performance comparison:
Now some hands-on (a good system admin guide:   http://docs.oracle.com/cd/E37670_01/E37355/html/ol_use_case7_btrfs.html):
First create a file to be used as a virtual disk:
truncate -s 10G test-image.1
Next ensure the userspace tools for BTRFS are installed (I am on Ubuntu 12.04 LTS 64-bit):
sudo apt-get install btrfs-tools
Next make BTRFS filesystem on the virtual disk:
mkfs.btrfs test-image.1
Is the btrfs kernel module loaded?
lsmod|grep btr
No.   Check if it is compiled into the kernel?
grep -i btrfs /boot/config-3.8.0-29-generic 
Oh, yes, it is compiled into kernel as a loadable module.   So we shall load it:
sudo modprobe btrfs
Now use that to mount a BTRFS filesystem:
sudo mkdir  /btrfs
sudo mount test-image.1 /btrfs
sudo chmod 777 /btrfs/
Next we can start doing filesystem testing on /btrfs, using tools from below:
And to understand the filesystem internals, we can use ftrace, the sequence of operation are captured essentially as follows:
sudo mkdir /debug
sudo mount -t debugfs nodev /debug
echo 0 >/debug/tracing/tracing_on
echo ‘btrfs_*’ ‘journal_*’ > /debug/tracing/set_ftrace_filter
cat /debug/tracing/enabled_functions 
We can see many BTRFS functions which is partially listed here:
btrfs_create_qgroup [btrfs] (1)
btrfs_remove_qgroup [btrfs] (1)
btrfs_limit_qgroup [btrfs] (1)
btrfs_qgroup_record_ref [btrfs] (1)
btrfs_qgroup_account_ref [btrfs] (1)
btrfs_run_qgroups [btrfs] (1)
btrfs_qgroup_inherit [btrfs] (1)
btrfs_qgroup_reserve [btrfs] (1)
btrfs_qgroup_free [btrfs] (1)
btrfs_ioctl_send [btrfs] (1)
btrfs_init_dev_replace [btrfs] (1)
btrfs_after_dev_replace_commit [btrfs] (1)
btrfs_dev_replace_is_ongoing [btrfs] (1)
btrfs_dev_replace_lock [btrfs] (1)
btrfs_dev_replace_unlock [btrfs] (1)
btrfs_resume_dev_replace_async [btrfs] (1)
btrfs_dev_replace_finishing [btrfs] (1)
btrfs_dev_replace_suspend_for_unmount [btrfs] (1)
btrfs_dev_replace_cancel [btrfs] (1)
btrfs_dev_replace_status [btrfs] (1)
btrfs_dev_replace_kthread [btrfs] (1)
btrfs_dev_replace_start [btrfs] (1)
btrfs_run_dev_replace [btrfs] (1)
btrfs_set_acl [btrfs] (1)
btrfs_xattr_acl_set [btrfs] (1)
btrfs_get_acl [btrfs] (1)
btrfs_xattr_acl_get [btrfs] (1)
btrfs_init_acl [btrfs] (1)
btrfs_acl_chmod [btrfs] (1)
btrfs_interface_exit [btrfs] (1)
btrfs_find_highest_objectid [btrfs] (1)
echo function >/debug/tracing/current_tracer
echo 1 >/debug/tracing/tracing_on
ls -alR /btrfs
cp /bin/l* /btrfs
sleep 3
echo 0 >/debug/tracing/tracing_on
cat /debug/tracing/trace
And looking into the output:
  1.            <…>-7497  [003] ….  8751.390285: btrfs_real_readdir <-vfs_readdir
  2.            <…>-7497  [003] ….  8751.390286: btrfs_alloc_path <-btrfs_real_readdir
  3.            <…>-7497  [003] ….  8751.390288: btrfs_get_delayed_items <-btrfs_real_readdir
  4.            <…>-7497  [003] ….  8751.390288: btrfs_get_delayed_node <-btrfs_get_delayed_items
  5.            <…>-7497  [003] ….  8751.390289: btrfs_search_slot <-btrfs_real_readdir
And the full trace is here (for pid=7497):
And some of the userspace command features of BTRFS:

btrfs subvolume snapshot <source> [<dest>/]<name>
Create a writable snapshot of the subvolume <source> with
the name <name> in the <dest> directory.
btrfs subvolume delete <subvolume>
Delete the subvolume <subvolume>.
btrfs subvolume create [<dest>/]<name>
Create a subvolume in <dest> (or the current directory if
not passed).
btrfs subvolume list <path>
List the snapshot/subvolume of a filesystem.
btrfs subvolume find-new <path> <last_gen>
List the recently modified files in a filesystem.
btrfs filesystem defragment [-vcf] [-s start] [-l len] [-t size] <file>|<dir> [<file>|<dir>…]
Defragment a file or a directory.
btrfs subvolume set-default <id> <path>
Set the subvolume of the filesystem <path> which will be mounted
as default.
btrfs filesystem sync <path>
Force a sync on the filesystem <path>.
btrfs filesystem resize [+/-]<newsize>[gkm]|max <filesystem>
Resize the file system. If ‘max’ is passed, the filesystem
will occupe all available space on the device.
btrfs filesystem show [<uuid>|<label>]
Show the info of a btrfs filesystem. If no <uuid> or <label>
is passed, info of all the btrfs filesystem are shown.
btrfs filesystem df <path>
Show space usage information for a mount point
btrfs filesystem balance <path>
Balance the chunks across the device.
btrfs device scan [<device> [<device>..]
Scan all device for or the passed device for a btrfs
btrfs device add <dev> [<dev>..] <path>
Add a device to a filesystem.
btrfs device delete <dev> [<dev>..] <path>
Remove a device from a filesystem.

btrfs help|–help|-h
Show the help.http://docs.oracle.com/cd/E37670_01/E37355/html/ol_use_case7_btrfs.html

Btrfs Btrfs v0.19

And some other commands:
btrfsck btrfs-debug-tree

2 responses to this post.

  1. Just found this:

    In this month Linux Journal issue:

    An In-Depth Look at ZFS and BTRFS
    Ease the Pain of Provisioning X.509 Certificates
    Synchronize Your Life with ownCloud
    Check Your Exchange Inbox from the Command Line
    pi-web-agent: a Desktop Environment for the Raspberry Pi


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: