NFS: Cleanup file handle allocations in fs/nfs/super.c
[safe/jmp/linux-2.6] / fs / btrfs / ioctl.c
index 291aa51..e84ef60 100644 (file)
@@ -39,6 +39,7 @@
 #include <linux/security.h>
 #include <linux/xattr.h>
 #include <linux/vmalloc.h>
+#include <linux/slab.h>
 #include "compat.h"
 #include "ctree.h"
 #include "disk-io.h"
@@ -48,7 +49,6 @@
 #include "print-tree.h"
 #include "volumes.h"
 #include "locking.h"
-#include "ctree.h"
 
 /* Mask out flags that are inappropriate for the given type of inode. */
 static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
@@ -511,7 +511,7 @@ static int should_defrag_range(struct inode *inode, u64 start, u64 len,
                em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
                unlock_extent(io_tree, start, start + len - 1, GFP_NOFS);
 
-               if (!em)
+               if (IS_ERR(em))
                        return 0;
        }
 
@@ -997,8 +997,8 @@ 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;
@@ -1147,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);
@@ -1161,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 ||
@@ -1184,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;
 
        }
@@ -1209,12 +1212,18 @@ static noinline int btrfs_ioctl_ino_lookup(struct file *file,
                return -EPERM;
 
        args = kmalloc(sizeof(*args), GFP_KERNEL);
+       if (!args)
+               return -ENOMEM;
+
        if (copy_from_user(args, argp, sizeof(*args))) {
                kfree(args);
                return -EFAULT;
        }
        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);
@@ -1369,6 +1378,7 @@ static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
                                           sizeof(*range))) {
                                ret = -EFAULT;
                                kfree(range);
+                               goto out;
                        }
                        /* compression requires us to start the IO */
                        if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {