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 btrfs_item *item;
31 struct extent_buffer *leaf;
33 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
35 struct btrfs_dir_item *di;
36 di = btrfs_match_dir_item_name(fs_info, path, name, name_len);
38 return ERR_PTR(-EEXIST);
39 btrfs_extend_item(path, data_size);
43 leaf = path->nodes[0];
44 item = btrfs_item_nr(path->slots[0]);
45 ptr = btrfs_item_ptr(leaf, path->slots[0], char);
46 BUG_ON(data_size > btrfs_item_size(leaf, item));
47 ptr += btrfs_item_size(leaf, item) - data_size;
48 return (struct btrfs_dir_item *)ptr;
52 * xattrs work a lot like directories, this inserts an xattr item
55 int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans,
56 struct btrfs_root *root,
57 struct btrfs_path *path, u64 objectid,
58 const char *name, u16 name_len,
59 const void *data, u16 data_len)
62 struct btrfs_dir_item *dir_item;
63 unsigned long name_ptr, data_ptr;
64 struct btrfs_key key, location;
65 struct btrfs_disk_key disk_key;
66 struct extent_buffer *leaf;
69 if (name_len + data_len > BTRFS_MAX_XATTR_SIZE(root->fs_info))
72 key.objectid = objectid;
73 key.type = BTRFS_XATTR_ITEM_KEY;
74 key.offset = btrfs_name_hash(name, name_len);
76 data_size = sizeof(*dir_item) + name_len + data_len;
77 dir_item = insert_with_overflow(trans, root, path, &key, data_size,
80 return PTR_ERR(dir_item);
81 memset(&location, 0, sizeof(location));
83 leaf = path->nodes[0];
84 btrfs_cpu_key_to_disk(&disk_key, &location);
85 btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
86 btrfs_set_dir_type(leaf, dir_item, BTRFS_FT_XATTR);
87 btrfs_set_dir_name_len(leaf, dir_item, name_len);
88 btrfs_set_dir_transid(leaf, dir_item, trans->transid);
89 btrfs_set_dir_data_len(leaf, dir_item, data_len);
90 name_ptr = (unsigned long)(dir_item + 1);
91 data_ptr = (unsigned long)((char *)name_ptr + name_len);
93 write_extent_buffer(leaf, name, name_ptr, name_len);
94 write_extent_buffer(leaf, data, data_ptr, data_len);
95 btrfs_mark_buffer_dirty(path->nodes[0]);
101 * insert a directory item in the tree, doing all the magic for
102 * both indexes. 'dir' indicates which objectid to insert it into,
103 * 'location' is the key to stuff into the directory item, 'type' is the
104 * type of the inode we're pointing to, and 'index' is the sequence number
105 * to use for the second index (if one is created).
106 * Will return 0 or -ENOMEM
108 int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, const char *name,
109 int name_len, struct btrfs_inode *dir,
110 struct btrfs_key *location, u8 type, u64 index)
114 struct btrfs_root *root = dir->root;
115 struct btrfs_path *path;
116 struct btrfs_dir_item *dir_item;
117 struct extent_buffer *leaf;
118 unsigned long name_ptr;
119 struct btrfs_key key;
120 struct btrfs_disk_key disk_key;
123 key.objectid = btrfs_ino(dir);
124 key.type = BTRFS_DIR_ITEM_KEY;
125 key.offset = btrfs_name_hash(name, name_len);
127 path = btrfs_alloc_path();
131 btrfs_cpu_key_to_disk(&disk_key, location);
133 data_size = sizeof(*dir_item) + name_len;
134 dir_item = insert_with_overflow(trans, root, path, &key, data_size,
136 if (IS_ERR(dir_item)) {
137 ret = PTR_ERR(dir_item);
143 leaf = path->nodes[0];
144 btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
145 btrfs_set_dir_type(leaf, dir_item, type);
146 btrfs_set_dir_data_len(leaf, dir_item, 0);
147 btrfs_set_dir_name_len(leaf, dir_item, name_len);
148 btrfs_set_dir_transid(leaf, dir_item, trans->transid);
149 name_ptr = (unsigned long)(dir_item + 1);
151 write_extent_buffer(leaf, name, name_ptr, name_len);
152 btrfs_mark_buffer_dirty(leaf);
155 /* FIXME, use some real flag for selecting the extra index */
156 if (root == root->fs_info->tree_root) {
160 btrfs_release_path(path);
162 ret2 = btrfs_insert_delayed_dir_index(trans, name, name_len, dir,
163 &disk_key, type, index);
165 btrfs_free_path(path);
173 static struct btrfs_dir_item *btrfs_lookup_match_dir(
174 struct btrfs_trans_handle *trans,
175 struct btrfs_root *root, struct btrfs_path *path,
176 struct btrfs_key *key, const char *name,
177 int name_len, int mod)
179 const int ins_len = (mod < 0 ? -1 : 0);
180 const int cow = (mod != 0);
183 ret = btrfs_search_slot(trans, root, key, path, ins_len, cow);
187 return ERR_PTR(-ENOENT);
189 return btrfs_match_dir_item_name(root->fs_info, path, name, name_len);
193 * Lookup for a directory item by name.
195 * @trans: The transaction handle to use. Can be NULL if @mod is 0.
196 * @root: The root of the target tree.
197 * @path: Path to use for the search.
198 * @dir: The inode number (objectid) of the directory.
199 * @name: The name associated to the directory entry we are looking for.
200 * @name_len: The length of the name.
201 * @mod: Used to indicate if the tree search is meant for a read only
202 * lookup, for a modification lookup or for a deletion lookup, so
203 * its value should be 0, 1 or -1, respectively.
205 * Returns: NULL if the dir item does not exists, an error pointer if an error
206 * happened, or a pointer to a dir item if a dir item exists for the given name.
208 struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
209 struct btrfs_root *root,
210 struct btrfs_path *path, u64 dir,
211 const char *name, int name_len,
214 struct btrfs_key key;
215 struct btrfs_dir_item *di;
218 key.type = BTRFS_DIR_ITEM_KEY;
219 key.offset = btrfs_name_hash(name, name_len);
221 di = btrfs_lookup_match_dir(trans, root, path, &key, name, name_len, mod);
222 if (IS_ERR(di) && PTR_ERR(di) == -ENOENT)
228 int btrfs_check_dir_item_collision(struct btrfs_root *root, u64 dir,
229 const char *name, int name_len)
232 struct btrfs_key key;
233 struct btrfs_dir_item *di;
235 struct extent_buffer *leaf;
237 struct btrfs_path *path;
239 path = btrfs_alloc_path();
244 key.type = BTRFS_DIR_ITEM_KEY;
245 key.offset = btrfs_name_hash(name, name_len);
247 di = btrfs_lookup_match_dir(NULL, root, path, &key, name, name_len, 0);
250 /* Nothing found, we're safe */
251 if (ret == -ENOENT) {
260 /* we found an item, look for our name in the item */
262 /* our exact name was found */
268 * see if there is room in the item to insert this
271 data_size = sizeof(*di) + name_len;
272 leaf = path->nodes[0];
273 slot = path->slots[0];
274 if (data_size + btrfs_item_size_nr(leaf, slot) +
275 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
278 /* plenty of insertion room */
282 btrfs_free_path(path);
287 * Lookup for a directory index item by name and index number.
289 * @trans: The transaction handle to use. Can be NULL if @mod is 0.
290 * @root: The root of the target tree.
291 * @path: Path to use for the search.
292 * @dir: The inode number (objectid) of the directory.
293 * @index: The index number.
294 * @name: The name associated to the directory entry we are looking for.
295 * @name_len: The length of the name.
296 * @mod: Used to indicate if the tree search is meant for a read only
297 * lookup, for a modification lookup or for a deletion lookup, so
298 * its value should be 0, 1 or -1, respectively.
300 * Returns: NULL if the dir index item does not exists, an error pointer if an
301 * error happened, or a pointer to a dir item if the dir index item exists and
302 * matches the criteria (name and index number).
304 struct btrfs_dir_item *
305 btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
306 struct btrfs_root *root,
307 struct btrfs_path *path, u64 dir,
308 u64 index, const char *name, int name_len,
311 struct btrfs_dir_item *di;
312 struct btrfs_key key;
315 key.type = BTRFS_DIR_INDEX_KEY;
318 di = btrfs_lookup_match_dir(trans, root, path, &key, name, name_len, mod);
319 if (di == ERR_PTR(-ENOENT))
325 struct btrfs_dir_item *
326 btrfs_search_dir_index_item(struct btrfs_root *root,
327 struct btrfs_path *path, u64 dirid,
328 const char *name, int name_len)
330 struct extent_buffer *leaf;
331 struct btrfs_dir_item *di;
332 struct btrfs_key key;
336 key.objectid = dirid;
337 key.type = BTRFS_DIR_INDEX_KEY;
340 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
344 leaf = path->nodes[0];
345 nritems = btrfs_header_nritems(leaf);
348 if (path->slots[0] >= nritems) {
349 ret = btrfs_next_leaf(root, path);
354 leaf = path->nodes[0];
355 nritems = btrfs_header_nritems(leaf);
359 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
360 if (key.objectid != dirid || key.type != BTRFS_DIR_INDEX_KEY)
363 di = btrfs_match_dir_item_name(root->fs_info, path,
373 struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,
374 struct btrfs_root *root,
375 struct btrfs_path *path, u64 dir,
376 const char *name, u16 name_len,
379 struct btrfs_key key;
380 struct btrfs_dir_item *di;
383 key.type = BTRFS_XATTR_ITEM_KEY;
384 key.offset = btrfs_name_hash(name, name_len);
386 di = btrfs_lookup_match_dir(trans, root, path, &key, name, name_len, mod);
387 if (IS_ERR(di) && PTR_ERR(di) == -ENOENT)
394 * helper function to look at the directory item pointed to by 'path'
395 * this walks through all the entries in a dir item and finds one
396 * for a specific name.
398 struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_fs_info *fs_info,
399 struct btrfs_path *path,
400 const char *name, int name_len)
402 struct btrfs_dir_item *dir_item;
403 unsigned long name_ptr;
407 struct extent_buffer *leaf;
409 leaf = path->nodes[0];
410 dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item);
412 total_len = btrfs_item_size_nr(leaf, path->slots[0]);
413 while (cur < total_len) {
414 this_len = sizeof(*dir_item) +
415 btrfs_dir_name_len(leaf, dir_item) +
416 btrfs_dir_data_len(leaf, dir_item);
417 name_ptr = (unsigned long)(dir_item + 1);
419 if (btrfs_dir_name_len(leaf, dir_item) == name_len &&
420 memcmp_extent_buffer(leaf, name, name_ptr, name_len) == 0)
424 dir_item = (struct btrfs_dir_item *)((char *)dir_item +
431 * given a pointer into a directory item, delete it. This
432 * handles items that have more than one entry in them.
434 int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
435 struct btrfs_root *root,
436 struct btrfs_path *path,
437 struct btrfs_dir_item *di)
440 struct extent_buffer *leaf;
445 leaf = path->nodes[0];
446 sub_item_len = sizeof(*di) + btrfs_dir_name_len(leaf, di) +
447 btrfs_dir_data_len(leaf, di);
448 item_len = btrfs_item_size_nr(leaf, path->slots[0]);
449 if (sub_item_len == item_len) {
450 ret = btrfs_del_item(trans, root, path);
453 unsigned long ptr = (unsigned long)di;
456 start = btrfs_item_ptr_offset(leaf, path->slots[0]);
457 memmove_extent_buffer(leaf, ptr, ptr + sub_item_len,
458 item_len - (ptr + sub_item_len - start));
459 btrfs_truncate_item(path, item_len - sub_item_len, 1);