2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
21 #include "kerncompat.h"
22 #include "radix-tree.h"
26 #include "transaction.h"
28 static struct btrfs_dir_item *insert_with_overflow(struct
29 btrfs_trans_handle *trans,
30 struct btrfs_root *root,
31 struct btrfs_path *path,
32 struct btrfs_key *cpu_key,
37 struct btrfs_item *item;
38 struct btrfs_leaf *leaf;
40 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
42 ret = btrfs_extend_item(trans, root, path, data_size);
48 leaf = &path->nodes[0]->leaf;
49 item = leaf->items + path->slots[0];
50 ptr = btrfs_item_ptr(leaf, path->slots[0], char);
51 BUG_ON(data_size > btrfs_item_size(item));
52 ptr += btrfs_item_size(item) - data_size;
53 return (struct btrfs_dir_item *)ptr;
56 int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
57 *root, char *name, int name_len, u64 dir,
58 struct btrfs_key *location, u8 type)
61 struct btrfs_path path;
62 struct btrfs_dir_item *dir_item;
68 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
69 if (name_len == 1 && *name == '.')
71 else if (name_len == 2 && name[0] == '.' && name[1] == '.')
74 ret = btrfs_name_hash(name, name_len, &key.offset);
76 btrfs_init_path(&path);
77 data_size = sizeof(*dir_item) + name_len;
78 dir_item = insert_with_overflow(trans, root, &path, &key, data_size);
83 btrfs_cpu_key_to_disk(&dir_item->location, location);
84 btrfs_set_dir_type(dir_item, type);
85 btrfs_set_dir_flags(dir_item, 0);
86 btrfs_set_dir_name_len(dir_item, name_len);
87 name_ptr = (char *)(dir_item + 1);
88 memcpy(name_ptr, name, name_len);
90 /* FIXME, use some real flag for selecting the extra index */
91 if (root == root->fs_info->tree_root)
94 btrfs_release_path(root, &path);
95 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
96 key.offset = location->objectid;
97 dir_item = insert_with_overflow(trans, root, &path, &key, data_size);
102 btrfs_cpu_key_to_disk(&dir_item->location, location);
103 btrfs_set_dir_type(dir_item, type);
104 btrfs_set_dir_flags(dir_item, 0);
105 btrfs_set_dir_name_len(dir_item, name_len);
106 name_ptr = (char *)(dir_item + 1);
107 memcpy(name_ptr, name, name_len);
109 btrfs_release_path(root, &path);
113 int btrfs_lookup_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
114 *root, struct btrfs_path *path, u64 dir, char *name,
115 int name_len, int mod)
118 struct btrfs_key key;
119 int ins_len = mod < 0 ? -1 : 0;
123 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
124 ret = btrfs_name_hash(name, name_len, &key.offset);
126 ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
130 int btrfs_match_dir_item_name(struct btrfs_root *root,
131 struct btrfs_path *path, char
134 struct btrfs_dir_item *dir_item;
137 dir_item = btrfs_item_ptr(&path->nodes[0]->leaf, path->slots[0],
138 struct btrfs_dir_item);
139 if (btrfs_dir_name_len(dir_item) != name_len)
141 name_ptr = (char *)(dir_item + 1);
142 if (memcmp(name_ptr, name, name_len))