]> git.karo-electronics.de Git - mv-sheeva.git/blobdiff - fs/btrfs/ioctl.c
Btrfs: fix the inode ref searches done by btrfs_search_path_in_tree
[mv-sheeva.git] / fs / btrfs / ioctl.c
index 9aaba6e472d3f61f98085ae549304f375775efc1..2845c6ceecd247f78adcd2b049ea220bbe764ff4 100644 (file)
@@ -776,7 +776,7 @@ static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
                        mod = 1;
                        sizestr++;
                }
-               new_size = btrfs_parse_size(sizestr);
+               new_size = memparse(sizestr, NULL);
                if (new_size == 0) {
                        ret = -EINVAL;
                        goto out_unlock;
@@ -914,17 +914,23 @@ out:
 static noinline int key_in_sk(struct btrfs_key *key,
                              struct btrfs_ioctl_search_key *sk)
 {
-       if (key->objectid < sk->min_objectid)
-               return 0;
-       if (key->offset < sk->min_offset)
-               return 0;
-       if (key->type < sk->min_type)
-               return 0;
-       if (key->objectid > sk->max_objectid)
-               return 0;
-       if (key->type > sk->max_type)
+       struct btrfs_key test;
+       int ret;
+
+       test.objectid = sk->min_objectid;
+       test.type = sk->min_type;
+       test.offset = sk->min_offset;
+
+       ret = btrfs_comp_cpu_keys(key, &test);
+       if (ret < 0)
                return 0;
-       if (key->offset > sk->max_offset)
+
+       test.objectid = sk->max_objectid;
+       test.type = sk->max_type;
+       test.offset = sk->max_offset;
+
+       ret = btrfs_comp_cpu_keys(key, &test);
+       if (ret > 0)
                return 0;
        return 1;
 }
@@ -991,20 +997,25 @@ static noinline int copy_to_sk(struct btrfs_root *root,
                        read_extent_buffer(leaf, p,
                                           item_off, item_len);
                        *sk_offset += item_len;
-                       found++;
                }
+               found++;
 
                if (*num_found >= sk->nr_items)
                        break;
        }
 advance_key:
-       if (key->offset < (u64)-1)
+       ret = 0;
+       if (key->offset < (u64)-1 && key->offset < sk->max_offset)
                key->offset++;
-       else if (key->type < (u64)-1)
+       else if (key->type < (u8)-1 && key->type < sk->max_type) {
+               key->offset = 0;
                key->type++;
-       else if (key->objectid < (u64)-1)
+       } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
+               key->offset = 0;
+               key->type = 0;
                key->objectid++;
-       ret = 0;
+       } else
+               ret = 1;
 overflow:
        *num_found += found;
        return ret;
@@ -1136,12 +1147,13 @@ static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
        root = btrfs_read_fs_root_no_name(info, &key);
        if (IS_ERR(root)) {
                printk(KERN_ERR "could not find root %llu\n", tree_id);
-               return -ENOENT;
+               ret = -ENOENT;
+               goto out;
        }
 
        key.objectid = dirid;
        key.type = BTRFS_INODE_REF_KEY;
-       key.offset = 0;
+       key.offset = (u64)-1;
 
        while(1) {
                ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
@@ -1150,6 +1162,8 @@ static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
 
                l = path->nodes[0];
                slot = path->slots[0];
+               if (ret > 0 && slot > 0)
+                       slot--;
                btrfs_item_key_to_cpu(l, &key, slot);
 
                if (ret > 0 && (key.objectid != dirid ||
@@ -1173,7 +1187,7 @@ static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
 
                btrfs_release_path(root, path);
                key.objectid = key.offset;
-               key.offset = 0;
+               key.offset = (u64)-1;
                dirid = key.objectid;
 
        }
@@ -1204,6 +1218,9 @@ static noinline int btrfs_ioctl_ino_lookup(struct file *file,
        }
        inode = fdentry(file)->d_inode;
 
+       if (args->treeid == 0)
+               args->treeid = BTRFS_I(inode)->root->root_key.objectid;
+
        ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
                                        args->treeid, args->objectid,
                                        args->name);
@@ -1843,6 +1860,84 @@ 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_ioctl_space_info *dest_orig;
+       struct btrfs_ioctl_space_info *user_dest;
+       struct btrfs_space_info *info;
+       int alloc_size;
+       int ret = 0;
+       int slot_count = 0;
+
+       if (copy_from_user(&space_args,
+                          (struct btrfs_ioctl_space_args __user *)arg,
+                          sizeof(space_args)))
+               return -EFAULT;
+
+       /* first we count slots */
+       rcu_read_lock();
+       list_for_each_entry_rcu(info, &root->fs_info->space_info, list)
+               slot_count++;
+       rcu_read_unlock();
+
+       /* space_slots == 0 means they are asking for a count */
+       if (space_args.space_slots == 0) {
+               space_args.total_spaces = slot_count;
+               goto out;
+       }
+       alloc_size = sizeof(*dest) * slot_count;
+       /* we generally have at most 6 or so space infos, one for each raid
+        * level.  So, a whole page should be more than enough for everyone
+        */
+       if (alloc_size > PAGE_CACHE_SIZE)
+               return -ENOMEM;
+
+       space_args.total_spaces = 0;
+       dest = kmalloc(alloc_size, GFP_NOFS);
+       if (!dest)
+               return -ENOMEM;
+       dest_orig = dest;
+
+       /* now we have a buffer to copy into */
+       rcu_read_lock();
+       list_for_each_entry_rcu(info, &root->fs_info->space_info, list) {
+               /* make sure we don't copy more than we allocated
+                * in our buffer
+                */
+               if (slot_count == 0)
+                       break;
+               slot_count--;
+
+               /* make sure userland has enough room in their buffer */
+               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;
+               memcpy(dest, &space, sizeof(space));
+               dest++;
+               space_args.total_spaces++;
+       }
+       rcu_read_unlock();
+
+       user_dest = (struct btrfs_ioctl_space_info *)
+               (arg + sizeof(struct btrfs_ioctl_space_args));
+
+       if (copy_to_user(user_dest, dest_orig, alloc_size))
+               ret = -EFAULT;
+
+       kfree(dest_orig);
+out:
+       if (ret == 0 && 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 +2010,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;