]> 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 3a89cd77f30716b5cca904abb4d857e2941e4ef1..2845c6ceecd247f78adcd2b049ea220bbe764ff4 100644 (file)
@@ -476,13 +476,18 @@ out_unlock:
 }
 
 static int should_defrag_range(struct inode *inode, u64 start, u64 len,
-                              u64 *last_len, u64 *skip, u64 *defrag_end)
+                              int thresh, u64 *last_len, u64 *skip,
+                              u64 *defrag_end)
 {
        struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
        struct extent_map *em = NULL;
        struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
        int ret = 1;
 
+
+       if (thresh == 0)
+               thresh = 256 * 1024;
+
        /*
         * make sure that once we start defragging and extent, we keep on
         * defragging it
@@ -517,8 +522,7 @@ static int should_defrag_range(struct inode *inode, u64 start, u64 len,
        /*
         * we hit a real extent, if it is big don't bother defragging it again
         */
-       if ((*last_len == 0 || *last_len >= 256 * 1024) &&
-           em->len >= 256 * 1024)
+       if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
                ret = 0;
 
        /*
@@ -542,7 +546,8 @@ static int should_defrag_range(struct inode *inode, u64 start, u64 len,
        return ret;
 }
 
-static int btrfs_defrag_file(struct file *file)
+static int btrfs_defrag_file(struct file *file,
+                            struct btrfs_ioctl_defrag_range_args *range)
 {
        struct inode *inode = fdentry(file)->d_inode;
        struct btrfs_root *root = BTRFS_I(inode)->root;
@@ -563,11 +568,19 @@ static int btrfs_defrag_file(struct file *file)
        if (inode->i_size == 0)
                return 0;
 
-       last_index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT;
-       i = 0;
+       if (range->start + range->len > range->start) {
+               last_index = min_t(u64, inode->i_size - 1,
+                        range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
+       } else {
+               last_index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT;
+       }
+
+       i = range->start >> PAGE_CACHE_SHIFT;
        while (i <= last_index) {
                if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
-                                       PAGE_CACHE_SIZE, &last_len, &skip,
+                                       PAGE_CACHE_SIZE,
+                                       range->extent_thresh,
+                                       &last_len, &skip,
                                        &defrag_end)) {
                        unsigned long next;
                        /*
@@ -585,6 +598,8 @@ static int btrfs_defrag_file(struct file *file)
                }
                total_read++;
                mutex_lock(&inode->i_mutex);
+               if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
+                       BTRFS_I(inode)->force_compress = 1;
 
                ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE);
                if (ret) {
@@ -658,7 +673,7 @@ again:
                                  page_end, EXTENT_DIRTY | EXTENT_DELALLOC |
                                  EXTENT_DO_ACCOUNTING, GFP_NOFS);
 
-               btrfs_set_extent_delalloc(inode, page_start, page_end);
+               btrfs_set_extent_delalloc(inode, page_start, page_end, NULL);
                ClearPageChecked(page);
                set_page_dirty(page);
                unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
@@ -673,6 +688,28 @@ loop_unlock:
                i++;
        }
 
+       if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
+               filemap_flush(inode->i_mapping);
+
+       if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
+               /* the filemap_flush will queue IO into the worker threads, but
+                * we have to make sure the IO is actually started and that
+                * ordered extents get created before we return
+                */
+               atomic_inc(&root->fs_info->async_submit_draining);
+               while (atomic_read(&root->fs_info->nr_async_submits) ||
+                     atomic_read(&root->fs_info->async_delalloc_pages)) {
+                       wait_event(root->fs_info->async_submit_wait,
+                          (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
+                           atomic_read(&root->fs_info->async_delalloc_pages) == 0));
+               }
+               atomic_dec(&root->fs_info->async_submit_draining);
+
+               mutex_lock(&inode->i_mutex);
+               BTRFS_I(inode)->force_compress = 0;
+               mutex_unlock(&inode->i_mutex);
+       }
+
        return 0;
 
 err_reservations:
@@ -739,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;
@@ -877,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;
 }
@@ -954,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;
@@ -1099,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);
@@ -1113,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 ||
@@ -1136,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;
 
        }
@@ -1167,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);
@@ -1284,10 +1338,11 @@ out:
        return err;
 }
 
-static int btrfs_ioctl_defrag(struct file *file)
+static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
 {
        struct inode *inode = fdentry(file)->d_inode;
        struct btrfs_root *root = BTRFS_I(inode)->root;
+       struct btrfs_ioctl_defrag_range_args *range;
        int ret;
 
        ret = mnt_want_write(file->f_path.mnt);
@@ -1308,7 +1363,30 @@ static int btrfs_ioctl_defrag(struct file *file)
                        ret = -EINVAL;
                        goto out;
                }
-               btrfs_defrag_file(file);
+
+               range = kzalloc(sizeof(*range), GFP_KERNEL);
+               if (!range) {
+                       ret = -ENOMEM;
+                       goto out;
+               }
+
+               if (argp) {
+                       if (copy_from_user(range, argp,
+                                          sizeof(*range))) {
+                               ret = -EFAULT;
+                               kfree(range);
+                       }
+                       /* compression requires us to start the IO */
+                       if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
+                               range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
+                               range->extent_thresh = (u32)-1;
+                       }
+               } else {
+                       /* the rest are all set to zero by kzalloc */
+                       range->len = (u64)-1;
+               }
+               btrfs_defrag_file(file, range);
+               kfree(range);
                break;
        }
 out:
@@ -1782,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
@@ -1831,7 +1987,9 @@ long btrfs_ioctl(struct file *file, unsigned int
        case BTRFS_IOC_DEFAULT_SUBVOL:
                return btrfs_ioctl_default_subvol(file, argp);
        case BTRFS_IOC_DEFRAG:
-               return btrfs_ioctl_defrag(file);
+               return btrfs_ioctl_defrag(file, NULL);
+       case BTRFS_IOC_DEFRAG_RANGE:
+               return btrfs_ioctl_defrag(file, argp);
        case BTRFS_IOC_RESIZE:
                return btrfs_ioctl_resize(root, argp);
        case BTRFS_IOC_ADD_DEV:
@@ -1852,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;