Btrfs: add a "df" ioctl for btrfs
authorJosef Bacik <josef@redhat.com>
Wed, 13 Jan 2010 18:19:06 +0000 (18:19 +0000)
committerChris Mason <chris.mason@oracle.com>
Mon, 15 Mar 2010 15:00:14 +0000 (11:00 -0400)
df is a very loaded question in btrfs.  This gives us a way to get the per-space
usage information so we can tell exactly what is in use where.  This will help
us figure out ENOSPC problems, and help users better understand where their disk
space is going.

Signed-off-by: Josef Bacik <josef@redhat.com>
Signed-off-by: Chris Mason <chris.mason@oracle.com>
fs/btrfs/ioctl.c
fs/btrfs/ioctl.h

index 9aaba6e472d3f61f98085ae549304f375775efc1..9213d39d36ccdd624ea56bfaf066d12aae4d2439 100644 (file)
@@ -1843,6 +1843,49 @@ static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
        return 0;
 }
 
+long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
+{
+       struct btrfs_ioctl_space_args space_args;
+       struct btrfs_ioctl_space_info space;
+       struct btrfs_ioctl_space_info *dest;
+       struct btrfs_space_info *info;
+       int ret = 0;
+
+       if (copy_from_user(&space_args,
+                          (struct btrfs_ioctl_space_args __user *)arg,
+                          sizeof(space_args)))
+               return -EFAULT;
+
+       space_args.total_spaces = 0;
+       dest = (struct btrfs_ioctl_space_info *)
+               (arg + sizeof(struct btrfs_ioctl_space_args));
+
+       rcu_read_lock();
+       list_for_each_entry_rcu(info, &root->fs_info->space_info, list) {
+               if (!space_args.space_slots) {
+                       space_args.total_spaces++;
+                       continue;
+               }
+               if (space_args.total_spaces >= space_args.space_slots)
+                       break;
+               space.flags = info->flags;
+               space.total_bytes = info->total_bytes;
+               space.used_bytes = info->bytes_used;
+               if (copy_to_user(dest, &space, sizeof(space))) {
+                       ret = -EFAULT;
+                       break;
+               }
+               dest++;
+               space_args.total_spaces++;
+       }
+       rcu_read_unlock();
+
+       if (copy_to_user(arg, &space_args, sizeof(space_args)))
+               ret = -EFAULT;
+
+       return ret;
+}
+
 /*
  * there are many ways the trans_start and trans_end ioctls can lead
  * to deadlocks.  They should only be used by applications that
@@ -1915,6 +1958,8 @@ long btrfs_ioctl(struct file *file, unsigned int
                return btrfs_ioctl_tree_search(file, argp);
        case BTRFS_IOC_INO_LOOKUP:
                return btrfs_ioctl_ino_lookup(file, argp);
+       case BTRFS_IOC_SPACE_INFO:
+               return btrfs_ioctl_space_info(root, argp);
        case BTRFS_IOC_SYNC:
                btrfs_sync_fs(file->f_dentry->d_sb, 1);
                return 0;
index 2d64a65842f32eec7de2490b5016ee4bd2fca76e..81eac0a59ea31088084b56d27460171258cb9457 100644 (file)
@@ -1,4 +1,3 @@
-
 /*
  * Copyright (C) 2007 Oracle.  All rights reserved.
  *
@@ -127,6 +126,17 @@ struct btrfs_ioctl_defrag_range_args {
        __u32 unused[5];
 };
 
+struct btrfs_ioctl_space_info {
+       u64 flags;
+       u64 total_bytes;
+       u64 used_bytes;
+};
+
+struct btrfs_ioctl_space_args {
+       u64 space_slots;
+       u64 total_spaces;
+       struct btrfs_ioctl_space_info spaces[0];
+};
 
 #define BTRFS_IOC_SNAP_CREATE _IOW(BTRFS_IOCTL_MAGIC, 1, \
                                   struct btrfs_ioctl_vol_args)
@@ -166,4 +176,6 @@ struct btrfs_ioctl_defrag_range_args {
 #define BTRFS_IOC_INO_LOOKUP _IOWR(BTRFS_IOCTL_MAGIC, 18, \
                                   struct btrfs_ioctl_ino_lookup_args)
 #define BTRFS_IOC_DEFAULT_SUBVOL _IOW(BTRFS_IOCTL_MAGIC, 19, u64)
+#define BTRFS_IOC_SPACE_INFO _IOWR(BTRFS_IOCTL_MAGIC, 20, \
+                                   struct btrfs_ioctl_space_args)
 #endif