Set license using %license
[platform/upstream/btrfs-progs.git] / inode-item.c
index d94930f..1cc1066 100644 (file)
@@ -19,7 +19,7 @@
 #include "ctree.h"
 #include "disk-io.h"
 #include "transaction.h"
-#include "crc32c.h"
+#include "hash.h"
 
 static int find_name_in_backref(struct btrfs_path *path, const char * name,
                         int name_len, struct btrfs_inode_ref **ref_ret)
@@ -64,7 +64,7 @@ int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans,
 
        key.objectid = inode_objectid;
        key.offset = ref_objectid;
-       btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
+       key.type = BTRFS_INODE_REF_KEY;
 
        path = btrfs_alloc_path();
        if (!path)
@@ -79,7 +79,7 @@ int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans,
                        goto out;
 
                old_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
-               ret = btrfs_extend_item(trans, root, path, ins_len);
+               ret = btrfs_extend_item(root, path, ins_len);
                BUG_ON(ret);
                ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
                                     struct btrfs_inode_ref);
@@ -89,6 +89,8 @@ int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans,
                ptr = (unsigned long)(ref + 1);
                ret = 0;
        } else if (ret < 0) {
+               if (ret == -EOVERFLOW)
+                       ret = -EMLINK;
                goto out;
        } else {
                ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
@@ -102,6 +104,14 @@ int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans,
 
 out:
        btrfs_free_path(path);
+
+       if (ret == -EMLINK) {
+               if (btrfs_fs_incompat(root->fs_info, EXTENDED_IREF))
+                       ret = btrfs_insert_inode_extref(trans, root, name,
+                                                       name_len,
+                                                       inode_objectid,
+                                                       ref_objectid, index);
+       }
        return ret;
 }
 
@@ -117,13 +127,13 @@ int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
        struct btrfs_key found_key;
 
        ret = btrfs_search_slot(trans, root, location, path, ins_len, cow);
-       if (ret > 0 && btrfs_key_type(location) == BTRFS_ROOT_ITEM_KEY &&
+       if (ret > 0 && location->type == BTRFS_ROOT_ITEM_KEY &&
            location->offset == (u64)-1 && path->slots[0] != 0) {
                slot = path->slots[0] - 1;
                leaf = path->nodes[0];
                btrfs_item_key_to_cpu(leaf, &found_key, slot);
                if (found_key.objectid == location->objectid &&
-                   btrfs_key_type(&found_key) == btrfs_key_type(location)) {
+                   found_key.type == location->type) {
                        path->slots[0]--;
                        return 0;
                }
@@ -147,10 +157,30 @@ int btrfs_insert_inode(struct btrfs_trans_handle *trans, struct btrfs_root
        return ret;
 }
 
-static inline u64 btrfs_extref_hash(u64 parent_ino, const char *name,
-                                   int namelen)
+struct btrfs_inode_ref *btrfs_lookup_inode_ref(struct btrfs_trans_handle *trans,
+               struct btrfs_root *root, struct btrfs_path *path,
+               const char *name, int namelen, u64 ino, u64 parent_ino,
+               int ins_len)
 {
-       return (u64)btrfs_crc32c(parent_ino, name, namelen);
+       struct btrfs_key key;
+       struct btrfs_inode_ref *ret_inode_ref = NULL;
+       int ret = 0;
+
+       key.objectid = ino;
+       key.type = BTRFS_INODE_REF_KEY;
+       key.offset = parent_ino;
+
+       ret = btrfs_search_slot(trans, root, &key, path, ins_len,
+                               ins_len ? 1 : 0);
+       if (ret)
+               goto out;
+
+       find_name_in_backref(path, name, namelen, &ret_inode_ref);
+out:
+       if (ret < 0)
+               return ERR_PTR(ret);
+       else
+               return ret_inode_ref;
 }
 
 static int btrfs_find_name_in_ext_backref(struct btrfs_path *path,
@@ -282,7 +312,7 @@ int btrfs_del_inode_extref(struct btrfs_trans_handle *trans,
        memmove_extent_buffer(leaf, ptr, ptr + del_len,
                              item_size - (ptr + del_len - item_start));
 
-       btrfs_truncate_item(trans, root, path, item_size - del_len, 1);
+       btrfs_truncate_item(root, path, item_size - del_len, 1);
 
 out:
        btrfs_free_path(path);
@@ -324,7 +354,7 @@ int btrfs_insert_inode_extref(struct btrfs_trans_handle *trans,
                                                   name, name_len, NULL))
                        goto out;
 
-               btrfs_extend_item(trans, root, path, ins_len);
+               btrfs_extend_item(root, path, ins_len);
                ret = 0;
        }
 
@@ -350,3 +380,73 @@ out:
 
        return ret;
 }
+
+int btrfs_del_inode_ref(struct btrfs_trans_handle *trans,
+                       struct btrfs_root *root, const char *name, int name_len,
+                       u64 ino, u64 parent_ino, u64 *index)
+{
+       struct btrfs_path *path;
+       struct btrfs_key key;
+       struct btrfs_inode_ref *ref;
+       struct extent_buffer *leaf;
+       unsigned long ptr;
+       unsigned long item_start;
+       u32 item_size;
+       u32 sub_item_len;
+       int ret;
+       int search_ext_refs = 0;
+       int del_len = name_len + sizeof(*ref);
+
+       key.objectid = ino;
+       key.offset = parent_ino;
+       key.type = BTRFS_INODE_REF_KEY;
+
+       path = btrfs_alloc_path();
+       if (!path)
+               return -ENOMEM;
+
+       ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
+       if (ret > 0) {
+               ret = -ENOENT;
+               search_ext_refs = 1;
+               goto out;
+       } else if (ret < 0) {
+               goto out;
+       }
+       if (!find_name_in_backref(path, name, name_len, &ref)) {
+               ret = -ENOENT;
+               search_ext_refs = 1;
+               goto out;
+       }
+       leaf = path->nodes[0];
+       item_size = btrfs_item_size_nr(leaf, path->slots[0]);
+
+       if (index)
+               *index = btrfs_inode_ref_index(leaf, ref);
+
+       if (del_len == item_size) {
+               ret = btrfs_del_item(trans, root, path);
+               goto out;
+       }
+       ptr = (unsigned long)ref;
+       sub_item_len = name_len + sizeof(*ref);
+       item_start = btrfs_item_ptr_offset(leaf, path->slots[0]);
+       memmove_extent_buffer(leaf, ptr, ptr + sub_item_len,
+                             item_size - (ptr + sub_item_len - item_start));
+       btrfs_truncate_item(root, path, item_size - sub_item_len, 1);
+       btrfs_mark_buffer_dirty(path->nodes[0]);
+out:
+       btrfs_free_path(path);
+
+       if (search_ext_refs &&
+           btrfs_fs_incompat(root->fs_info, EXTENDED_IREF)) {
+               /*
+                * No refs were found, or we could not find the name in our ref
+                * array. Find and remove the extended inode ref then.
+                */
+               return btrfs_del_inode_extref(trans, root, name, name_len,
+                                             ino, parent_ino, index);
+       }
+
+       return ret;
+}