1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2007 Oracle. All rights reserved.
8 #include "transaction.h"
11 * insert a name into a directory, doing overflow properly if there is a hash
12 * collision. data_size indicates how big the item inserted should be. On
13 * success a struct btrfs_dir_item pointer is returned, otherwise it is
16 * The name is not copied into the dir item, you have to do that yourself.
18 static struct btrfs_dir_item *insert_with_overflow(struct btrfs_trans_handle
20 struct btrfs_root *root,
21 struct btrfs_path *path,
22 struct btrfs_key *cpu_key,
27 struct btrfs_fs_info *fs_info = root->fs_info;
30 struct extent_buffer *leaf;
32 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
34 struct btrfs_dir_item *di;
35 di = btrfs_match_dir_item_name(fs_info, path, name, name_len);
37 return ERR_PTR(-EEXIST);
38 btrfs_extend_item(path, data_size);
42 leaf = path->nodes[0];
43 ptr = btrfs_item_ptr(leaf, path->slots[0], char);
44 ASSERT(data_size <= btrfs_item_size(leaf, path->slots[0]));
45 ptr += btrfs_item_size(leaf, path->slots[0]) - data_size;
46 return (struct btrfs_dir_item *)ptr;
50 * xattrs work a lot like directories, this inserts an xattr item
53 int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans,
54 struct btrfs_root *root,
55 struct btrfs_path *path, u64 objectid,
56 const char *name, u16 name_len,
57 const void *data, u16 data_len)
60 struct btrfs_dir_item *dir_item;
61 unsigned long name_ptr, data_ptr;
62 struct btrfs_key key, location;
63 struct btrfs_disk_key disk_key;
64 struct extent_buffer *leaf;
67 if (name_len + data_len > BTRFS_MAX_XATTR_SIZE(root->fs_info))
70 key.objectid = objectid;
71 key.type = BTRFS_XATTR_ITEM_KEY;
72 key.offset = btrfs_name_hash(name, name_len);
74 data_size = sizeof(*dir_item) + name_len + data_len;
75 dir_item = insert_with_overflow(trans, root, path, &key, data_size,
78 return PTR_ERR(dir_item);
79 memset(&location, 0, sizeof(location));
81 leaf = path->nodes[0];
82 btrfs_cpu_key_to_disk(&disk_key, &location);
83 btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
84 btrfs_set_dir_type(leaf, dir_item, BTRFS_FT_XATTR);
85 btrfs_set_dir_name_len(leaf, dir_item, name_len);
86 btrfs_set_dir_transid(leaf, dir_item, trans->transid);
87 btrfs_set_dir_data_len(leaf, dir_item, data_len);
88 name_ptr = (unsigned long)(dir_item + 1);
89 data_ptr = (unsigned long)((char *)name_ptr + name_len);
91 write_extent_buffer(leaf, name, name_ptr, name_len);
92 write_extent_buffer(leaf, data, data_ptr, data_len);
93 btrfs_mark_buffer_dirty(path->nodes[0]);
99 * insert a directory item in the tree, doing all the magic for
100 * both indexes. 'dir' indicates which objectid to insert it into,
101 * 'location' is the key to stuff into the directory item, 'type' is the
102 * type of the inode we're pointing to, and 'index' is the sequence number
103 * to use for the second index (if one is created).
104 * Will return 0 or -ENOMEM
106 int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, const char *name,
107 int name_len, struct btrfs_inode *dir,
108 struct btrfs_key *location, u8 type, u64 index)
112 struct btrfs_root *root = dir->root;
113 struct btrfs_path *path;
114 struct btrfs_dir_item *dir_item;
115 struct extent_buffer *leaf;
116 unsigned long name_ptr;
117 struct btrfs_key key;
118 struct btrfs_disk_key disk_key;
121 key.objectid = btrfs_ino(dir);
122 key.type = BTRFS_DIR_ITEM_KEY;
123 key.offset = btrfs_name_hash(name, name_len);
125 path = btrfs_alloc_path();
129 btrfs_cpu_key_to_disk(&disk_key, location);
131 data_size = sizeof(*dir_item) + name_len;
132 dir_item = insert_with_overflow(trans, root, path, &key, data_size,
134 if (IS_ERR(dir_item)) {
135 ret = PTR_ERR(dir_item);
141 leaf = path->nodes[0];
142 btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
143 btrfs_set_dir_type(leaf, dir_item, type);
144 btrfs_set_dir_data_len(leaf, dir_item, 0);
145 btrfs_set_dir_name_len(leaf, dir_item, name_len);
146 btrfs_set_dir_transid(leaf, dir_item, trans->transid);
147 name_ptr = (unsigned long)(dir_item + 1);
149 write_extent_buffer(leaf, name, name_ptr, name_len);
150 btrfs_mark_buffer_dirty(leaf);
153 /* FIXME, use some real flag for selecting the extra index */
154 if (root == root->fs_info->tree_root) {
158 btrfs_release_path(path);
160 ret2 = btrfs_insert_delayed_dir_index(trans, name, name_len, dir,
161 &disk_key, type, index);
163 btrfs_free_path(path);
171 static struct btrfs_dir_item *btrfs_lookup_match_dir(
172 struct btrfs_trans_handle *trans,
173 struct btrfs_root *root, struct btrfs_path *path,
174 struct btrfs_key *key, const char *name,
175 int name_len, int mod)
177 const int ins_len = (mod < 0 ? -1 : 0);
178 const int cow = (mod != 0);
181 ret = btrfs_search_slot(trans, root, key, path, ins_len, cow);
185 return ERR_PTR(-ENOENT);
187 return btrfs_match_dir_item_name(root->fs_info, path, name, name_len);
191 * Lookup for a directory item by name.
193 * @trans: The transaction handle to use. Can be NULL if @mod is 0.
194 * @root: The root of the target tree.
195 * @path: Path to use for the search.
196 * @dir: The inode number (objectid) of the directory.
197 * @name: The name associated to the directory entry we are looking for.
198 * @name_len: The length of the name.
199 * @mod: Used to indicate if the tree search is meant for a read only
200 * lookup, for a modification lookup or for a deletion lookup, so
201 * its value should be 0, 1 or -1, respectively.
203 * Returns: NULL if the dir item does not exists, an error pointer if an error
204 * happened, or a pointer to a dir item if a dir item exists for the given name.
206 struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
207 struct btrfs_root *root,
208 struct btrfs_path *path, u64 dir,
209 const char *name, int name_len,
212 struct btrfs_key key;
213 struct btrfs_dir_item *di;
216 key.type = BTRFS_DIR_ITEM_KEY;
217 key.offset = btrfs_name_hash(name, name_len);
219 di = btrfs_lookup_match_dir(trans, root, path, &key, name, name_len, mod);
220 if (IS_ERR(di) && PTR_ERR(di) == -ENOENT)
226 int btrfs_check_dir_item_collision(struct btrfs_root *root, u64 dir,
227 const char *name, int name_len)
230 struct btrfs_key key;
231 struct btrfs_dir_item *di;
233 struct extent_buffer *leaf;
235 struct btrfs_path *path;
237 path = btrfs_alloc_path();
242 key.type = BTRFS_DIR_ITEM_KEY;
243 key.offset = btrfs_name_hash(name, name_len);
245 di = btrfs_lookup_match_dir(NULL, root, path, &key, name, name_len, 0);
248 /* Nothing found, we're safe */
249 if (ret == -ENOENT) {
258 /* we found an item, look for our name in the item */
260 /* our exact name was found */
266 * see if there is room in the item to insert this
269 data_size = sizeof(*di) + name_len;
270 leaf = path->nodes[0];
271 slot = path->slots[0];
272 if (data_size + btrfs_item_size(leaf, slot) +
273 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
276 /* plenty of insertion room */
280 btrfs_free_path(path);
285 * Lookup for a directory index item by name and index number.
287 * @trans: The transaction handle to use. Can be NULL if @mod is 0.
288 * @root: The root of the target tree.
289 * @path: Path to use for the search.
290 * @dir: The inode number (objectid) of the directory.
291 * @index: The index number.
292 * @name: The name associated to the directory entry we are looking for.
293 * @name_len: The length of the name.
294 * @mod: Used to indicate if the tree search is meant for a read only
295 * lookup, for a modification lookup or for a deletion lookup, so
296 * its value should be 0, 1 or -1, respectively.
298 * Returns: NULL if the dir index item does not exists, an error pointer if an
299 * error happened, or a pointer to a dir item if the dir index item exists and
300 * matches the criteria (name and index number).
302 struct btrfs_dir_item *
303 btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
304 struct btrfs_root *root,
305 struct btrfs_path *path, u64 dir,
306 u64 index, const char *name, int name_len,
309 struct btrfs_dir_item *di;
310 struct btrfs_key key;
313 key.type = BTRFS_DIR_INDEX_KEY;
316 di = btrfs_lookup_match_dir(trans, root, path, &key, name, name_len, mod);
317 if (di == ERR_PTR(-ENOENT))
323 struct btrfs_dir_item *
324 btrfs_search_dir_index_item(struct btrfs_root *root,
325 struct btrfs_path *path, u64 dirid,
326 const char *name, int name_len)
328 struct btrfs_dir_item *di;
329 struct btrfs_key key;
332 key.objectid = dirid;
333 key.type = BTRFS_DIR_INDEX_KEY;
336 btrfs_for_each_slot(root, &key, &key, path, ret) {
337 if (key.objectid != dirid || key.type != BTRFS_DIR_INDEX_KEY)
340 di = btrfs_match_dir_item_name(root->fs_info, path,
345 /* Adjust return code if the key was not found in the next leaf. */
352 struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,
353 struct btrfs_root *root,
354 struct btrfs_path *path, u64 dir,
355 const char *name, u16 name_len,
358 struct btrfs_key key;
359 struct btrfs_dir_item *di;
362 key.type = BTRFS_XATTR_ITEM_KEY;
363 key.offset = btrfs_name_hash(name, name_len);
365 di = btrfs_lookup_match_dir(trans, root, path, &key, name, name_len, mod);
366 if (IS_ERR(di) && PTR_ERR(di) == -ENOENT)
373 * helper function to look at the directory item pointed to by 'path'
374 * this walks through all the entries in a dir item and finds one
375 * for a specific name.
377 struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_fs_info *fs_info,
378 struct btrfs_path *path,
379 const char *name, int name_len)
381 struct btrfs_dir_item *dir_item;
382 unsigned long name_ptr;
386 struct extent_buffer *leaf;
388 leaf = path->nodes[0];
389 dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item);
391 total_len = btrfs_item_size(leaf, path->slots[0]);
392 while (cur < total_len) {
393 this_len = sizeof(*dir_item) +
394 btrfs_dir_name_len(leaf, dir_item) +
395 btrfs_dir_data_len(leaf, dir_item);
396 name_ptr = (unsigned long)(dir_item + 1);
398 if (btrfs_dir_name_len(leaf, dir_item) == name_len &&
399 memcmp_extent_buffer(leaf, name, name_ptr, name_len) == 0)
403 dir_item = (struct btrfs_dir_item *)((char *)dir_item +
410 * given a pointer into a directory item, delete it. This
411 * handles items that have more than one entry in them.
413 int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
414 struct btrfs_root *root,
415 struct btrfs_path *path,
416 struct btrfs_dir_item *di)
419 struct extent_buffer *leaf;
424 leaf = path->nodes[0];
425 sub_item_len = sizeof(*di) + btrfs_dir_name_len(leaf, di) +
426 btrfs_dir_data_len(leaf, di);
427 item_len = btrfs_item_size(leaf, path->slots[0]);
428 if (sub_item_len == item_len) {
429 ret = btrfs_del_item(trans, root, path);
432 unsigned long ptr = (unsigned long)di;
435 start = btrfs_item_ptr_offset(leaf, path->slots[0]);
436 memmove_extent_buffer(leaf, ptr, ptr + sub_item_len,
437 item_len - (ptr + sub_item_len - start));
438 btrfs_truncate_item(path, item_len - sub_item_len, 1);